Developer Hub
Relation ProtocolRelation ONE APIRelation Graph API
English
English
  • Overview
  • QUICK START
    • Deploy a Semantic SBT contract leveraging Relation Protocol
    • Deploying query services using Graph Indexer
  • KEY CONCEPTS
    • RDF
    • Semantic SBTs
    • Social Graph
  • Architecture
  • Schema Standard
    • Overview
    • How schemas limit smart contracts
    • Store schema
    • List of schemas
  • Contract Open Standard
    • Overview
    • Identity
      • Name Service
    • Relationship
      • Follow
      • Dao
    • Publication
      • Content
      • Privacy Content
  • Open Standard API
    • Introduction
    • EIP-6239
    • Business Interface
      • Identity
      • Relationship
      • Publication
  • Graph Indexer
    • Definition and usage
    • Listen to events
    • To parse RDF data
    • To build a social graph
  • Integrations
    • Quick start
    • Construct a social graph with Relation Protocol
    • List of resources
    • NameService contract guide
    • Follow contract guide
    • Dao contract guide
    • Content contract guide
    • PrivacyContent contract guide
  • Relation Name Service
    • Name Service Api
  • Use Case
  • APPENDIX
    • Deploy a contract using Hardhat
    • SemanticSBT Deployment tool
Powered by GitBook
On this page
  • Prepare the environment
  • Install jena-fuseki
  • Create a dataset
  • A simple Graph Indexer
  1. QUICK START

Deploying query services using Graph Indexer

Last updated 2 years ago

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
sudo yum install java-11-openjdk-devel

Check the installation:

java -version
  1. Install nodejs

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

  • Install Node.js with

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

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

You can access , and create a demo dataset(Figure 1-1, Figure 1-2)

After the data has been inserted, it can be queried at .

Node.js
NVM
http://localhost:3030
http://localhost:3030/#/dataset/demo/query
Figure 1-1 Add a dataset
Figure 1-2 Create demo dataset
Figure 1-3 Query results using SPARQL