sparql an rdf query language. sparql sparql is a recursive acronym for sparql protocol and rdf query...

25
SPARQL AN RDF Query Language

Upload: chad-hudson

Post on 01-Jan-2016

282 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

SPARQLAN RDF Query Language

Page 2: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

SPARQL

SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language

SPARQL is the SQL for RDF Example:

PREFIX abc: <http://example.com/exampleOnto#> SELECT ?capital ?countryWHERE { ?x abc:cityname ?capital ; abc:isCapitalOf ?y . ?y abc:countryname ?country ; abc:isInContinent abc:Africa .}

Page 3: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

SPARQL

SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language

SPARQL is the SQL for RDF Example:

PREFIX abc: <http://example.com/exampleOnto#> SELECT ?capital ?countryWHERE { ?x abc:cityname ?capital ; abc:isCapitalOf ?y . ?y abc:countryname ?country ; abc:isInContinent abc:Africa .}

Page 4: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

SPARQL History

Several RDF query languages were developed prior to SPARQL

W3C RDF Data Access Working Group (DAWG) worked out SPARQL 2005-2008

Became a W3C recommendation in January 2008 with key documents:– http://www.w3.org/TR/rdf-sparql-query/– http://www.w3.org/TR/rdf-sparql-protocol/– http://www.w3.org/TR/rdf-sparql-XMLres/

Implementations for multiple programming languages available

Page 5: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

SPARQL Query Forms

SELECT – Returns all, or a subset of, the variables bound in a

query pattern match. ASK

– Returns a boolean indicating whether a query pattern matches or not.

DESCRIBE – Returns an RDF graph that describes the resources

found. CONSTRUCT

– Returns an RDF graph constructed by substituting variables in a set of triple templates.

Page 6: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

It’s Turtles all the way down

Turtle (Terse RDF Triple Language ): − An RDF serialization

− Triple representation of <Subject, Predicate, Object>− Human-friendly alternative to RDF/XML

@prefix person: <http://example/person/> .@prefix foaf: <http://xmlns.com/foaf/0.1/> .Person:A foaf:name “Jek" .Person:A foaf:mbox <mailto:[email protected]> .Person:B foaf:name “Yuan" ._:b foaf:name “Jeff" . _:b foaf:mbox <mailto:[email protected]> .

A "hello world" of queriesSELECT ?nameWHERE { ?x foaf:name ?name }

-------------| name |========| “Jek” || ”Yuan” || ”Jeff” |-------------

<http://example/person/A> <http://xmlns.com/foaf.0.1/name> “Jek”

Blank

Node

Page 7: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

Matching RDF Literals

@prefix dt: <http://example.org/datatype#> . @prefix ns: <http://example.org/ns#> .@prefix : <http://example.org/ns#> .@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

:x ns:p "cat"@en . :y ns:p "42"^^xsd:integer . :z ns:p "abc"^^dt:specialDatatype .

SELECT ?v WHERE { ?v ?p "cat" }

---------| v |=====

SELECT ?v WHERE { ?v ?p "cat“@en }

----------------------------------| v |===================|http://example.org/ns#x |----------------------------------

SELECT ?v WHERE { ?v ?p 42 }

----------------------------------| V |===================|http://example.org/ns#y |----------------------------------

SELECT ?v WHERE { ?v ?p "abc dt:specialDatatype}

----------------------------------| V |===================|http://example.org/ns#z |----------------------------------

Page 8: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

Filter@prefix dc: <http://purl.org/dc/elements/1.1/> .@prefix stock: <http://example.org/stock#> .@prefix inv: <http://example.org/inventory#> .stock:book1 dc:title "SPARQL Query Language Tutorial" .stock:book1 dc:edition “First”stock:book1 inv:price 10 .stock:book1 inv:quantity 3 .stock:book2 dc:title "SPARQL Query Language (2nd ed)" .stock:book2 inv:price 20 ; inv:quantity 5 .stock:book3 dc:title "Applying XQuery“; dc:edition “Second” .stock:book3 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 ?titleWHERE {?book dc:title ?title .?book inv:price ?price . FILTER ( ?price < 15 )?book inv:quantity ?num . FILTER ( ?num > 0 ) }

---------------------------------------------------------------------| book | title |=======================================| stock:book1 | "SPARQL Query Language Tutorial" |---------------------------------------------------------------------

Page 9: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

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

Page 10: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

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:[email protected]> .

PREFIX foaf: <http://xmlns.com/foaf/0.1/> ASK { ?x foaf:name “Jek" }

Yes

<?xml version="1.0"?> <sparql xmlns="http://www.w3.org/2005/sparql-results#"> <head>

</head> <results>

<boolean>true</boolean> </results>

</sparql>

Page 11: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

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/book3"> <dc:creator rdf:parseType="Resource"> <vcard:N rdf:parseType="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>

Page 12: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

Describes’s results?

The DAWG did not reach a consensus on what describe should return

Possibilities include– All triples where the variable bindings are mentioned– All triples where the bindings are the subject– Something else

What is useful might depend on the application or the amount of data involved

So it was left to the implementation

Page 13: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

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.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 } .}

