sparql all slides are adapted from the w3c recommendation sparql query language for rdf web link:

21
SPARQL All slides are adapted from the W3C Recommendation SPARQL Query Language for RDF Web link: http://www.w3.org/TR/rdf-sparql-query/

Upload: asher-fowler

Post on 26-Dec-2015

239 views

Category:

Documents


2 download

TRANSCRIPT

SPARQLAll slides are adapted from the W3C Recommendation

SPARQL Query Language for RDF

Web link: http://www.w3.org/TR/rdf-sparql-query/

5. Named Graphs

Named Graphs

• Every SPARQL query runs against:–At least the default graph–One or more named graphs–Or merged set of default and all named graphs

*Derived from http://www.w3.org/TR/rdf-sparql-query -Section 8.2

Named Graph Concepts

• Set of triples with an identifier– No relation required to URIs in the set– Identifier must be IRI

• Generally URI with no spaces (and some other characters)

• Name/IRI can (obviously) also be a URI– Often associated with concept of a file

• i.e. graph in a file• Not a required structure

– A triple may be in any number of graphs• If file-based, then appearing in multiple files• In a persistent store, triple is likely replicated with a different graph identifier with each copy

Named Graph Aggregation• Graphs at a single SPARQL endpoint

– Endpoints may aggregate as they see fit• May query other/remote SPARQL endpoints for knowledge

• May provide whatever graph name they wish– SPARQL does not define the mechanism that requests aggregation of multiple endpoints• Nor does it define whether or not they are considered a named graph

• SPARQL allows selection of graphs based on their name (within the local system)

SPARQL Named Graph

• Keywords– FROM

• contains an IRI that indicates a graph to be used to form the default graph

• In this case, the named graph lose its identity in the query

– FROM NAMED• provide a named graph in the RDF Dataset• The name of the graph can be part of the query• If no FROM clause, then default graph is empty

– GRAPH • For the WHERE clause to specify or find out the graph name fulfilling a query

Simple Annotated Example

PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX dc: <http://purl.org/dc/elements/1.1/>

SELECT ?who ?g ?mboxFROM <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 }}

Replace default graph with dft.ttl

Add two named graphs to the set of graphs

Find who published the graph*Find graphs using the mbox property.

*The default graph has no identifier (so is never the answer).

Usage Query Effects

Construct Number of Occurrences

Effect

FROM One Replaces Default Graph

FROM More than one Merged together and replaces Default Graph

FROM NAMED

N/A Added to triple set “along-side” default graph (i.e. maintaining named identity)

Example Using GRAPH for RestrictionPREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX data: <http://example.org/foaf/>

SELECT ?nickFROM NAMED <http://example.org/foaf/aliceFoaf>FROM NAMED <http://example.org/foaf/bobFoaf>WHERE { GRAPH data:bobFoaf { ?x foaf:mbox <mailto:[email protected]> . ?x foaf:nick ?nick } }

Only query bobFoaf for this info*.

*The default graph has no identifier (so it is never the restriction).

6. Unsupported Functionality

Data Modification

• Some proposed standards, but generally considered a bad idea

• Proposals–SPARQL Update

•SPARUL or SPARQL+•Adds CREATE DELETE, DROP, and INSERT

Subqueries

• No specific subquery support, but does support nested subgraphs in a query

• Subquery–Nested SELECT within a WHERE

• Nested subgraphs–Can nest graph patterns inside other graph patterns

Aggregation

• No count, min, max, avg, or sum• SPARQL+ proposes such

• Full list of SPARQL issues maintained: – http://www.w3.org/2001/sw/DataAccess/issues

7. Other Features

Other Query Forms

• ASK– Test whether or not a query pattern has a solution

– Just returns whether or not a solution existsPREFIX foaf: <http://xmlns.com/foaf/0.1/>/ASK { ?x foaf:name "Alice" ; foaf:mbox <mailto:[email protected]> }

-> true / false (in XML form)

Other Query Forms

• DESCRIBE– returns a single result RDF graph (i.e. tripes file) containing RDF data about resources

– The SPARQL service decides how much/what to return (whatever the service providers considers to be useful)PREFIX foaf: <http://xmlns.com/foaf/0.1/>DESCRIBE ?x ?y <http://example.org/>WHERE {?x foaf:knows ?y}

SortingSyntax Description

LIMIT <number> Limit the number of rows of output

OFFSET <number> Skip the number of rows of output

ORDER BY <variable>ORDER BY ASC()/DESC()

Sort the output by variable – default is ascending

PREFIX table: <http://www.daml.org/2003/01/periodictable/PeriodicTable#>

SELECT ?name

WHERE

{

?element table:name ?name;

table:atomicWeight ?weight.

}

ORDER BY DESC(?weight)

LIMIT 10

OFFSET 10

7. Summary Features

Summarized from: http://thefigtrees.net/lee/sw/sparql-faq

SPARQL Benefits• Implicit join syntax

– Does not require explicit joins that specify the relationship between differently structured data• All relationships are of a fixed size• Data lives in a single graph

• Strong support for querying semi-structured and ragged data– i.e. data with an unpredictable and unreliable structure

– Variables may occur in the predicate position to query unknown relationships

– OPTIONAL keyword provides support for querying relationships that may or may not occur in the data (a la SQL left joins)

More SPARQL Benefits

• Query disparate data sources (not sharing a single native representation) in a single query– Join heterogeneous data at a higher level than that of the native structure of the data

– Mappings can be performed on the fly– Overcomes the different structures used by different database vendors!

• Query in a networked, web environment– Pairing of a default graph and zero or more named graphs

– SPARQL endpoint may join information from multiple data sources accessible across different Web sites • Can use NAMED or NAMED GRAPH to indicate remote graphs to include

SPARQL Drawbacks

• Relatively Young– At least compared to XPath or SQL

• Immature– Query optimization still a research topic

• Lack of support for transitive/ hierarchical queries– Really for known/explicit levels or hierarchy