ESWC SS 2012 - Monday Tutorial 2 Barry Norton: SPARQL 1.1 Query Language

314 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
314
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

ESWC SS 2012 - Monday Tutorial 2 Barry Norton: SPARQL 1.1 Query Language

  1. 1. SPARQL 1.1 Query Language SPARQL 1.1 Query
  2. 2. Overview • • • • • • • • SPARQL 1.1 Overview Aggregates Subqueries Negation Expressions in SELECT Property Paths New Built-ins Short form CONSTRUCT SPARQL 1.1 Query #2
  3. 3. What is SPARQL? • SPARQL was a query language for RDF data – http://www.w3.org/TR/rdf-sparql-query/ • And a protocol – http://www.w3.org/TR/rdf-sparql-protocol/ • Designed around graph patterns – Graph patterns use Turtle syntax • SPARQL 1.1 introduces: – – – – Query extensions (covered here) Protocol extensions and a (RESTful) graph store protocol Updates (covered next) Service descriptions and federation SPARQL 1.1 Query #3
  4. 4. SPARQL 1.1 Query Extensions • SPARQL 1.1 adds the following new features (http://www.w3.org/TR/sparql11-query) – Aggregates – – – – – – – Subqueries Negation Expressions in the SELECT clause Property Paths Assignment A short form for CONSTRUCT An expanded set of functions and operators SPARQL 1.1 Query #4
  5. 5. Aggregates • Aggregates allow computation of values using: – COUNT, SUM, MIN, MAX, AVG, GROUP_CONCAT and SAMPLE • Built around the GROUP BY operator • For example computing popularity in a social graph: SELECT ?person (COUNT(?someone) AS ?popularity) WHERE {?someone foaf:knows ?person} GROUP BY ?person • Prune at group level (cf. FILTER) using HAVING, e.g.: GROUP BY ?person HAVING (COUNT(?someone) > 300) SPARQL 1.1 Query #5
  6. 6. Subqueries • Subqueries allow bindings from one (inner) query to be used in another (outer) query • For example, to compute whom the most popular person in a social graph knows: SELECT ?friend WHERE {?popular foaf:knows ?friend. {SELECT ?popular (COUNT(?someone) AS ?popularity) WHERE {?someone foaf:knows ?popular} GROUP BY ?popular ORDER BY DESC (?popularity) LIMIT 1}} SPARQL 1.1 Query #6
  7. 7. Negation 1/2 • SPARQL 1.0 negation was limited, applying only to simple FILTER expressions • SPARQL 1.1 introduces EXISTS, allowing graph matching (without binding) • For example, to compute email addresses of people who know no Barrys (!): SELECT ?email WHERE {?sad foaf:mbox ?email . FILTER NOT EXISTS {?sad foaf:knows ?barry. {?barry foaf:givenName "Barry"} UNION {?barry foaf:firstName "Barry"}} SPARQL 1.1 Query #7
  8. 8. Negation 2/2 • SPARQL 1.0 negation was limited, applying only to simple FILTER expressions • SPARQL 1.1 also introduces MINUS, allowing explicit removal of triples (according to another match) • For example, to compute people who know Barrys other than Barry Norton: SELECT DISTINCT ?person WHERE {?person foaf:knows ?barry. ?barry foaf:givenName "Barry". MINUS {?person foaf:knows [foaf:mbox <mailto:barry.norton@ontotext.com>]}} SPARQL 1.1 Query #8
  9. 9. Expressions in SELECT (Projection) Clauses • SPARQL 1.0 allows built-in functions, etc., to be used in FILTER • SPARQL 1.1 allows their use in (the projection onto) variables in the head of the query • For example, to glue together names: SELECT (CONCAT(?forename, " ", ?surname) AS ?name) WHERE {{{?person foaf:givenName ?forename} UNION {?person foaf:firstName ?forename}}. {{?person foaf:familyName ?surname} UNION {?person foaf:lastName ?surname}}} SPARQL 1.1 Query #9
  10. 10. Property Paths 1/2 • SPARQL 1.0 builds graph patterns from triple patterns, where resources are separated in the graph by one arc • SPARQL 1.1 generalises on triple patterns to model resources separated by paths of arbitrary length • Paths are specified using the following grammar that follows • Example: any member of an RDF List: ?list rdf:rest*/rdf:first ?element SPARQL 1.1 Query #10
  11. 11. Property Paths 2/2 • Example: anyone in social graph of ?p knows a Barry: ?p {foaf:knows | ^foaf:knows}* / (foaf:givenName | foaf:firstName) "Barry" SPARQL 1.1 Query #11
  12. 12. New Built-ins • SPARQL 1.1 supports a wider set of built-ins than SPARQL 1.0, introducing: – Maths: ABS (absolute value), ROUND/CEIL /FLOOR(rounding), RAND (random), isNUMERIC – Strings: CONCAT, CONTAINS, SUBSTR, STRLEN, UCASE, LCASE – Dates: YEAR, MONTH, DAY, HOURS, MINUTES, SECONDS, TIMEZONE/TZ (timezone as XML or literal), NOW – Hashes: MD5 , SHA1, SHA224, SHA256, SHA384, SHA512 – Enumeration: IN, NOT IN – Control: IF, COALESCE – Identifiers: ENCODE_FOR_URI, IRI/URI, BNODE (create a new blank node, can be with parameter) SPARQL 1.1 Query #12
  13. 13. Short Form CONSTRUCT • In order to query for a subgraph, without change, it is no longer necessary to repeat the graph pattern • Example: CONSTRUCT WHERE {?barry foaf:givenName "Barry"; ?p ?o} SPARQL 1.1 Query #13

×