Developer Hub
Relation ProtocolRelation ONE APIRelation Graph API
中文
中文
  • 概述
  • 快速开始
    • 基于Relation Protocol 部署合约
    • 使用Graph Indexer部署Social Graph的查询服务
  • 关键概念
    • RDF
    • Semantic SBTs
    • Social Graph
  • Relation Protocol的架构
  • Schema Standard
    • 概述
    • schema如何约束智能合约
    • schema存储
    • schema列表
  • Contract Open Standard
    • 概述
    • Identity
      • Name Service
    • Relationship
      • Follow
      • Dao
    • Publication
      • Content
      • Privacy Content
  • Open Standard API
    • 介绍
    • EIP-6239
    • 业务接口
      • Identity
      • Relationship
      • Publication
  • Graph Indexer
    • 定义与用途
    • 事件监听
    • 解析数据
    • 构建图谱
  • 集成
    • 快速开始
    • 构建Relation Protocol社交图谱
    • Relation Protocol 资源列表
    • NameService 合约请求示例
    • Follow 合约请求示例
    • Dao 合约请求示例
    • Content 合约请求示例
    • PrivacyContent 合约请求示例
  • Relation Name Service
    • Name Service Api
  • 用例
  • 附录
    • 使用Hardhat部署合约
    • SemanticSBT部署工具
Powered by GitBook
On this page
  • 环境依赖
  • 安装 jena-fuseki
  • 创建数据集
  • 一个简单的 Graph Indexer
  1. 快速开始

使用Graph Indexer部署Social Graph的查询服务

Last updated 2 years ago

这里将介绍如何快速构建一个 graph-indexer。

环境依赖

  1. 安装 jdk(要求 JDK11 及以上)

brew install openjdk@11
sudo yum install java-11-openjdk-devel

安装完成后检查

java -version
  1. 安装 nodejs

开始本教程之前,您的计算机上必须安装。

  • 使用安装 Node.js。

安装 jena-fuseki

安装 jena-fuseki 并运行:

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

创建数据集

一个简单的 Graph Indexer

这里我们使用 nodejs 来完成。

本示例将会监听Mumbai网络上32362681至32362699区块的RDF数据。

  1. 新建一个项目,安装 ethers(v5)、axios、qs 库

mkdir graph-indexer
cd graph-indexer
npm init -y
npm install ethers@5 axios qs
  1. 监听链上数据,创建一个 app.js,内容如下:

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 = {}

// 监听链上所有新的事件
provider.on(filter, async (e) => {
  try {
    let event = iface.parseLog(e)
    if (event) {
      const rdf = event.args[1]
        // todo: 做自己的业务逻辑
      const res = await insertRdf(rdf)
    }
  } catch (error) {}
})

// 也可以获取指定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)
  })

// 将rdf插入数据库
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. 运行 Graph Indexer 程序

node app.js

浏览器访问 ,并创建 demo 数据集(图 1-1、图 1-2)

等待数据插入后,即可在中查询到

Node.js
NVM
http://localhost:3030
http://localhost:3030/#/dataset/demo/query
图 1-1 添加数据集
图 1-2 创建demo数据集
图 1-3 使用SPARQL查询索引结果