relation-graph is a graph database library implementing the SPARQL standard.
Its goal is to provide a compliant, safe and fast graph database. It also provides a set of utility functions for reading, writing, and processing RDF files.
relation-graph implements the following specifications:
- SPARQL 1.1 Query, SPARQL 1.1 Update, and SPARQL 1.1 Federated Query.
- Turtle, TriG, N-Triples, N-Quads, and RDF XML RDF serialization formats for both data ingestion and retrieval using the Rio library.
- SPARQL Query Results XML Format, SPARQL 1.1 Query Results JSON Format and SPARQL 1.1 Query Results CSV and TSV Formats.
Usage example with the MemoryStore:
use relation_graph_core::MemoryStore;use relation_graph_core::model::*;use relation_graph_core::sparql::QueryResults;let store = MemoryStore::new();// insertionlet ex = NamedNode::new("http://example.com")?;let quad = Quad::new(ex.clone(), ex.clone(), ex.clone(),None);
store.insert(quad.clone());// quad filterlet results:Vec<Quad> = store.quads_for_pattern(Some(ex.as_ref().into()),None,None,None).collect();assert_eq!(vec![quad], results);// SPARQL queryifletQueryResults::Solutions(mut solutions) = store.query("SELECT ?s WHERE { ?s ?p ?o }")? {assert_eq!(solutions.next().unwrap()?.get("s"),Some(&ex.into()));}rustup target add wasm32-unknown-unknown
# install SDK
sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"dfx start --clean --background
dfx deployExample: hello
dfx canister call relation_graph_hello hello- arg1: principal
- arg2: role (values: user, admin)
dfx canister call relation_graph acl_grant '("rrkah-fqaaa-aaaaa-aaaaq-cai","user")'- arg1: principal
dfx canister call relation_graph acl_revoke rrkah-fqaaa-aaaaa-aaaaq-cai- arg1: limit (limit of how many records returned)
dfx canister call relation_graph acl_show 10- arg1:
tsv, csv, xml, json - arg2: sparql query
Query-1:
dfx canister call relation_graph sparql_query '("tsv","
SELECT*WHERE {
?persona :Person ;
:name ?name;
:age ?age ;
:gender ?gender ;
:friends ?friends ;
:birthdate ?birthdate .
FILTER (year(?birthdate) >= 1960) .
FILTER regex(?name, \"^Ma\", \"i\") .
} LIMIT 2
")'Query-2:
dfx canister call relation_graph sparql_query '("tsv","
SELECTDISTINCT?name?age?gender?birthdateWHERE {
:P1 :friends ?friends1.
?friends1 :friends ?friends2.
?friends2 :friends ?friends3.
?friends3a :Person ;
:name ?name;
:age ?age ;
:gender \"Male\" ;
:birthdate ?birthdate .
}
LIMIT 10
")'Query-3:
dfx canister call relation_graph sparql_query '("tsv","
SELECTDISTINCT?name?age?gender?birthdateWHERE {
:P1 :friends ?friends1.
?friends1 :friends ?friends2.
?friends2a :Person ;
:name ?name;
:age ?age ;
:gender ?gender ;
:birthdate ?birthdate .
}
LIMIT 10
")'Query-4:
dfx canister call relation_graph sparql_query '("tsv","
SELECTDISTINCT?name?age?gender?birthdateWHERE {
:P1 :friends ?friends1.
?friends1a :Person ;
:name ?name;
:age ?age ;
:gender ?gender ;
:birthdate ?birthdate .
}
LIMIT 10
")'Insert-1:
dfx canister call relation_graph sparql_update '(" INSERTDATA
{ :P001 :name \"Luna\" ;
:gender \"Female\" ;
:age 35 ;
:birthdate \"1989-10-14\"^^xsd:date ;
:friends :P2, :P3 .
}
")'Query-1: query the inserted person
dfx canister call relation_graph sparql_query '("tsv","
SELECT?name?friendsWHERE {
:P001 :name ?name;
:age ?age ;
:gender ?gender ;
:friends ?friends ;
:birthdate ?birthdate .
} ")'Query-2: query the inserted person's friends
dfx canister call relation_graph sparql_query '("tsv","
SELECTDISTINCT?friend?friend_nameWHERE {
:P001 :friends ?friend .
?friend :name ?friend_name
} ")'Changes to existing triples are performed as a delete operation followed by an insert
operation in a single update request. The specification refers to this as DELETE/INSERT
Update-1: update age to 36 for person P001
dfx canister call relation_graph sparql_update '(" DELETE
{ :P001 :age ?o }
INSERT
{ :P001 :age 36 }
WHERE
{ :P001 :age ?o }
")'Query-1: check update result
dfx canister call relation_graph sparql_query '("tsv","
SELECT?p?oWHERE { :P001 ?p?o .
}
")'Delete-1: delete all properties of a person P001
dfx canister call relation_graph sparql_update '(" DELETEWHERE
{
:P001 ?p?o .
}
")'Delete-2: delete partial properties of a person
dfx canister call relation_graph sparql_update '("
DELETEWHERE
{
:P001 :age ?age;
:name ?name .
}
")'Query-1: check deletion result
dfx canister call relation_graph sparql_query '("tsv","
SELECT?p?oWHERE { :P001 ?p?o .
}
")'