250 likes | 275 Views
SPARQL AN RDF Query Language. SPARQL. SPARQL is a recursive acronym for S PARQL P rotocol A nd R df Q uery L anguage S PARQL is the SQL for RDF Example: PREFIX abc: <http://example.com/exampleOnto#> SELECT ?capital ?country WHERE { ?x abc:cityname ?capital ;
E N D
SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX abc: <http://example.com/exampleOnto#> SELECT ?capital ?country WHERE { ?x abc:cityname ?capital ; abc:isCapitalOf ?y . ?y abc:countryname ?country ; abc:isInContinent abc:Africa . }
SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX abc: <http://example.com/exampleOnto#> SELECT ?capital ?country WHERE { ?x abc:cityname ?capital ; abc:isCapitalOf ?y . ?y abc:countryname ?country ; abc:isInContinent abc:Africa . }
SPARQL History Several RDF query languages were developed prior to SPARQL W3C RDF Data Access Working Group (DAWG) worked out SPARQL 2005-2008 Became a W3C recommendation in January 2008 with key documents: http://www.w3.org/TR/rdf-sparql-query/ http://www.w3.org/TR/rdf-sparql-protocol/ http://www.w3.org/TR/rdf-sparql-XMLres/ Implementations for multiple programming languages available
SPARQL Query Forms • SELECT • Returns all, or a subset of, the variables bound in a query pattern match. • ASK • Returns a boolean indicating whether a query pattern matches or not. • DESCRIBE • Returns an RDF graph that describes the resources found. • CONSTRUCT • Returns an RDF graph constructed by substituting variables in a set of triple templates.
It’s Turtles all the way down Turtle (Terse RDF Triple Language ): − An RDF serialization − Triple representation of <Subject, Predicate, Object> − Human-friendly alternative to RDF/XML <http://example/person/A> <http://xmlns.com/foaf.0.1/name> “Jek” @prefix person: <http://example/person/> . @prefix foaf: <http://xmlns.com/foaf/0.1/> . Person:A foaf:name “Jek" . Person:A foaf:mbox <mailto:jek@example.net> . Person:B foaf:name “Yuan" . _:b foaf:name “Jeff" . _:b foaf:mbox <mailto:jeff@example.org> . ------------- | name | ======== | “Jek” | | ”Yuan” | | ”Jeff” | ------------- Blank Node A "hello world" of queries SELECT ?name WHERE { ?x foaf:name ?name }
Matching RDF Literals @prefix dt: <http://example.org/datatype#> . @prefix ns: <http://example.org/ns#> . @prefix : <http://example.org/ns#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :x ns:p "cat"@en . :y ns:p "42"^^xsd:integer . :z ns:p "abc"^^dt:specialDatatype . --------- | v | ===== ---------------------------------- | v | =================== |http://example.org/ns#x | ---------------------------------- ---------------------------------- | V | =================== |http://example.org/ns#z | ---------------------------------- ---------------------------------- | V | =================== |http://example.org/ns#y | ---------------------------------- SELECT ?v WHERE { ?v ?p "cat" } SELECT ?v WHERE { ?v ?p "cat“@en } SELECT ?v WHERE { ?v ?p 42 } SELECT ?v WHERE { ?v ?p "abc dt:specialDatatype}
Filter @prefix dc: <http://purl.org/dc/elements/1.1/> . @prefix stock: <http://example.org/stock#> . @prefix inv: <http://example.org/inventory#> . stock:book1 dc:title "SPARQL Query Language Tutorial" . stock:book1 dc:edition “First” stock:book1 inv:price 10 . stock:book1 inv:quantity 3 . stock:book2 dc:title "SPARQL Query Language (2nd ed)" . stock:book2 inv:price 20 ; inv:quantity 5 . stock:book3 dc:title "Applying XQuery“; dc:edition “Second” . stock:book3 inv:price 20 ; inv:quantity 8 . --------------------------------------------------------------------- | book | title | ======================================= | stock:book1 | "SPARQL Query Language Tutorial" | --------------------------------------------------------------------- PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX stock: <http://example.org/stock#> PREFIX inv: <http://example.org/inventory#> SELECT ?book ?title WHERE { ?book dc:title ?title . ?book inv:price ?price . FILTER ( ?price < 15 ) ?book inv:quantity ?num . FILTER ( ?num > 0 ) }
Other Solution Modifiers PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name WHERE { ?x foaf:name ?name } ORDER BY ?name PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT DISTINCT ?name WHERE { ?x foaf:name ?name } ORDER BY?name LIMIT5OFFSET10
ASK @prefix foaf: <http://xmlns.com/foaf/0.1/> . _:a foaf:name “Jek" . _:a foaf:homepage <http://work.example.org/Jek/> . _:b foaf:name "Bob" . _:b foaf:mbox <mailto:bob@work.example> . Yes PREFIX foaf: <http://xmlns.com/foaf/0.1/> ASK { ?x foaf:name “Jek" } <?xml version="1.0"?> <sparql xmlns="http://www.w3.org/2005/sparql-results#"> <head> </head> <results> <boolean>true</boolean> </results> </sparql>
DESCRIBE PREFIX books: <http://example.org/book/> PREFIX dc: <http://purl.org/dc/elements/1.1/> DESCRIBE ?book WHERE { ?book dc:title "Harry Potter and the Prisoner Of Azkaban" } <rdf:RDF> <rdf:Description rdf:about="http://example.org/book/book3"> <dc:creator rdf:parseType="Resource"> <vcard:N rdf:parseType="Resource"> <vcard:Given>Joanna</vcard:Given> <vcard:Family>Rowling</vcard:Family> </vcard:N> <vcard:FN>J.K. Rowling</vcard:FN> </dc:creator> <dc:title>Harry Potter and the Prisoner Of Azkaban</dc:title> </rdf:Description> </rdf:RDF>
Describes’s results? • The DAWG did not reach a consensus on what describe should return • Possibilities include • All triples where the variable bindings are mentioned • All triples where the bindings are the subject • Something else • What is useful might depend on the application or the amount of data involved • So it was left to the implementation
CONSTRUCT @prefix foaf: <http://xmlns.com/foaf/0.1/> . _:a foaf:givenname "Alice" . _:a foaf:family_name "Hacker" . _:b foaf:firstname "Bob" . _:b foaf:surname "Hacker" . PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> CONSTRUCT{ ?x vcard:N _:v . _:v vcard:givenName ?gname . _:v vcard:familyName ?fname } WHERE { { ?x foaf:firstname ?gname } UNION { ?x foaf:givenname ?gname } . { ?x foaf:surname ?fname }UNION { ?x foaf:family_name ?fname } .} @prefix vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> . _:v1 vcard:N _:x . _:x vcard:givenName "Alice" . _:x vcard:familyName "Hacker" . _:v2 vcard:N _:z . _:z vcard:givenName "Bob" . _:z vcard:familyName "Hacker" .
On construct • Having a result form that produces an RDF graph is a good idea • It enables on to construct systems by using the output of one SPARQL query as the data over which another query works • This kind of capability was a powerful one for relational databases
Data: @prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . _:a rdf:type foaf:Person . _:a foaf:name "Alice" . _:a foaf:mbox <mailto:alice@example.com> . _:a foaf:mbox <mailto:alice@work.example> . _:b rdf:type foaf:Person . _:b foaf:name "Bob" . Query: PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name ?mbox WHERE { ?x foaf:name ?name . OPTIONAL { ?x foaf:mbox ?mbox } } Optional Pattern Matching Query Result:
RDF Dataset • RDF data stores may hold multiple RDF graphs: • record information about each graph • queries that involve information from > one graph • RDF Dataset in SPARQL terminology • the background graph, which doen’t have a name, & 0 or more named graphs, identified by URI reference • Use cases: • (i) to have information in the background graph that includes provenance information about the named graphs (the application is not directly trusting the information in the named graphs ) • (ii) to include the information in the named graphs in the background graph as well
RDF Named graphs • Having multiple RDF graphs in a single document/repository and naming them with URIs • Provides useful additional functionality built on top of the RDF Recommendations • SPARQL queries can involve several graphs, a background one and multiple named ones, e.g.: SELECT ?who ?g ?mbox FROM <http://example.org/dft.ttl> FROM NAMED <http://example.org/alice> FROM NAMED <http://example.org/bob> WHERE { ?g dc:publisher ?who . GRAPH ?g { ?x foaf:mbox ?mbox } }
More Features RDF Dataset - Collection of RDF Graphs use FROMhttp://planetrdf.com/bloggers.rdf use FROM NAMED <http://site1.example.com/foo.rdf>
Limitation of SPARQL SPARQL has many limitations, including No Insert, Update, Delete queries No aggregation functions These and many other features are being evaluated for inclusion in a future recommendation, see http://www.w3.org/2009/sparql/wiki/Category:Features