Deploying query services using Graph Indexer

Here we will introduce how to quickly build a graph-indexer.

Prepare the environment

  1. Install JDK(Requires JDK11 and above)

brew install openjdk@11

Check the installation:

java -version
  1. Install nodejs

To complete this tutorial successfully, you must have Node.js installed on your machine.

  • Install Node.js with NVM

Install jena-fuseki

Install and run:

wget  https://dlcdn.apache.org/jena/binaries/apache-jena-fuseki-4.7.0.tar.gz
tar xvf apache-jena-fuseki-4.7.0.tar.gz
cd apache-jena-fuseki-4.7.0
sh fuseki-server

Create a dataset

You can access http://localhost:3030, and create a demo dataset(Figure 1-1, Figure 1-2)

A simple Graph Indexer

Here, we will use Node.js to accomplish this process.

This example will listen to RDF data on the Mumbai network, between blocks 32362681 and 32362699.

  1. Create a new project and install the ethers (v5), axios, and qs.

mkdir graph-indexer
cd graph-indexer
npm init -y
npm install ethers@5 axios qs
  1. Create an app.js file with the following content:

const axios = require('axios')
const qs = require('qs')
const ethers = require('ethers')
const rpc = 'https://polygon-mumbai.blockpi.network/v1/rpc/public'
let abi = ['event CreateRDF(uint256 indexed tokenId, string rdfStatements)']
const provider = new ethers.providers.JsonRpcProvider(rpc)
const iface = new ethers.utils.Interface(abi)
const filter = {}

// Listen to all new events on the blockchain.
provider.on(filter, async (e) => {
  try {
    let event = iface.parseLog(e)
    if (event) {
      const rdf = event.args[1]
        // todo: Do your own business
      const res = await insertRdf(rdf)
    }
  } catch (error) {}
})

// You can also get the data of the specified filter
const logFilter = {
  fromBlock: 32362681,
  toBlock: 32362699
}
provider
  .getLogs(logFilter)
  .then(function (logs) {
    for (let index = 0; index < logs.length; index++) {
      try {
        const log = logs[index]
        const event = iface.parseLog(log)
        if (event) {
          const rdf = event.args[1]
          insertRdf(rdf)
        }
      } catch (error) {}
    }
  })
  .catch(function (err) {
    console.log(err)
  })

// Insert RDF into Graph database.
function insertRdf(rdfString) {
  const data = qs.stringify({
    update: `PREFIX : <http://relationlabs.ai/entity/>
    PREFIX p: <http://relationlabs.ai/property/>
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    INSERT DATA  {
      ${rdfString}
    }`
  })
  const config = {
    method: 'post',
    maxBodyLength: Infinity,
    url: 'http://localhost:3030/demo/update',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: data
  }

  return axios(config)
    .then(function (response) {
      console.log(JSON.stringify(response.data))
    })
    .catch(function (error) {
      console.log(error)
    })
}
  1. Run the Graph Indexer

node app.js

After the data has been inserted, it can be queried at http://localhost:3030/#/dataset/demo/query.

Last updated