An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview...

34
An Introduction to the SPARQL RDF Query Language Gregory Todd Williams

Transcript of An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview...

Page 1: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

An Introduction to the SPARQL RDF Query

Language

Gregory Todd Williams

Page 2: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

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

Page 3: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

Overview (cont.)

• Results• Modifiers• Ordering, Limiting, Pagination

• Forms• Select• Ask• Construct• Describe

• Issues and Ongoing Work• References

Page 4: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

What is SPARQL?

• Query Language for RDF

• Like SQL, but for graphs

• Matches graph patterns against RDF graph

Page 5: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

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

Page 6: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

RDF Review

Page 7: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

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

Page 8: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

RDF Statements

• Triple consisting of a subject, predicate, object:

• (URI | Blank) (URI) (URI | Blank | Literal)

Page 9: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

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)

Page 10: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

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 }

Page 11: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

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 }

Page 12: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

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

Page 13: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

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":

Page 14: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

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 ) .}

Page 15: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

Querying Graphs: Filtering

• Filters can use:

• Logical operations: <, <=, >, >=, =, !=, ...

• Built-in functions: STR, REGEX, LANG, ...

• Implementation specific functions

Page 17: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

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 . }}

Page 18: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

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

Page 19: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

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 }}

Page 20: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

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 }}

Page 21: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

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?

Page 22: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

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 .

}}

Page 23: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

Solution Modifiers

• How can a set of results be modified?

• Sorting

• Limiting

• Pagination

Page 28: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

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.)

Page 29: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

Result Forms: ASK

PREFIX foaf: <http://xmlns.com/foaf/0.1/>ASK { ?person a foaf:Person ; foaf:name "Jim Hendler" .}

Page 31: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

Result Forms: DESCRIBE

PREFIX foaf: <http://xmlns.com/foaf/0.1/>DESCRIBE ?personWHERE { ?person a foaf:Person ; foaf:name "Jim Hendler" .}

Page 32: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

Issues

• Inferred triples (need for preprocessing?)

• No aggregates

• Difficult to query rdf:List structures

• Difficult to make queries about missing data

Page 33: An Introduction to the SPARQL RDF Query Languagetw.rpi.edu/2007/11/sparql-intro-gtw.pdf · Overview • Introduction • What is SPARQL • SPARQL Tools • Basics • RDF Nodes and

Ongoing Work

• Scaling

• Inferencing (RDFS/OWL/Rules)

• Querying paths

• Querying provenance (time, source, author)

• SPARQL ⇔ RDBMS mapping