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.13/x64/lib/python3.11/site-packages (0.1.135)

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_str(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 .
}
"""

results = rudof.run_query_str(query)

Show the results:

print(results.show())
╭───┬─────────┬────────────────┬────────────────────────┬─────────────╮
│   │ ?person │ ?name          │ ?date                  │ ?company    │
├───┼─────────┼────────────────┼────────────────────────┼─────────────┤
│ 1 │ :alice  │ "Alice"        │ "2005-03-01"^^xsd:date │ "Acme Inc." │
├───┼─────────┼────────────────┼────────────────────────┼─────────────┤
│ 2 │ :bob    │ "Robert Smith" │ "2003-01-02"^^xsd: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 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.use_endpoint("https://query.wikidata.org/sparql")
results = rudof.run_query_str(query)
print(results.show())
╭────┬──────────────────────────────────────────┬─────────────────────────────────╮
│    │ ?person                                  │ ?occupation                     │
├────┼──────────────────────────────────────────┼─────────────────────────────────┤
│ 1  │ "Luis López-Doriga"en                    │ "politician"en                  │
├────┼──────────────────────────────────────────┼─────────────────────────────────┤
│ 2  │ "Leticia Sánchez Ruiz"en                 │ "writer"en                      │
├────┼──────────────────────────────────────────┼─────────────────────────────────┤
│ 3  │ "Javier Paredes"en                       │ "association football player"en │
├────┼──────────────────────────────────────────┼─────────────────────────────────┤
│ 4  │ "Fernando Alonso"en                      │ "Formula One driver"en          │
├────┼──────────────────────────────────────────┼─────────────────────────────────┤
│ 5  │ "Fernando Alonso"en                      │ "vegetarian"en                  │
├────┼──────────────────────────────────────────┼─────────────────────────────────┤
│ 6  │ "Luis López-Doriga"en                    │ "Catholic priest"en             │
├────┼──────────────────────────────────────────┼─────────────────────────────────┤
│ 7  │ "Luis López-Doriga"en                    │ "theologian"en                  │
├────┼──────────────────────────────────────────┼─────────────────────────────────┤
│ 8  │ "Manuel del Fresno y Pérez del Villar"en │ "composer"en                    │
├────┼──────────────────────────────────────────┼─────────────────────────────────┤
│ 9  │ "Luis José de Ávila"en                   │ "journalist"en                  │
├────┼──────────────────────────────────────────┼─────────────────────────────────┤
│ 10 │ "Ramiro I of Asturias"en                 │ "ruler"en                       │
╰────┴──────────────────────────────────────────┴─────────────────────────────────╯
rudof.use_endpoint("https://query.wikidata.org/sparql")
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 :    <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.use_endpoint("https://query.wikidata.org/sparql")
result = rudof.run_query_construct_str(query, QueryResultFormat.Turtle)
print(result)
@prefix wd: <http://www.wikidata.org/entity/> .
@prefix wdt: <http://www.wikidata.org/prop/direct/> .
@prefix : <http://example.org/> .
@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 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 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 :Person ;
	:name "Leticia Sánchez Ruiz"@en ;
	:occupation "writer"@en .

wd:Q5983811 a :Person ;
	:name "Luis López-Doriga"@en ;
	:occupation "politician"@en .

wd:Q5983941 a :Person ;
	:name "Luis Menéndez-Pidal y Álvarez"@en ;
	:occupation "architect"@en .

wd:Q5983724 a :Person ;
	:name "Luis José de Ávila"@en ;
	:occupation "journalist"@en .

wd:Q5984337 a :Person ;
	:name "Luis Sela Sampil"@en ;
	:occupation "jurist"@en .

wd:Q5983811 :occupation "Catholic priest"@en .

wd:Q5984068 a :Person ;
	:name "Luis Noval Ferrao"@en ;
	:occupation "military personnel"@en .

wd:Q5983811 :occupation "theologian"@en .

wd:Q5992314 a :Person ;
	:name "Manuel Atienza Rodríguez"@en ;
	:occupation "jurist"@en , "university teacher"@en .

References#

Resources for learning SPARQL#

Lists of SPARQL endpoints#