400 likes | 417 Views
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.
E N D
SPARQL - A querylanguage for RDF(s) Fred Freitas - CIn/UFPE - Brazil
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
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
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
TwobasicfamiliesofSQL-likelangages for RDF(S) • RDQL • Implementations: Jena, Sesame, RDFStore, ... • RQL • Implementations: RQL, SPARQL, ...
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
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/>
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"
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"
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#>
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#>
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
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
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
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.
Basic Graph Pattern - Multiple Matches Data Query Group Graph Pattern (set of graph patterns) also! Query Result
Basic Graph Pattern - Blank Nodes Data Query Query Result
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
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
Value Constraints Data Query Query Result
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”
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
Optional graph patterns Data Query Query Result
Multiple Optional Blocks Data Query Query Result
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
Alternative Graph Patterns Data Query Query Result
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
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.
RDF Dataset- The Relationship between Named and Background Graphs (I)
RDF Dataset- The Relationship between Named and Background Graphs (II)
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" .
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).