SPARQL AN RDF Query Language SPARQL l SPARQL

























- Slides: 25
SPARQL AN RDF Query Language
SPARQL l SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language l l SPARQL is the SQL for RDF Example: PREFIX abc: <http: //example. com/example. Onto#> SELECT ? capital ? country WHERE { ? x abc: cityname ? capital ; abc: is. Capital. Of ? y abc: countryname ? country ; abc: is. In. Continent abc: Africa. }
SPARQL l SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language l l SPARQL is the SQL for RDF Example: PREFIX abc: <http: //example. com/example. Onto#> SELECT ? capital ? country WHERE { ? x abc: cityname ? capital ; abc: is. Capital. Of ? y abc: countryname ? country ; abc: is. In. Continent abc: Africa. }
SPARQL History l l l Several RDF query languages were developed prior to SPARQL W 3 C RDF Data Access Working Group (DAWG) worked out SPARQL 2005 -2008 Became a W 3 C recommendation in January 2008 with key documents: – – – l http: //www. w 3. org/TR/rdf-sparql-query/ http: //www. w 3. org/TR/rdf-sparql-protocol/ http: //www. w 3. org/TR/rdf-sparql-XMLres/ Implementations for multiple programming languages available
SPARQL Query Forms l SELECT – Returns all, or a subset of, the variables bound in a query pattern match. l ASK – Returns a boolean indicating whether a query pattern matches or not. l DESCRIBE – Returns an RDF graph that describes the resources found. l 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> <http: //example/person/A> <http: //xmlns. com/foaf. 0. 1/name> “Jek” − Human-friendly alternative to RDF/XML Blank Node @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". | name | _: b foaf: name “Jeff". _: b foaf: mbox <mailto: jeff@example. org>. ==== | “Jek” | A "hello world" of queries | ”Yuan” | SELECT ? name | ”Jeff” | WHERE ------{ ? x foaf: name ? name }
Matching RDF Literals @prefix dt: <http: //example. org/datatype#>. @prefix ns: <http: //example. org/ns#>. @prefix xsd: <http: //www. w 3. org/2001/XMLSchema#>. ---------------------| v | ===== -----------------========== -----------------| V | | |http: //example. org/ns#x SELECT ? v WHERE { ? v ? p "cat" } | V | ========== -----------------========== |http: //example. org/ns#z | SELECT ? v WHERE { ? v ? p "cat“@en } |http: //example. org/ns#y | ---------------------------------SELECT ? v WHERE { ? v ? p 42 } : x ns: p "cat"@en. : y ns: p "42"^^xsd: integer. : z ns: p "abc"^^dt: special. Datatype. SELECT ? v WHERE { ? v ? p "abc dt: special. Datatype}
Filter @prefix dc: <http: //purl. org/dc/elements/1. 1/>. @prefix stock: <http: //example. org/stock#>. @prefix inv: <http: //example. org/inventory#>. stock: book 1 dc: title "SPARQL Query Language Tutorial". stock: book 1 dc: edition “First” ----------------------------------stock: book 1 inv: price 10. | book | title | stock: book 1 inv: quantity 3. ==================== stock: book 2 dc: title "SPARQL Query Language (2 nd ed)". | stock: book 1 | "SPARQL Query Language Tutorial" | stock: book 2 inv: price 20 ; inv: quantity 5. ----------------------------------stock: book 3 dc: title "Applying XQuery“; dc: edition “Second”. stock: book 3 inv: price 20 ; inv: quantity 8. 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 LIMIT 5 OFFSET 10
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. w 3. 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 3"> <dc: creator rdf: parse. Type="Resource"> <vcard: N rdf: parse. Type="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? l The DAWG did not reach a consensus on what describe should return l Possibilities include – – – All triples where the variable bindings are mentioned All triples where the bindings are the subject Something else l What is useful might depend on the application or the amount of data involved l 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. w 3. org/2001/vcard-rdf/3. 0#> CONSTRUCT { ? x vcard: N _: v vcard: given. Name ? gname. _: v vcard: family. Name ? 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. w 3. org/2001/vcard-rdf/3. 0#>. _: v 1 vcard: N _: x vcard: given. Name "Alice". _: x vcard: family. Name "Hacker". _: v 2 vcard: N _: z vcard: given. Name "Bob". _: z vcard: family. Name "Hacker".
On construct l Having a result form that produces an RDF graph is a good idea l It enables on to construct systems by using the output of one SPARQL query as the data over which another query works l This kind of capability was a powerful one for relational databases
Optional Pattern Matching Data: @prefix foaf: <http: //xmlns. com/foaf/0. 1/>. @prefix rdf: <http: //www. w 3. org/1999/02/22 -rdfsyntax-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 } } Query Result: name mbox „Alice“ <mailto: alice@example. com> „Alice“ <mailto: alice@work. example> „Bob“
RDF Dataset l RDF – – 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 l Use – – data stores may hold multiple RDF graphs: 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 l Having multiple RDF graphs in a single document/repository and naming them with URIs l Provides useful additional functionality built on top of the RDF Recommendations l 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 } }
Example (I) # Background graph @prefix dc: <http: //purl. org/dc/elements/1. 1/>. <http: //example. org/bob> dc: publisher "Bob". <http: //example. org/alice> dc: publisher "Alice". # Graph: http: //example. org/bob @prefix foaf: <http: //xmlns. com/foaf/0. 1/>. _: a foaf: name "Bob". _: a foaf: mbox <mailto: bob@oldcorp. example. org>. # Graph: http: //example. org/alice @prefix foaf: <http: //xmlns. com/foaf/0. 1/>. _: a foaf: name "Alice". _: a foaf: mbox <mailto: alice@work. example. org> .
Example(II) # Background graph @prefix foaf: <http: //xmlns. com/foaf/0. 1/>. _: x foaf: name "Bob". _: x foaf: mbox <mailto: bob@oldcorp. example. org>. _: y foaf: name "Alice". _: y foaf: mbox <mailto: alice@work. example. org>. # Graph: http: //example. org/bob @prefix foaf: <http: //xmlns. com/foaf/0. 1/>. _: a foaf: name "Bob". _: a foaf: mbox <mailto: bob@oldcorp. example. org>. # Graph: http: //example. org/alice @prefix foaf: <http: //xmlns. com/foaf/0. 1/>. _: a foaf: name "Alice". _: a foaf: mbox <mailto: alice@work. example. org>.
Querying the Dataset # Graph: http: //example. org/foaf/alice. Foaf @prefix foaf: <http: //xmlns. com/foaf/0. 1/>. @prefix rdf: <http: //www. w 3. org/1999/02/22 -rdf-syntax-ns#>. @prefix rdfs: <http: //www. w 3. org/2000/01/rdf-schema#>. _: a foaf: name "Alice". _: a foaf: mbox <mailto: alice@work. example>. _: a foaf: knows _: b rdfs: see. Also <http: //example. org/foaf/bob. Foaf> rdf: type foaf: Personal. Profile. Document. _: b foaf: name "Bob". _: b foaf: mbox <mailto: bob@work. example>. _: b foaf: age 32. # Graph: http: //example. org/foaf/bob. Foaf @prefix foaf: <http: //xmlns. com/foaf/0. 1/>. @prefix rdf: <http: //www. w 3. org/1999/02/22 -rdf-syntax-ns#>. @prefix rdfs: <http: //www. w 3. org/2000/01/rdf-schema#>. _: 1 foaf: mbox <mailto: bob@work. example>. _: 1 rdfs: see. Also <http: //example. org/foaf/bob. Foaf>. _: 1 foaf: age 35. <http: //example. org/foaf/bob. Foaf> rdf: type foaf: Personal. Profile. Document.
Accessing Graph Labels PREFIX foaf: <http: //xmlns. com/foaf/0. 1/> SELECT ? src ? bob. Age WHERE { GRAPH ? src { ? x foaf: mbox <mailto: bob@work. example>. ? x foaf: age ? bob. Age } } src bob. Age <http: //example. org/foaf/alice. Foaf> 32 <http: //example. org/foaf/bob. Foaf> 35
Restricting by Graph Label PREFIX foaf: <http: //xmlns. com/foaf/0. 1/> PREFIX data: <http: //example. org/foaf/> SELECT ? age WHERE { GRAPH data: bob. Foaf { ? x foaf: mbox <mailto: bob@work. example>. ? x foaf: age ? age } } age 35
Restricting via Query Pattern PREFIX data: <http: //example. org/foaf/> PREFIX foaf: <http: //xmlns. com/foaf/0. 1/> PREFIX rdf: <http: //www. w 3. org/1999/02/22 -rdf-syntax-ns#> PREFIX rdfs: <http: //www. w 3. org/2000/01/rdf-schema#> SELECT ? mbox ? age ? ppd WHERE { GRAPH data: alice. Foaf { ? alice foaf: mbox <mailto: alice@work. example> ; foaf: knows ? whom foaf: mbox ? mbox ; rdfs: see. Also ? ppd a foaf: Personal. Profile. Document. }. GRAPH ? ppd { ? w foaf: mbox ? mbox ; foaf: age ? age } } mbox age ppd <mailto: bob@work. example> 35 <http: //example. org/foaf/bob. Foaf>
More Features l RDF Dataset - Collection of RDF Graphs l use FROM http: //planetrdf. com/bloggers. rdf use FROM NAMED <http: //site 1. example. com/foo. rdf> l
Limitation of SPARQL l l 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. w 3. org/2009/sparql/wiki/Category: Features