1 / 40

SPARQL - A query language for RDF(s)

SPARQL - A query language for RDF(s) . Fred Freitas - CIn/UFPE - Brazil. Query Languages: SQL ( S QL Q uery L anguage ) [Oracle 2006]. A language for querying collections of tuples: SELECT SALARY, HIRE_DATE FROM EMPS WHERE EMP_ID = 13954.

charliek
Download Presentation

SPARQL - A query language for RDF(s)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SPARQL - A querylanguage for RDF(s) Fred Freitas - CIn/UFPE - Brazil

  2. Query Languages: SQL(SQL Query Language) [Oracle 2006] • A language for querying collections of tuples:SELECT SALARY, HIRE_DATE FROM EMPS WHERE EMP_ID = 13954

  3. Query Languages: XQuery(XML Query) [Oracle 2006] • 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

  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! A Semantic Web Primer

  5. TwobasicfamiliesofSQL-likelangages for RDF(S) • RDQL • Implementations: Jena, Sesame, RDFStore, ... • RQL • Implementations: RQL, SPARQL, ...

  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

  7. Example

  8. Example • q1 contains a query: SELECT ?x WHERE (?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/>

  9. Example • Return all the resources that have property FN and the associated values: SELECT ?x, ?fname WHERE (?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"

  10. Example • Return the first name of Jones: SELECT ?givenName WHERE (?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"

  11. URI Prefixes : USING • RDQL has a syntactic convenience that allows prefix strings to be defined in the USING clause : SELECT ?x WHERE (?x, vCard:FN, "John Smith") USING vCard FOR <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?givenName WHERE (?y, vCard:Family, "Smith"), (?y, vCard:Given, ?givenName) USING vCard FOR <http://www.w3.org/2001/vcard-rdf/3.0#>

  12. Filters • RDQL has a syntactic convenience that allows prefix strings to be defined in the USING clause : SELECT ?resource WHERE (?resource, info:age, ?age) AND ?age >= 24 USING info FOR <http://somewhere/peopleInfo#>

  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

  14. SPARQL • SPARQL is a recursive acronym standing for • SPARQL Protocol and RDF Query Language • It provides facilities to [Staab 2006]: • 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

  15. Graph Patterns Basic Graph Pattern – set of Triple Patterns Group Pattern - a set of graph patterns must all match Value Constraints - restrict RDF terms in a solution Optional Graph Patterns .- additional patterns may extend the solution Alternative Graph Pattern – two or more possible patterns are tried Patterns on Named Graphs - patterns are matched against named graphs

  16. 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 • 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.

  17. Basic Graph Pattern - Multiple Matches Data Query Group Graph Pattern (set of graph patterns) also! Query Result

  18. Basic Graph Pattern - Blank Nodes Data Query Query Result

  19. Graph Patterns Basic Graph Pattern – set of Triple Patterns Group Pattern - a set of graph patterns must all match Value Constraints - restrict RDF terms in a solution Optional Graph Patterns .- additional patterns may extend the solution Alternative Graph Pattern – two or more possible patterns are tried Patterns on Named Graphs - patterns are matched against named graphs

  20. Group Pattern

  21. Graph Patterns Basic Graph Pattern – set of Triple Patterns Group Pattern - a set of graph patterns must all match Value Constraints - restrict RDF terms in a solution Optional Graph Patterns .- additional patterns may extend the solution Alternative Graph Pattern – two or more possible patterns are tried Patterns on Named Graphs - patterns are matched against named graphs

  22. Value Constraints Data Query Query Result

  23. Regular expressions can be used PREFIX dc: <http://purl.org/dc/elements/1.1/>PREFIX ldap: <http://ldap.hp.com/people#> PREFIX foaf: SELECT ?name ?name2 { ?doc dc:title ?title . FILTER regex(?title, “SPARQL”) . ?doc dc:creator ?reseacher . ?researcher ldap:email ?email . ?researcher ldap:name ?name } • “Find the name and email addresses of authors of a paper about SPARQL”

  24. Graph Patterns Basic Graph Pattern – set of Triple Patterns Group Pattern - a set of graph patterns must all match Value Constraints - restrict RDF terms in a solution Optional Graph Patterns .- additional patterns may extend the solution Alternative Graph Pattern – two or more possible patterns are tried Patterns on Named Graphs - patterns are matched against named graphs

  25. Optional graph patterns Data Query Query Result

  26. Multiple Optional Blocks Data Query Query Result

  27. Graph Patterns Basic Graph Patterns – set of Triple Patterns Group Patterns - a set of graph patterns must all match Value Constraints - restrict RDF terms in a solution Optional Graph Patterns .- additional patterns may extend the solution Alternative Graph Patterns – two or more possible patterns are tried Patterns on Named Graphs - patterns are matched against named graphs

  28. Alternative Graph Patterns Data Query Query Result

  29. Graph Patterns Basic Graph Pattern – set of Triple Patterns Group Pattern - a set of graph patterns must all match Value Constraints - restrict RDF terms in a solution Optional Graph Patterns .- additional patterns may extend the solution Alternative Graph Pattern – two or more possible patterns are tried Patterns on Named Graphs - patterns are matched against named graphs

  30. RDF Dataset • RDF data stores may hold multiple RDF graphs: • record information about each graph • queries that involve information from more than one graph • RDF Dataset in SPARQL terminology • the background graph, which does not have a name, and zero or more named graphs, identified by URI reference • the relationship between named and background graphs: • (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.

  31. RDF Dataset- The Relationship between Named and Background Graphs (I)

  32. RDF Dataset- The Relationship between Named and Background Graphs (II)

  33. Querying the Dataset

  34. Querying the Dataset - Accessing Graph Labels

  35. Querying the Dataset - Restricting by Graph Label

  36. Querying the Dataset - Restricting via Query Pattern

  37. Data: @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" . Query: 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 } . } Constructing an Output Graph Result: @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" .

  38. ASK – A Boolean query

  39. Conclusions • Several query languages for RDF/RDF(S) • Some (not exclusive) SPARQL advantages • Inferencing • Implicit and explicit joins • Once the schemas are defined elsewhere in RDF/RDF(S) too, they can also be queried upon! • Many facilities: regex, datatypes, functions,... • Queries built similarly to SQL... • But also over graphs (in opposition to flat relational DBs).

  40. Any Questions?

More Related