SPARQL#
This document contains a short introduction to SPARQL using rudof.
Preliminaries#
The library is available as pyrudof.
!pip install pyrudof
Requirement already satisfied: pyrudof in /opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages (0.2.9)
The main entry point if a class called Rudof through which most of the functionality is provided.
from pyrudof import Rudof, RudofConfig
In order to initialize that class, it is possible to pass a RudofConfig instance which contains configuration parameters for customization.
rudof = Rudof(RudofConfig())
SPARQL queries with local RDF data#
As SPARQL is an RDF query language, we first need some RDF data.
rudof can run queries against local RDF data or RDF that is available through some SPARQL endpoint.
Let’s start with some local RDF data.
rdf = """
prefix : <http://example.org/>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
:alice a :Person ;
:name "Alice" ;
:birthDate "2005-03-01"^^xsd:date ;
:worksFor :acme .
:bob a :Person ;
:name "Robert Smith" ;
:birthDate "2003-01-02"^^xsd:date ;
:worksFor :acme .
:acme a :Company ;
:name "Acme Inc." .
"""
rudof.read_data(rdf)
We can run SPARQL queries as follows:
query = """
PREFIX : <http://example.org/>
SELECT ?person ?name ?date ?company WHERE {
?person a :Person ;
:name ?name ;
:birthDate ?date ;
:worksFor ?c .
?c :name ?company .
}
"""
rudof.read_query(query)
rudof.run_query()
results = rudof.serialize_query_results()
Show the results:
print(results)
╭───┬─────────┬────────────────┬───────────────────────────────────────────────────────┬─────────────╮
│ │ ?person │ ?name │ ?date │ ?company │
├───┼─────────┼────────────────┼───────────────────────────────────────────────────────┼─────────────┤
│ 1 │ :alice │ "Alice" │ "2005-03-01"^^<http://www.w3.org/2001/XMLSchema#date> │ "Acme Inc." │
├───┼─────────┼────────────────┼───────────────────────────────────────────────────────┼─────────────┤
│ 2 │ :bob │ "Robert Smith" │ "2003-01-02"^^<http://www.w3.org/2001/XMLSchema#date> │ "Acme Inc." │
╰───┴─────────┴────────────────┴───────────────────────────────────────────────────────┴─────────────╯
rudof.reset_all()
SPARQL queries with an endpoint#
It is also possible to run SPARQL queries against SPARQL endpoints.
SPARQL endpoints are usually identified by an IRI.
rudof contains a list of popular SPARQL endpoints which can also be identified by name, like wikidata, whose IRI is: https://query.wikidata.org/sparql
query = """
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT ?person ?occupation WHERE {
?p wdt:P31 wd:Q5 ;
wdt:P106 ?o ;
rdfs:label ?person ;
wdt:P19 wd:Q14317 .
?o rdfs:label ?occupation
FILTER (lang(?person) = "en" && lang(?occupation) = "en")
}
LIMIT 10
"""
rudof.read_data(endpoint="https://query.wikidata.org/sparql")
rudof.read_query(query)
rudof.run_query()
results = rudof.serialize_query_results()
print(results)
╭────┬──────────────────────────────────┬────────────────────╮
│ │ ?person │ ?occupation │
├────┼──────────────────────────────────┼────────────────────┤
│ 1 │ "Manuel González Llana"en │ "writer"en │
├────┼──────────────────────────────────┼────────────────────┤
│ 2 │ "Mercedes Arriaga Florez"en │ "writer"en │
├────┼──────────────────────────────────┼────────────────────┤
│ 3 │ "Dorotea Bárcena"en │ "actor"en │
├────┼──────────────────────────────────┼────────────────────┤
│ 4 │ "Maribel Álvarez"en │ "writer"en │
├────┼──────────────────────────────────┼────────────────────┤
│ 5 │ "Constantino García"en │ "writer"en │
├────┼──────────────────────────────────┼────────────────────┤
│ 6 │ "Ángel Mario Carreño"en │ "politician"en │
├────┼──────────────────────────────────┼────────────────────┤
│ 7 │ "Ignacio Herrero de Collantes"en │ "businessperson"en │
├────┼──────────────────────────────────┼────────────────────┤
│ 8 │ "José María Casielles Aguadé"en │ "politician"en │
├────┼──────────────────────────────────┼────────────────────┤
│ 9 │ "Manuel González Llana"en │ "politician"en │
├────┼──────────────────────────────────┼────────────────────┤
│ 10 │ "Ignacio Herrero de Collantes"en │ "politician"en │
╰────┴──────────────────────────────────┴────────────────────╯
rudof.reset_all()
Federated queries#
TBD
CONSTRUCT queries#
from pyrudof import QueryResultFormat
It is also possible to run SPARQL CONSTRUCT queries which can be used to return RDF results.
For example:
query = """
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX : <http://example.org/>
CONSTRUCT {
?p a :Person ;
:name ?person ;
:occupation ?occupation
} WHERE {
?p wdt:P31 wd:Q5 ;
wdt:P106 ?o ;
rdfs:label ?person ;
wdt:P19 wd:Q14317 .
?o rdfs:label ?occupation
FILTER (lang(?person) = "en" && lang(?occupation) = "en")
}
LIMIT 10
"""
We indicate rudof that we will use Wikidata’s endpoint:
rudof.read_data(endpoint="https://query.wikidata.org/sparql")
rudof.read_query(query)
rudof.run_query()
result = rudof.serialize_query_results(QueryResultFormat.Turtle)
print(result)
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sesame: <http://www.openrdf.org/schema/sesame#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix fn: <http://www.w3.org/2005/xpath-functions#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix hint: <http://www.bigdata.com/queryHints#> .
@prefix bd: <http://www.bigdata.com/rdf#> .
@prefix bds: <http://www.bigdata.com/rdf/search#> .
@prefix psn: <http://www.wikidata.org/prop/statement/value-normalized/> .
@prefix pqn: <http://www.wikidata.org/prop/qualifier/value-normalized/> .
@prefix prn: <http://www.wikidata.org/prop/reference/value-normalized/> .
@prefix mwapi: <https://www.mediawiki.org/ontology#API/> .
@prefix gas: <http://www.bigdata.com/rdf/gas#> .
@prefix wdsubgraph: <https://query.wikidata.org/subgraph/> .
@prefix wdt: <http://www.wikidata.org/prop/direct/> .
@prefix wdtn: <http://www.wikidata.org/prop/direct-normalized/> .
@prefix psv: <http://www.wikidata.org/prop/statement/value/> .
@prefix ps: <http://www.wikidata.org/prop/statement/> .
@prefix pqv: <http://www.wikidata.org/prop/qualifier/value/> .
@prefix pq: <http://www.wikidata.org/prop/qualifier/> .
@prefix prv: <http://www.wikidata.org/prop/reference/value/> .
@prefix pr: <http://www.wikidata.org/prop/reference/> .
@prefix wdno: <http://www.wikidata.org/prop/novalue/> .
@prefix p: <http://www.wikidata.org/prop/> .
@prefix wikibase: <http://wikiba.se/ontology#> .
@prefix wd: <http://www.wikidata.org/entity/> .
@prefix wdata: <http://www.wikidata.org/wiki/Special:EntityData/> .
@prefix wds: <http://www.wikidata.org/entity/statement/> .
@prefix wdv: <http://www.wikidata.org/value/> .
@prefix wdref: <http://www.wikidata.org/reference/> .
@prefix schema: <http://schema.org/> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix geo: <http://www.opengis.net/ont/geosparql#> .
@prefix geof: <http://www.opengis.net/def/geosparql/function/> .
@prefix mediawiki: <https://www.mediawiki.org/ontology#> .
@prefix ontolex: <http://www.w3.org/ns/lemon/ontolex#> .
@prefix dct: <http://purl.org/dc/terms/> .
@prefix wikibaseqs: <http://wikiba.se/queryService#> .
wd:Q5973925 a <http://example.org/Person> ;
<http://example.org/name> "Leticia Sánchez Ruiz"@en ;
<http://example.org/occupation> "writer"@en .
wd:Q5983811 a <http://example.org/Person> ;
<http://example.org/name> "Luis López-Doriga"@en ;
<http://example.org/occupation> "politician"@en .
wd:Q5983941 a <http://example.org/Person> ;
<http://example.org/name> "Luis Menéndez-Pidal y Álvarez"@en ;
<http://example.org/occupation> "architect"@en .
wd:Q5984337 a <http://example.org/Person> ;
<http://example.org/name> "Luis Sela Sampil"@en ;
<http://example.org/occupation> "jurist"@en .
wd:Q5983811 <http://example.org/occupation> "Catholic priest"@en .
wd:Q5984068 a <http://example.org/Person> ;
<http://example.org/name> "Luis Noval Ferrao"@en ;
<http://example.org/occupation> "military personnel"@en .
wd:Q5983811 <http://example.org/occupation> "theologian"@en .
wd:Q5992314 a <http://example.org/Person> ;
<http://example.org/name> "Manuel Atienza Rodríguez"@en ;
<http://example.org/occupation> "jurist"@en .
wd:Q10514 a <http://example.org/Person> ;
<http://example.org/name> "Fernando Alonso"@en ;
<http://example.org/occupation> "vegetarian"@en , "Formula One driver"@en .
References#
Resources for learning SPARQL#
Learning SPARQL book and blog by Bob Ducharne.
SPARQL tutorial from Wikidata
Lists of SPARQL endpoints#
SPARQL endpoints collected in Wikidata
SPARQL endpoints collected at W3C.