This page is dedicated to notes re: SPARQL which include infrastructure notes and may be particularly uninteresting to people other than myself.
SPARQL Templates
General query template:
PREFIX ex: <http://example.com>
SELECT ?o
WHERE
{ s? ex:subject o? .}
Querying specific file:
PREFIX ex: <http://example.com>
SELECT ?o FROM <filepath/filename.ttl> # you can use repeated FROM to query multiple files/sources.
WHERE
{ s? ex:subject o? .}
Use FROM NAMED instead of FROM if you don’t want the file’s triples to be merged into the default graph (which is what will happen with all the plain FROM ones). Then use GRAPH to refer to that graph (as a URI).
SPARQL Querying Notes: Syntax
This is all basic syntax and practice notes, ranging from the most basic to the more complex.
Querying with Multiple Predicates
When you want data which might be using multiple predicates put inside a parenthetical with a pipe for “OR.” For example, unsure whether the labels are dc:title or rdfs:label?
SELECT ?s ?title
WHERE { ?s (dc:title | rdfs:label) ?title . }
Querying with Multiple values
If you need to search based on a list of multiple values, use the following syntax:
SELECT ?human ?humanLabel WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
VALUES ?NAF { "n2007041690" "n50034519" "n2017046461" "n84162081" "n85234663" "no2009045604"}
?human wdt:P31 wd:Q5;
wdt:P244 ?NAF }
LIMIT 200
Note, the values list could also contain a bunch of wikidata entities, etc.
UNION
UNION can used to repeat as many graph patterns as you want… even really non-productive ones. This is a really basic one:
SELECT ?first ?last ?instrument
WHERE
{
?person ab:firstName ?first ;
ab:lastName ?last ;
ab:instrument ?instrument .
{ ?person ab:instrument "sax" . }
UNION
{ ?person ab:instrument "trumpet" . }
}
OPTIONAL
Wrap section in:
OPTIONAL { }
Extending OPTIONAL: You can use repeating OPTIONAL to bind the first instance of a variable which occurs and ignore the rest. e.g. you can get someone’s preferred name and, if doesn’t exist, their legal first name by using the following.
OPTIONAL { ?s foaf:nick ?first . }
OPTIONAL { ?s foaf:firstName ?first . }
Repeating/Reversing Query
Use Regex or XPath-type syntax to query recursively/deeply.
SELECT ?s
WHERE { ?s c:cites+ :paperA . }
will find all papers that CITE papers that CITE paper A as well as the ones that cite paper A.
SELECT ?s
WHERE { ?s c:cites/c:cites/c:cites :paperA . }
will only identify the ones at 3 removes of citation from paper A.
Use ^ to invert statement. Most useful when it’s something like:
SELECT ?s
WHERE
{
?s c:cites/^c:cites :paperF .
FILTER(?s != :paperF)
}
Select all papers that cite or are cited by paper F that are not paper F.
OFFSET as paired with LIMIT
LIMIT is easy to remember, but use OFFSET along with it to iterate through results better.
(note: you’ll need to ORDER BY as well or you’ll get weird results.)
Filters
Obviously you can use operators like > < = != >= <= …
lang - your basic language filter
FILTER ( lang(?field) = "en" )
isURI(): You can filter based on whether something is a URI or, more likely !isURI() - to check data quality or determine what you’re getting.
bound(): Boolean trick which works in SPARQL 1.0 to determine if a value is bound for a particular person or not. Example below (example uses Not Bound !bound())
SELECT ?first ?last
WHERE
{
?s foaf:givenmame ?first ;
foaf:familyName ?last .
OPTIONAL { ?s foaf:phone ?phone . }
FILTER (!bound(?phone))
}
# returns all cases where they don't have a phone number in their file
IN / NOT IN - SPARQL 1.1 test whether something is in (or not in) a list.
SELECT ?s ?cost ?location
WHERE
{
?s dm:location ?location ;
dm:cost ?cost .
FILTER (?location IN (db:Montreal, db:Lisbon)) .
}
FILTER NOT EXISTS - SPARQL 1.1 simpler testing for doesn’t exist
SELECT ?first ?last
WHERE
{
?s foaf:givenmame ?first ;
foaf:familyName ?last .
FILTER NOT EXISTS { ?s foaf:phone ?phone }
}
MINUS - Similar to testing for not exists but not quite the same
Use MINUS { } to remove results with certain things from the search results. However, this will only remove those bindings if they’ve been bound to something. Similar results but not the same and should be tested in comparsion with NOT EXISTS to be sure you’re using the right one.
SPARQL Querying Notes: Datatypes
Useful Namespaces
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix dc: <http://purl.org/dc/elements/1.1/>
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
Integers / Booleans / Decimals
In Turtle, integers, booleans, and decimals NOT expressed as strings will parse as such without needing to declare a datatype. If strings, they require datatypes. Dates do not auto-parse.
Strings as Strings and Filters
Strings can be queried IFF they’re stored as strings in the RDF and you can exactly match them. Otherwise you’ll need to add filters.
e.g. on a query for email addresses when you’re not sure what the predicate for emails is:
SELECT * WHERE {
?s ?p ?o.
FILTER (regex(?o, "yahoo","i"))
}
ARQ / Local SPARQL Querying
Basic ARQ syntax (order of variables doesn’t matter, just be sure to flag properly):
arq --data datafile.ttl --query queryfile.rq
ARQ syntax with multiple datasources (in this case, from Learning SPARQL 2, the second datasource is FOAF schema):
arq --data ex050.ttl --data index.rdf --query ex052.rq
ARQ flags of possible interest:
--time # time the operation
--explain # explain and log query execution
-v # verbose output
--debug # output debug information
--strict # run in strict SPARQL mode, no extensions
--optimize=off # turn off optimizer
Wikidata Queries
Select all articles whose authors have published datasets with ARDA
SELECT DISTINCT ?article ?articleLabel WHERE {
?dataset wdt:P126 wd:Q4810029;
wdt:P50 ?author.
?article wdt:P31 wd:Q13442814;
wdt:P50 ?author.
?author wdt:P31 wd:Q5.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Select all journals whose authors have published datasets with ARDA
this version has no instance-of so it could be any kind of article
SELECT DISTINCT ?publicationLabel (COUNT(?publication) AS ?count ) WHERE {
?dataset wdt:P126 wd:Q4810029;
wdt:P50 ?author.
?author wdt:P31 wd:Q5.
?article wdt:P50 ?author;
wdt:P1433 ?publication.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
GROUP BY ?publicationLabel
ORDER BY DESC(?count)
this has instance of kinds of article so maybe should be experimented with or create values list of kinds of articles
SELECT DISTINCT ?publicationLabel (COUNT(?publication) AS ?count ) WHERE {
?dataset wdt:P126 wd:Q4810029;
wdt:P50 ?author.
?author wdt:P31 wd:Q5.
?article wdt:P31 wd:Q13442814;
wdt:P50 ?author;
wdt:P1433 ?publication.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
GROUP BY ?publicationLabel
ORDER BY DESC(?count)
Working on query to get more info about educational institutions of librarians of congress
(Query works but there’s no data for them, tested on Presidents things and it worked – does need to be fixed though to avoid carrying over the same data if there’s no data on the next entity https://query.wikidata.org/#SELECT%20%3FLibrarian_of_CongressLabel%20%3FinstitutionLabel%20%3FdegreeLabel%20%3FfieldLabel%20%3Fend_timeLabel%20WHERE%20%7B%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%20%20%3FLibrarian_of_Congress%20wdt%3AP39%20wd%3AQ11696%3B%0A%20%20%20%20wdt%3AP69%20%3Finstitution%3B%0A%20%20%20%20p%3AP69%20%3Feducated_statement.%0A%20%20OPTIONAL%20%7B%20%3Feducated_statement%20pq%3AP512%20%3Fdegree.%20%7D%0A%20%20OPTIONAL%20%7B%20%3Feducated_statement%20pq%3AP812%20%3Ffield.%20%7D%0A%20%20OPTIONAL%20%7B%20%3Feducated_statement%20pq%3AP582%20%3Fend_time.%20%7D%0A%7D%0ALIMIT%2010000 )
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?Librarian_of_Congress wdt:P39 wd:Q6542448;
wdt:P69 ?institution;
p:P69 ?educated_statement.
OPTIONAL { ?educated_statement pq:P512 ?degree }
OPTIONAL { ?educated_statement pq:P812 ?field }
OPTIONAL { ?educated_statement pq:P582 ?end_time }
}
LIMIT 100
Infrastructure
Infrastructure: Apache Jena / ARQ
Installed Jena, which includes ARQ, on my computers in order to practice running queries in the command line.
Infrastructure: Fuseki
Installed Fuseki on my computers with the idea of doing some minor dataset-related work. Attempting to upload a 20MB DBpedia TTL file on the Linux Mint computer led to a session timeout (2021-07-28), which may mean I need to use the Kalin to get a more dedicated space to have Fuseki running. What are my shared computing options?




