Here we will introduce how to quickly build a graph-indexer.
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
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.
mkdir graph-indexer
cd graph-indexer
npm init -y
npm install ethers@5 axios qs
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)
})
}