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.
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:
Composite the sparql constructed from the last chapter into an "insert" command in Neptune:
Copy 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 .
}}
Call the Neptune service interface to save RDF:
Copy 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:
Copy 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:
Copy 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".
First please create a dataset:
Then import the data from the last step into the graph database.
Copy 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 .
Finally, switch to the Query tag to query which people Alice has followed:
Copy 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
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:
Copy docker run \
-p 7474:7474 -p 7687:7687 \
--name neo4j-neosemantics \
neo4j:5.5.0
Install plugins to support RDF
Copy 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
Open file: conf/neo4j.conf
and append dbms.unmanaged_extension_classes=n10s.endpoint=/rdf
.
then restart Neo4j Service:
Copy docker restart neo4j-neosemantics
Open Neo4j Browser: http://localhost:7474/browser/ Default login is username 'neo4j' and password 'neo4j'.
Execute the following commands in the Neo4j browser input box:
cypher-shell
Copy 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/" );
Execute the following commands in the Neo4j browser input box:
cypher-shell
Copy 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");
Execute the following commands in the Neo4j browser input box:
cypher-shell
Copy MATCH (ss:Resource { p__name : [ "Alice" ] } ) - [: p__following ] -> (oo) RETURN ss, oo