SPARQL1
Query Languages: SQL(SQL Query Language)       A language for querying collections of tuples:               SELECT SALARY...
Query Languages: XQuery(XML Query)       A language for querying trees of XDM nodes:            for $e in document(my_emp...
Why an RDF Query Language?Different XML Representations       XML at a lower level of abstraction than RDF       There a...
Two basic families of SQL-like langages forRDF(S)       RDQL           Implementations: Jena, Sesame, RDFStore, ...    ...
Introduction to RDQL       RDF Data Query Language       JDBC/ODBC friendly       Simple:          SELECT              ...
Example7
Example       q1 contains a query:SELECT ?xWHERE (?x, <http://www.w3.org/2001/vcard-rdf/3.0#FN>, "John Smith")       For...
Example       Return all the resources that have property FN and the        associated values:SELECT ?x, ?fnameWHERE (?x,...
Example    Return the first name of Jones:SELECT ?givenNameWHERE (?y, <http://www.w3.org/2001/vcard-rdf/3.0#Family>, "Jon...
URI Prefixes : USING    RDQL has a syntactic convenience that allows prefix strings     to be defined in the USING clause...
Filters    RDQL has a syntactic convenience that allows prefix strings     to be defined in the USING clause :SELECT ?res...
Limitations    Does not take into account semantics of RDF(S)    For example:        ex:human rdfs:subClassOf ex:animal ...
Why an RDF Query Language?Different XML Representations    XML at a lower level of abstraction than RDF    There are var...
SPARQL   SPARQL is RDF Query Language   It provides facilities to:     extract information in the form of URIs, blank n...
SPARQL Basic Queries    SPARQL is based on matching graph patterns    The simplest graph pattern is the triple pattern :...
Basic graph pattern   Set of Triple Patterns     Triple Pattern – similar to an RDF Triple (subject, predicate, object),...
ExamplesPREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>SELEC...
Examples (2)    Get all instances of a particular class (e.g. course) :PREFIX uni: <http://www.mydomain.org/uni-ns#>SELEC...
Multiple Matches                              Data    @prefix foaf: <http://xmlns.com/foaf/0.1/> .                        ...
Using select-from-where    As in SQL, SPARQL queries have a SELECT-FROM-WHERE structure:      SELECT specifies the proje...
Implicit Join    Retrieve all lecturers and their phone numbers:             SELECT ?x ?y             WHERE             {...
Implicit join (2)    The previous query is equivalent to writing:         SELECT ?x ?y         WHERE         {           ...
Explicit Join    Retrieve the name of all courses taught by the lecturer with ID 949352     SELECT ?n     WHERE     {    ...
Basic Graph Pattern - Blank Nodes             @prefix foaf: <http://xmlns.com/foaf/0.1/> .    Data             _:a foaf:na...
Group PatternPREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?name ?mboxWHERE{ ?x foaf:name ?name . ?x foaf:mbox ?mbox }PR...
Value Constraints                         @prefix dc: <http://purl.org/dc/elements/1.1/> .                Data     @prefix...
Regular expressions can be used•   “Find the name and email addresses of authors of a paper    about SPARQL”PREFIX dc:    ...
Optional graph patterns<uni:lecturer rdf:about=“949352”>     <uni:name>Grigoris Antoniou</uni:name></uni:lecturer><uni:pro...
Optional graph patterns (2)    All lecturers and their email addresses:SELECT ?name ?emailWHERE{ ?x rdf:type uni:Lecturer...
Optional graph patterns(3)    As a solution we can adapt the query to use an optional pattern:     SELECT ?name ?email   ...
Optional graph patterns (4)                   @prefix dc: <http://purl.org/dc/elements/1.1/> .                   @prefix :...
Multiple Optional Blocks                  @prefix foaf: <http://xmlns.com/foaf/0.1/> .                  @prefix rdf: <http...
Alternative Graph Patterns                 @prefix dc10: <http://purl.org/dc/elements/1.0/> .                 @prefix dc11...
Upcoming SlideShare
Loading in...5
×

Sparql

858

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
858
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
22
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sparql

  1. 1. SPARQL1
  2. 2. Query Languages: SQL(SQL Query Language) A language for querying collections of tuples: SELECT SALARY, HIRE_DATE FROM EMPS WHERE EMP_ID = 13954 EMP_ID NAME HIRE_DATE SALARY 13954 Joe 2000-04-14 48000 10335 Mary 1998-11-23 52000 … … … … 04182 Bob 2005-02-10 21750 2
  3. 3. Query Languages: XQuery(XML Query) A language for querying trees of XDM nodes: for $e in document(my_employees.xml) where $emp/emp/@emp-id = 13954 return $emp/emp/salary < e m p lo ye e s> < e m p> < e m p> <e m p> <e m p> e m p-id = 1 39 54 ... ... ... < n a m e> < h ire -d a te> < s a la ry> Joe 2 0 00 -04 -14 4 80 00 3
  4. 4. Why an RDF Query Language?Different XML Representations XML at a lower level of abstraction than RDF There are various ways of syntactically representing an RDF statement in XML Thus we would require several XQuery queries, e.g.  //uni:lecturer/uni:title if uni:title element  //uni:lecturer/@uni:title if uni:title attribute  Both XML representations equivalent! 4
  5. 5. Two basic families of SQL-like langages forRDF(S) RDQL  Implementations: Jena, Sesame, RDFStore, ... RQL  Implementations: RQL, SPARQL, ... 5
  6. 6. Introduction to RDQL RDF Data Query Language JDBC/ODBC friendly Simple: SELECT some information FROM somewhere WHERE this match AND these constraints USING these vocabularies 6
  7. 7. Example7
  8. 8. Example q1 contains a query:SELECT ?xWHERE (?x, <http://www.w3.org/2001/vcard-rdf/3.0#FN>, "John Smith") For executing q1with a model m1.rdf:java jena.rdfquery --data m1.rdf --query q1 The outcome is:x=============================<http://somewhere/JohnSmith/> 8
  9. 9. Example Return all the resources that have property FN and the associated values:SELECT ?x, ?fnameWHERE (?x, <http://www.w3.org/2001/vcard-rdf/3.0#FN>, ?fname) The outcome is:x | fname================================================<http://somewhere/JohnSmith/> | "John Smith"<http://somewhere/SarahJones/> | "Sarah Jones"<http://somewhere/MattJones/> | "Matt Jones" 9
  10. 10. Example Return the first name of Jones:SELECT ?givenNameWHERE (?y, <http://www.w3.org/2001/vcard-rdf/3.0#Family>, "Jones"), (?y, <http://www.w3.org/2001/vcard-rdf/3.0#Given>, ?givenName) The outcome is:givenName========="Matthew""Sarah" 10
  11. 11. URI Prefixes : USING RDQL has a syntactic convenience that allows prefix strings to be defined in the USING clause :SELECT ?xWHERE (?x, vCard:FN, "John Smith")USING vCard FOR <http://www.w3.org/2001/vcard-rdf/3.0#>SELECT ?givenNameWHERE (?y, vCard:Family, "Smith"), (?y, vCard:Given, ?givenName)USING vCard FOR <http://www.w3.org/2001/vcard-rdf/3.0#> 11
  12. 12. Filters RDQL has a syntactic convenience that allows prefix strings to be defined in the USING clause :SELECT ?resourceWHERE (?resource, info:age, ?age)AND ?age >= 24USING info FOR <http://somewhere/peopleInfo#> 12
  13. 13. Limitations Does not take into account semantics of RDF(S) For example: ex:human rdfs:subClassOf ex:animal ex:student rdfs:subClassOf ex:human ex:john rdf:type ex:student Query: “ To which class does the resource John belong?” Expected answer: ex:student, ex:human, ex:animal However, the query: SELECT ?x WHERE (<http://example.org/#john>, rdf:type, ?x) USING rdf FOR <http://www.w3.org/1999/02/22-rdf-syntax-ns#> Yields only: <http://example.org/#student> Solution: Inference Engines 13
  14. 14. Why an RDF Query Language?Different XML Representations XML at a lower level of abstraction than RDF There are various ways of syntactically representing an RDF statement in XML Thus we would require several XPath queries, e.g.  //uni:lecturer/uni:title if uni:title element  //uni:lecturer/@uni:title if uni:title attribute  Both XML representations equivalent! 14
  15. 15. SPARQL SPARQL is RDF Query Language It provides facilities to:  extract information in the form of URIs, blank nodes, plain and typed literals.  extract RDF subgraphs.  construct new RDF graphs based on information in the queried graphs
  16. 16. SPARQL Basic Queries SPARQL is based on matching graph patterns The simplest graph pattern is the triple pattern :- like an RDF triple, but with the possibility of a variable instead of an RDF term in the subject, predicate, or object positions Combining triple patterns gives a basic graph pattern, where an exact match to a graph is needed to fulfill a pattern 16
  17. 17. Basic graph pattern Set of Triple Patterns  Triple Pattern – similar to an RDF Triple (subject, predicate, object), but any component can be a query variable; literal subjects are allowed ?book dc:title ?title  Matching a triple pattern to a graph: bindings between variables and RDF Terms Matching of Basic Graph Patterns  A Pattern Solution of Graph Pattern GP on graph G is any substitution S such that S(GP) is a subgraph of G. SELECT ?x ?v WHERE { ?x ?x ?v } rdf:type rdf:type rdf:Property x v rdf:type rdf:Property
  18. 18. ExamplesPREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>SELECT ?cWHERE{ ?c rdf:type rdfs:Class .} Retrieves all triple patterns, where:- the property is rdf:type- the object is rdfs:Class Which means that it retrieves all classes 18
  19. 19. Examples (2) Get all instances of a particular class (e.g. course) :PREFIX uni: <http://www.mydomain.org/uni-ns#>SELECT ?iWHERE{ ?i rdf:type uni:course .} 19
  20. 20. Multiple Matches Data @prefix foaf: <http://xmlns.com/foaf/0.1/> . _:a foaf:name "Johnny Lee Outlaw" . _:a foaf:mbox <mailto:jlow@example.com> . _:b foaf:name "Peter Goodguy" . _:b foaf:mbox <mailto:peter@example.org> .PREFIX foaf: <http://xmlns.com/foaf/0.1/> QuerySELECT ?name ?mbox Group Graph PatternWHERE (set of graph patterns){ ?x foaf:name ?name . also! ?x foaf:mbox ?mbox } Query Result name mbox "Johnny Lee <mailto:jlow@example.com> Outlaw" "Peter Goodguy" <mailto:peter@example.org>
  21. 21. Using select-from-where As in SQL, SPARQL queries have a SELECT-FROM-WHERE structure:  SELECT specifies the projection: the number and order of retrieved data  FROM is used to specify the source being queried (optional)  WHERE imposes constraints on possible solutions in the form of graph pattern templates and boolean constraints Retrieve all phone numbers of staff members: SELECT ?x ?y WHERE { ?x uni:phone ?y .} Here ?x and ?y are variables, and ?x uni:phone ?y represents a resource- property-value triple pattern 21
  22. 22. Implicit Join Retrieve all lecturers and their phone numbers: SELECT ?x ?y WHERE { ?x rdf:type uni:Lecturer ; uni:phone ?y . } Implicit join: We restrict the second pattern only to those triples, the resource of which is in the variable ?x  Notice the syntax shortcut : a semicolon indicates that the following triple shares its subject with the previous one 22
  23. 23. Implicit join (2) The previous query is equivalent to writing: SELECT ?x ?y WHERE { ?x rdf:type uni:Lecturer . ?x uni:phone ?y . } 23
  24. 24. Explicit Join Retrieve the name of all courses taught by the lecturer with ID 949352 SELECT ?n WHERE { ?x rdf:type uni:Course ; uni:isTaughtBy 949352 . ?c uni:name ?n . FILTER (?c = ?x) . } 24
  25. 25. Basic Graph Pattern - Blank Nodes @prefix foaf: <http://xmlns.com/foaf/0.1/> . Data _:a foaf:name "Alice" . _:b foaf:name "Bob" .PREFIX foaf: <http://xmlns.com/foaf/0.1/> QuerySELECT ?x ?nameWHERE { ?x foaf:name ?name } x name Query Result _:c “Alice“ _:d “Bob”
  26. 26. Group PatternPREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?name ?mboxWHERE{ ?x foaf:name ?name . ?x foaf:mbox ?mbox }PREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?name ?mboxWHERE{ {?x foaf:name ?name; foaf:mbox ?mbox } }
  27. 27. Value Constraints @prefix dc: <http://purl.org/dc/elements/1.1/> . Data @prefix : <http://example.org/book/> . @prefix ns: <http://example.org/ns#> . :book1 dc:title "SPARQL Tutorial" . :book1 ns:price 42 . :book2 dc:title "The Semantic Web" . :book2 ns:price 23 .PREFIX dc: <http://purl.org/dc/elements/1.1/> QueryPREFIX ns: <http://example.org/ns#>SELECT ?title ?priceWHERE { ?x ns:price ?price . FILTER ?price < 30 . ?x dc:title ?title . } title price Query Result "The Semantic Web" 23
  28. 28. Regular expressions can be used• “Find the name and email addresses of authors of a paper about SPARQL”PREFIX dc: <http://purl.org/dc/elements/1.1/>PREFIX ldap: <http://ldap.hp.com/people#>PREFIX foaf:SELECT ?name ?name2{ WHERE ?doc dc:title ?title . FILTER regex(?title, “SPARQL”) . ?doc dc:creator ?reseacher . ?researcher ldap:email ?email . ?researcher ldap:name ?name} 28
  29. 29. Optional graph patterns<uni:lecturer rdf:about=“949352”> <uni:name>Grigoris Antoniou</uni:name></uni:lecturer><uni:professor rdf:about=“94318”> <uni:name>David Billington</uni:name> <uni:email>david@work.example.org</uni:email></uni:professor> For one lecturer it only lists the name For the other it also lists the email address 29
  30. 30. Optional graph patterns (2) All lecturers and their email addresses:SELECT ?name ?emailWHERE{ ?x rdf:type uni:Lecturer ; uni:name ?name ; uni:email ?email .} The result of the previous query would be: ?name ?email David Billington david@work.example.org Grigoris Antoniou is listed as a lecturer, but he has no e-mail address 30
  31. 31. Optional graph patterns(3) As a solution we can adapt the query to use an optional pattern: SELECT ?name ?email WHERE { ?x rdf:type uni:Lecturer ; uni:name ?name . OPTIONAL { x? uni:email ?email } } The meaning is roughly “give us the names of lecturers, and if known also their e- mail address” The result looks like this: ?name ?email Grigoris Antoniou David Billington david@work.example.org 31
  32. 32. Optional graph patterns (4) @prefix dc: <http://purl.org/dc/elements/1.1/> . @prefix : <http://example.org/book/> . Data @prefix ns: <http://example.org/ns#> . :book1 dc:title "SPARQL Tutorial" . :book1 ns:price 42 . :book2 dc:title "The Semantic Web" . :book2 ns:price 23 .PREFIX dc: <http://purl.org/dc/elements/1.1/>PREFIX ns: <http://example.org/ns#>SELECT ?title ?price QueryWHERE { ?x dc:title ?title . OPTIONAL { ?x ns:price ?price . FILTER ?price < 30 }} title price “SPARQL Tutorial“ Query Result "The Semantic Web" 23
  33. 33. Multiple Optional Blocks @prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . Data @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . _:a foaf:name "Alice" . _:a foaf:homepage <http://work.example.org/alice/> . _:b foaf:name "Bob" . _:b foaf:mbox <mailto:bob@work.example> .PREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?name ?mbox ?hpage QueryWHERE { ?x foaf:name ?name . OPTIONAL { ?x foaf:mbox ?mbox }.OPTIONAL { ?x foaf:homepage ?hpage } } Query Resultname Mbox hpage“Alice“ <http://work.example.org/alice/>“Bob“ <mailto:bob@example.com>
  34. 34. Alternative Graph Patterns @prefix dc10: <http://purl.org/dc/elements/1.0/> . @prefix dc11: <http://purl.org/dc/elements/1.1/> . Data _:a dc10:title "SPARQL Query Language Tutorial" . _:b dc11:title "SPARQL Protocol Tutorial" . _:c dc10:title "SPARQL" . _:c dc11:title "SPARQL (updated)" .PREFIX dc10: <http://purl.org/dc/elements/1.0/>PREFIX dc11: <http://purl.org/dc/elements/1.1/> QuerySELECT ?x ?yWHERE { { ?book dc10:title ?x } UNION { ?book dc11:title ?y } } Query Resultx y "SPARQL (updated)" "SPARQL Protocol Tutorial""SPARQL""SPARQL Query Language Tutorial"
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×