Links
Comment on page

To build a social graph

This protocol supports all graph databases conforming to the RDF specification.
We can have a simple social graph by importing the RDF data constructed from the last chapter.
Figure 7-3 A simple social graph

To use Neptune

Amazon Neptune is a fast and scalable graph database service. It can efficiently store and navigate highly interconnected data. Its query processing engine is optimized for leading graph query languages such as Apache TinkerPop™ Gremlin and W3C's RDF SPARQL. Neptune provides high performance via these open graph frameworks and standard APIs.
With Amazon Neptune we can build a social graph using users' behavioral data on the blockchain.
Steps:
  1. 1.
    Launch a Neptune instance via the aws console.
  2. 2.
    Composite the sparql constructed from the last chapter into an "insert" command in Neptune:
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 {GRAPH <http://relationlabs.ai/relationship> {
:Soul_0x0109c8ee3151bde7b6b5d9f37e9d2c4bc16930fe a :Soul;
p:name "Alice" .
:Soul_0x6247123ec0fe0d25feb811e3c4d4a760c1f2e63e a :Soul;
p:name "Bob" .
:Soul_0x0109c8ee3151bde7b6b5d9f37e9d2c4bc16930fe p:following :Soul_0x6247123ec0fe0d25feb811e3c4d4a760c1f2e63e .
}}
  1. 3.
    Call the Neptune service interface to save RDF:
curl -X POST --data-binary '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 {GRAPH <http://relationlabs.ai/relationship> {
:Soul_0x0109c8ee3151bde7b6b5d9f37e9d2c4bc16930fe a :Soul;
p:name "Alice" .
:Soul_0x6247123ec0fe0d25feb811e3c4d4a760c1f2e63e a :Soul;
p:name "Bob" .
:Soul_0x0109c8ee3151bde7b6b5d9f37e9d2c4bc16930fe p:following :Soul_0x6247123ec0fe0d25feb811e3c4d4a760c1f2e63e . }}' https://your-neptune-endpoint:port/sparql
By now we have indexed the data Alice follow Bob into the graph database. We can query the data via the Neptune service interface:
curl -X POST --data-binary 'query=select * where {
?s a :Soul;
p:name "Alice".
?s p:following ?f .} limit 10' https://your-neptune-endpoint:port/sparql

To use Jena

For demonstration purposes, we will use docker to launch a Jena service:
docker run -p 3030:3030 -e ADMIN_PASSWORD=pw123 stain/jena-fuseki
If successful, please visit http://localhost:3030/ using the account "admin" and password "pw123".
  1. 1.
    First please create a dataset:
Figure 7-4 Add a dataset
Figure 7-5 Create demo dataset
  1. 2.
    Then import the data from the last step into the graph database.
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#>
:Soul_0x0109c8ee3151bde7b6b5d9f37e9d2c4bc16930fe a :Soul;
p:name "Alice" .
:Soul_0x6247123ec0fe0d25feb811e3c4d4a760c1f2e63e a :Soul;
p:name "Bob" .
:Soul_0x0109c8ee3151bde7b6b5d9f37e9d2c4bc16930fe p:following :Soul_0x6247123ec0fe0d25feb811e3c4d4a760c1f2e63e .
Figure 7-6 Upload RDF data
Figure 7-7 Select a file and upload
  1. 3.
    Finally, switch to the Query tag to query which people Alice has followed:
PREFIX : <http://relationlabs.ai/entity/>
PREFIX p: <http://relationlabs.ai/property/>
SELECT * WHERE {
?me a :Soul;
p:name "Alice";
p:following ?following .
?following p:name ?name .
}
LIMIT 10
Figure 7-8 Query with SPARQL
Figure 7-9 Query result
The query result is : Alice's address 0x0109c8ee3151bde7b6b5d9f37e9d2c4bc16930fe followed Bob's address 0x6247123ec0fe0d25feb811e3c4d4a760c1f2e63e.

To use Neo4j

For demonstration purposes, we will use docker to launch a Neo4j service:
docker run \
-p 7474:7474 -p 7687:7687 \
--name neo4j-neosemantics \
neo4j:5.5.0
  1. 1.
    Install plugins to support RDF
docker exec -it neo4j-neosemantics bash
cd plugins/
wget https://github.com/neo4j-labs/neosemantics/releases/download/5.5.0.0/neosemantics-5.5.0.0.jar
  1. 2.
    Configure Plugin
Open file: conf/neo4j.conf and append dbms.unmanaged_extension_classes=n10s.endpoint=/rdf.
then restart Neo4j Service:
docker restart neo4j-neosemantics
Open Neo4j Browser: http://localhost:7474/browser/ Default login is username 'neo4j' and password 'neo4j'.
  1. 3.
    Configure Graph
Execute the following commands in the Neo4j browser input box:
cypher-shell
CALL n10s.graphconfig.init();
CREATE CONSTRAINT n10s_unique_uri FOR (r:Resource) REQUIRE r.uri IS UNIQUE;
CALL n10s.graphconfig.init( { handleMultival: "ARRAY" })
CALL n10s.nsprefixes.add("e", "http://relationlabs.ai/entity/");
CALL n10s.nsprefixes.add("p", "http://relationlabs.ai/property/");
  1. 4.
    Importing RDF Data
Execute the following commands in the Neo4j browser input box:
cypher-shell
CALL n10s.rdf.import.inline("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#>
:Soul_0x0109c8ee3151bde7b6b5d9f37e9d2c4bc16930fe a :Soul;
p:name \"Alice\" .
:Soul_0x6247123ec0fe0d25feb811e3c4d4a760c1f2e63e a :Soul;
p:name \"Bob\" .
:Soul_0x0109c8ee3151bde7b6b5d9f37e9d2c4bc16930fe p:following :Soul_0x6247123ec0fe0d25feb811e3c4d4a760c1f2e63e .","Turtle");
  1. 5.
    Querying
Execute the following commands in the Neo4j browser input box:
cypher-shell
MATCH (ss:Resource {p__name: ["Alice"]})-[:p__following]->(oo) RETURN ss, oo
Figure 7-10 To use Neo4j
Last modified 8mo ago