@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" .

Page 14: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

On construct

Having a result form that produces an RDF graph is a good idea

It enables on to construct systems by using the output of one SPARQL query as the data over which another query works

This kind of capability was a powerful one for relational databases

Page 15: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

Optional Pattern Matching

Data:@prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . _:a rdf:type foaf:Person . _:a foaf:name "Alice" . _:a foaf:mbox <mailto:[email protected]> . _:a foaf:mbox <mailto:[email protected]> . _: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 }

}

name mbox

„Alice“ <mailto:[email protected]>

„Alice“ <mailto:[email protected]>

„Bob“

Query Result:

Page 16: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

RDF Dataset

RDF data stores may hold multiple RDF graphs:– 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 Use 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

Page 17: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

RDF Named graphs

Having multiple RDF graphs in a single document/repository and naming them with URIs

Provides useful additional functionality built on top of the RDF Recommendations

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 }

}

Page 18: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

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:[email protected]> .

# Graph: http://example.org/alice

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:name "Alice" .

_:a foaf:mbox <mailto:[email protected]> .

Page 19: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

Example(II)

# Background graph

@prefix foaf: <http://xmlns.com/foaf/0.1/> . _:x foaf:name "Bob" .

_:x foaf:mbox <mailto:[email protected]> .

_:y foaf:name "Alice" .

_:y foaf:mbox <mailto:[email protected]> .

# Graph: http://example.org/bob

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:name "Bob" .

_:a foaf:mbox <mailto:[email protected]> .

# Graph: http://example.org/alice

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:name "Alice" .

_:a foaf:mbox <mailto:[email protected]> .

Page 20: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

Querying the Dataset# Graph: http://example.org/foaf/aliceFoaf

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

_:a foaf:name "Alice" .

_:a foaf:mbox <mailto:[email protected]> .

_:a foaf:knows _:b .

_:b rdfs:seeAlso <http://example.org/foaf/bobFoaf> .

<http://example.org/foaf/bobFoaf> rdf:type foaf:PersonalProfileDocument .

_:b foaf:name "Bob" .

_:b foaf:mbox <mailto:[email protected]> .

_:b foaf:age 32 . # Graph: http://example.org/foaf/bobFoaf

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

_:1 foaf:mbox <mailto:[email protected]> .

_:1 rdfs:seeAlso <http://example.org/foaf/bobFoaf> .

_:1 foaf:age 35 .

<http://example.org/foaf/bobFoaf> rdf:type foaf:PersonalProfileDocument .

Page 21: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

Accessing Graph Labels

PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?src ?bobAge

WHERE { GRAPH ?src

{ ?x foaf:mbox <mailto:[email protected]> .

?x foaf:age ?bobAge }

}

src bobAge

<http://example.org/foaf/aliceFoaf> 32

<http://example.org/foaf/bobFoaf> 35

Page 22: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

Restricting by Graph Label

PREFIX foaf: <http://xmlns.com/foaf/0.1/>

PREFIX data: <http://example.org/foaf/>

SELECT ?age

WHERE

{

GRAPH data:bobFoaf {

?x foaf:mbox <mailto:[email protected]> .

?x foaf:age ?age }

}

age

35

Page 23: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

Restricting via Query Pattern PREFIX data: <http://example.org/foaf/>

PREFIX foaf: <http://xmlns.com/foaf/0.1/>

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?mbox ?age ?ppd

WHERE

{ GRAPH data:aliceFoaf

{ ?alice foaf:mbox <mailto:[email protected]> ;

foaf:knows ?whom .

?whom foaf:mbox ?mbox ;

rdfs:seeAlso ?ppd .

?ppd a foaf:PersonalProfileDocument . } .

GRAPH ?ppd { ?w foaf:mbox ?mbox ;

foaf:age ?age } }

mbox age ppd

<mailto:[email protected]> 35 <http://example.org/foaf/bobFoaf>

Page 24: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

More Features

RDF Dataset

- Collection of RDF Graphs use FROM http://planetrdf.com/bloggers.rdf use FROM NAMED

<http://site1.example.com/foo.rdf>

Page 25: SPARQL AN RDF Query Language. SPARQL SPARQL is a recursive acronym for SPARQL Protocol And Rdf Query Language SPARQL is the SQL for RDF Example: PREFIX

Limitation of SPARQL

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.w3.org/2009/sparql/wiki/Category:Features