Skip to content

Commit 77fe7c8

Browse files
committed
Run the example queries agains the local fuseki
1. prepare the local fuseki store with some data 2. execute the example queries
1 parent c97cfd3 commit 77fe7c8

File tree

1 file changed

+50
-10
lines changed

1 file changed

+50
-10
lines changed

examples/sparqlstore_example.py

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,42 @@
33
"""
44

55
from rdflib import Graph, Namespace, URIRef
6-
from rdflib.plugins.stores.sparqlstore import SPARQLStore
7-
from rdflib.term import Identifier
6+
from rdflib.plugins.stores.sparqlstore import SPARQLUpdateStore, SPARQLStore
7+
from rdflib.term import Identifier, Literal
8+
from rdflib.namespace import RDF, SKOS
9+
10+
# Shows examples of the useage of SPARQLStore and SPARQLUpdateStore against local SPARQL1.1 endpoint if
11+
# available. This assumes SPARQL1.1 query/update endpoints running locally at
12+
# http://localhost:3030/db/
13+
#
14+
# It uses the same endpoint as the test_dataset.py!
15+
#
16+
# For the tests here to run, you can for example start fuseki with:
17+
# ./fuseki-server --mem --update /db
18+
19+
# THIS WILL ADD DATA TO THE /db dataset
820

921
if __name__ == "__main__":
1022
dbo = Namespace("http://dbpedia.org/ontology/")
23+
dbr = Namespace("http://dbpedia.org/resource/")
24+
25+
# EXAMPLE Update Store 1:
26+
graph = Graph("SPARQLUpdateStore", identifier="http://dbpedia.org")
27+
graph.open(("http://localhost:3030/db/sparql", "http://localhost:3030/db/update"))
28+
graph.add((dbr.Berlin, dbo.populationTotal, Literal(3)))
29+
graph.add((dbr.Brisbane, dbo.populationTotal, Literal(2)))
30+
31+
# EXAMPLE Update Store 2:
32+
st = SPARQLUpdateStore(query_endpoint="http://localhost:3030/db/sparql", update_endpoint="http://localhost:3030/db/update")
33+
graph = Graph(store=st, identifier="http://dbpedia.org")
34+
graph.add((dbr["Category:Capitals_in_Europe"], RDF.type, SKOS.Concept))
35+
graph.add((dbr["Category:Holy_Grail"], RDF.type, SKOS.Concept))
36+
graph.add((dbr["Category:Hospital_ships_of_Japan"], RDF.type, SKOS.Concept))
1137

12-
# EXAMPLE 1: using a Graph with the Store type string set to "SPARQLStore"
38+
39+
# EXAMPLE Store 1: using a Graph with the Store type string set to "SPARQLStore"
1340
graph = Graph("SPARQLStore", identifier="http://dbpedia.org")
14-
graph.open("http://dbpedia.org/sparql")
41+
graph.open("http://localhost:3030/db/sparql")
1542

1643
pop = graph.value(URIRef("http://dbpedia.org/resource/Berlin"), dbo.populationTotal)
1744
assert isinstance(pop, Identifier)
@@ -23,8 +50,8 @@
2350
)
2451
print()
2552

26-
# EXAMPLE 2: using a SPARQLStore object directly
27-
st = SPARQLStore(query_endpoint="http://dbpedia.org/sparql")
53+
# EXAMPLE Query 2: using a SPARQLStore object directly
54+
st = SPARQLStore(query_endpoint="http://localhost:3030/db/sparql")
2855

2956
for p in st.objects(
3057
URIRef("http://dbpedia.org/resource/Brisbane"), dbo.populationTotal
@@ -35,21 +62,34 @@
3562
)
3663
print()
3764

38-
# EXAMPLE 3: doing RDFlib triple navigation using SPARQLStore as a Graph()
65+
# EXAMPLE Query 3: doing RDFlib triple navigation using SPARQLStore as a Graph()
3966
print("Triple navigation using SPARQLStore as a Graph():")
4067
graph = Graph("SPARQLStore", identifier="http://dbpedia.org")
41-
graph.open("http://dbpedia.org/sparql")
68+
graph.open("http://localhost:3030/db/sparql")
69+
# we are asking DBPedia for 3 skos:Concept instances
70+
count = 0
71+
72+
for s in graph.subjects(predicate=RDF.type, object=SKOS.Concept):
73+
count += 1
74+
print(f"\t- {s}")
75+
if count >= 3:
76+
break
77+
78+
79+
# EXAMPLE Query 4: doing RDFlib triple navigation using a Graph() with a SPARQLStore backend
80+
print("Triple navigation using a Graph() with a SPARQLStore backend:")
81+
st = SPARQLStore(query_endpoint="http://localhost:3030/db/sparql")
82+
graph = Graph(store=st)
4283
# we are asking DBPedia for 3 skos:Concept instances
4384
count = 0
44-
from rdflib.namespace import RDF, SKOS
4585

4686
for s in graph.subjects(predicate=RDF.type, object=SKOS.Concept):
4787
count += 1
4888
print(f"\t- {s}")
4989
if count >= 3:
5090
break
5191

52-
# EXAMPLE 4: using a SPARQL endpoint that requires Basic HTTP authentication
92+
# EXAMPLE Store 5: using a SPARQL endpoint that requires Basic HTTP authentication
5393
# NOTE: this example won't run since the endpoint isn't live (or real)
5494
sparql_store = SPARQLStore(
5595
query_endpoint="http://fake-sparql-endpoint.com/repository/x",

0 commit comments

Comments
 (0)