An Introduction to the SPARQL RDF Query
Language
Gregory Todd Williams
Overview• Introduction• What is SPARQL• SPARQL Tools
• Basics• RDF Nodes and Triple Patterns• Basic Graph Patterns• Filtering
• More• Group Graph Patterns• Optional Matching• Unions• Named Graphs
Overview (cont.)
• Results• Modifiers• Ordering, Limiting, Pagination
• Forms• Select• Ask• Construct• Describe
• Issues and Ongoing Work• References
What is SPARQL?
• Query Language for RDF
• Like SQL, but for graphs
• Matches graph patterns against RDF graph
SPARQL Tools
• From your favorite programming language
• In Java, Jena's SPARQL implementation is ARQ
• Many other implementations in other languages (Python, Perl, C, ...)
• From the web
• Many data-specific and general-purpose "endpoints" accessible via HTTP
RDF Review
RDF Nodes
• Resources (URIs) <http://www.rpi.edu/>
• Literals (Strings)
• Plain "Semantic Web"
• Plain with Language "Semantic Web"@en
• Datatyped "8"^^xsd:integer
• Blank Nodes
RDF Statements
• Triple consisting of a subject, predicate, object:
• (URI | Blank) (URI) (URI | Blank | Literal)
SPARQL: Basic SyntaxPREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?thing ?nameFROM <http://example.com/data.rdf>WHERE {
?thing foaf:name ?name}ORDER BY ?name
• Namespace Prefixes
• What variables to select
• Optional RDF data location
• A pattern to match
• Optional Solution modifiers (ordering, pagination)
User
SPARQL
Endpoint
Disk
What Data to Query?• Just query without specifying data.
PREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?personWHERE { ?person a foaf:Person }
What Data to Query?• Specify data URI(s) using "FROM <...>"
User
SPARQL
Endpoint
Internet
PREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?personFROM <http://www.cs.rpi.edu/~hendler/foaf.rdf>FROM <http://w3.org/People/Berners-Lee/card>WHERE { ?person a foaf:Person }
Querying Statements: Triple Patterns
• Simplest query would be a triple with a variable in place of one or more nodes:
• Find Greg's name:
<http://example.com/greg> foaf:name ?name
• Find all people and their names:
?person foaf:name ?name
Querying Graphs: Graph Patterns
• Multiple statements allow matching a subgraph:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?homepageWHERE { ?person a foaf:Person ; foaf:name "Jim Hendler" ; foaf:homepage ?homepage .}
Select the ?homepage of anyone named "Jim Hendler":
Querying Graphs: Filtering
• Beyond pattern matching, FILTER allows constraints on the values of nodes in a pattern:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX ex: <http://example.com/>SELECT ?personWHERE { ?person a foaf:Person ; ex:age ?age . FILTER( ?age > 21 ) .}
Querying Graphs: Filtering
• Filters can use:
• Logical operations: <, <=, >, >=, =, !=, ...
• Built-in functions: STR, REGEX, LANG, ...
• Implementation specific functions
Querying Graphs: Filtering
PREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?homepageWHERE { ?person a foaf:Person ; foaf:homepage ?homepage . FILTER( REGEX(STR(?homepage), "http://(\\w+\\.)+edu/.+") ) .}
Querying Graphs: Optional Matching
• To optionally match subgraphs if they are present, use an OPTIONAL pattern:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?name ?homepageWHERE { ?person a foaf:Person ; foaf:name ?name . OPTIONAL { ?person foaf:homepage ?homepage . }}
Querying Graphs: Combining Graph Patterns
• Graph patterns may be joined and/or restricted in several ways including:
• Take the UNION of two patterns
• Restrict matching of the pattern to a specific named graph
Querying Graphs
• If we want all images of people, we might try:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?person ?img ?depictionWHERE { ?person a foaf:Person . OPTIONAL { ?person foaf:img ?img } . OPTIONAL { ?person foaf:depiction ?depiction }}
Querying Graphs: UNION
• Two subgraphs may be matched independently with a UNION pattern:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?person ?imgWHERE { ?person a foaf:Person .
{ ?person foaf:img ?img } UNION { ?person foaf:depiction ?img }}
Querying Graphs: GRAPH
PREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?nameFROM <http://www.w3.org/People/Berners-Lee/card>FROM <http://www.cs.rpi.edu/~hendler/foaf.rdf>WHERE { ?person foaf:name ?name .}
• Find all people's names from two FOAF files:
• ... but which file did each result come from?
Querying Graphs: GRAPH
PREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?name ?fileFROM NAMED <http://www.w3.org/People/Berners-Lee/card>FROM NAMED <http://www.cs.rpi.edu/~hendler/foaf.rdf>WHERE { GRAPH ?file { ?person foaf:name ?name .
}}
Solution Modifiers
• How can a set of results be modified?
• Sorting
• Limiting
• Pagination
Solution Modifiers: Sorting
PREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?nameWHERE { ?person a foaf:Person ; foaf:name ?name .}ORDER BY ?name
Solution Modifiers: Sorting
PREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?nameWHERE { ?person a foaf:Person ; foaf:name ?name .}ORDER BY DESC(?name)
Solution Modifiers: Limiting
PREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?nameWHERE { ?person a foaf:Person ; foaf:name ?name .}ORDER BY ?nameLIMIT 5
Solution Modifiers: Pagination
PREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?nameWHERE { ?person a foaf:Person ; foaf:name ?name .}ORDER BY ?nameLIMIT 5OFFSET 5
Result Forms
• We've already seen how to get tabular data with SELECT.
• There are three other result forms:
• ASK: Does such a subgraph exist?
• CONSTRUCT: Take the results and create a new RDF graph with them.
• DESCRIBE: Retrieve an RDF graph describing a set of RDF Nodes. (Intentionally a bit vague.)
Result Forms: ASK
PREFIX foaf: <http://xmlns.com/foaf/0.1/>ASK { ?person a foaf:Person ; foaf:name "Jim Hendler" .}
Result Forms: CONSTRUCT
PREFIX foaf: <http://xmlns.com/foaf/0.1/>CONSTRUCT { ?person foaf:homepage ?homepage ; foaf:workplaceHomepage <http://www.w3.org/> .}WHERE { ?person a foaf:Person ; foaf:homepage ?homepage . FILTER REGEX( STR(?homepage), "w3.org/.+$") .}
Result Forms: DESCRIBE
PREFIX foaf: <http://xmlns.com/foaf/0.1/>DESCRIBE ?personWHERE { ?person a foaf:Person ; foaf:name "Jim Hendler" .}
Issues
• Inferred triples (need for preprocessing?)
• No aggregates
• Difficult to query rdf:List structures
• Difficult to make queries about missing data
Ongoing Work
• Scaling
• Inferencing (RDFS/OWL/Rules)
• Querying paths
• Querying provenance (time, source, author)
• SPARQL ⇔ RDBMS mapping
Additional Resources
• SPARQL specification
• General-purpose SPARQL endpoints:
• http://sparql.org/sparql.html
• http://librdf.org/query/
• Developers Guide to Semantic Web Toolkits for different Programming Languages
• D2RQ (RDBMS mapping)
<http://tw.rpi.edu/2007/11/sparql-intro-gtw.pdf>