40
SPARQL - A QUERY LANGUAGE FOR RDF(S) Fred Freitas - CIn/UFPE - Brazil

SPARQL- A query language for RDF(s)

  • Upload
    yachi

  • View
    43

  • Download
    4

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: SPARQL- A  query language  for RDF(s)

SPARQL - A QUERY LANGUAGE FOR RDF(S)Fred Freitas - CIn/UFPE - Brazil

Page 2: SPARQL- A  query language  for RDF(s)

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

EMP_ID NAME HIRE_DATE SALARY

13954 Joe 2000-04-14 48000

10335 Mary 1998-11-23 52000

… … … …

04182 Bob 2005-02-10 21750

Page 3: SPARQL- A  query language  for RDF(s)

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

e m p-id = 1 39 54

Joe

< n a m e>

2 0 00 -04 -14

< h ire -d a te>

4 80 00

< sa la ry>

< e m p>

...

< e m p>

...

< e m p>

...

< e m p>

< e m p lo ye e s>

Page 4: SPARQL- A  query language  for RDF(s)

Chapter 3 A Semantic Web Primer4

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!

Page 5: SPARQL- A  query language  for RDF(s)

TWO BASIC FAMILIES OF SQL-LIKE LANGAGES FOR RDF(S)

RDQL Implementations: Jena, Sesame, RDFStore, ...

RQL Implementations: RQL, SPARQL, ...

Page 6: SPARQL- A  query language  for RDF(s)

6

INTRODUCTION TO RDQL RDF Data Query Language JDBC/ODBC friendly

Simple:

SELECTsome information

FROMsomewhere

WHEREthis match

ANDthese constraints

USINGthese vocabularies

Page 7: SPARQL- A  query language  for RDF(s)

7

EXAMPLE

Page 8: SPARQL- A  query language  for RDF(s)

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/>

Page 9: SPARQL- A  query language  for RDF(s)

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"

Page 10: SPARQL- A  query language  for RDF(s)

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"

Page 11: SPARQL- A  query language  for RDF(s)

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 ?givenNameWHERE (?y, vCard:Family, "Smith"),

(?y, vCard:Given, ?givenName) USING vCard FOR <http://www.w3.org/2001/vcard-rdf/3.0#>

Page 12: SPARQL- A  query language  for RDF(s)

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#>

Page 13: SPARQL- A  query language  for RDF(s)

13

LIMITATIONS

Does not take into account semantics of RDF(S) For example:

ex:human rdfs:subClassOf ex:animalex:student rdfs:subClassOf ex:humanex:john rdf:type ex:student

Query: “ To which class does the resource John belong?”Expected answer: ex:student, ex:human, ex:animalHowever, the query:

SELECT ?xWHERE (<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

Page 14: SPARQL- A  query language  for RDF(s)

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

Page 15: SPARQL- A  query language  for RDF(s)

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

Page 16: SPARQL- A  query language  for RDF(s)

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.

x v

rdf:type rdf:Property

rdf:type rdf:type rdf:Property

SELECT ?x ?v WHERE { ?x ?x ?v }

?book dc:title ?title

Page 17: SPARQL- A  query language  for RDF(s)

BASIC GRAPH PATTERN - MULTIPLE MATCHES

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

SELECT ?name ?mbox

WHERE

{ ?x foaf:name ?name .

?x foaf:mbox ?mbox }

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

_:a foaf:name "Johnny Lee Outlaw" .

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

_:b foaf:name "Peter Goodguy" .

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

name mbox

"Johnny Lee Outlaw"

<mailto:[email protected]>

"Peter Goodguy" <mailto:[email protected]>

Group Graph Pattern (set of graph patterns) also!

Data

Query

Query Result

Page 18: SPARQL- A  query language  for RDF(s)

BASIC GRAPH PATTERN - BLANK NODES

PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?x ?name

WHERE { ?x foaf:name ?name }

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

_:a foaf:name "Alice" .

_:b foaf:name "Bob" .

x name

_:c “Alice“

_:d “Bob”

Data

Query

Query Result

Page 19: SPARQL- A  query language  for RDF(s)

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

Page 20: SPARQL- A  query language  for RDF(s)

GROUP PATTERN

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

SELECT ?name ?mbox

WHERE

{ ?x foaf:name ?name .

?x foaf:mbox ?mbox }

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

SELECT ?name ?mbox

WHERE

{ {?x foaf:name ?name;

foaf:mbox ?mbox }

}

Page 21: SPARQL- A  query language  for RDF(s)

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

Page 22: SPARQL- A  query language  for RDF(s)

VALUE CONSTRAINTS

PREFIX dc: <http://purl.org/dc/elements/1.1/>

PREFIX ns: <http://example.org/ns#>

SELECT ?title ?price

WHERE { ?x ns:price ?price .

FILTER ?price < 30 .

?x dc:title ?title . }

@prefix dc: <http://purl.org/dc/elements/1.1/> . @prefix : <http://example.org/book/> .

@prefix ns: <http://example.org/ns#> .

:book1 dc:title "SPARQL Tutorial" .

:book1 ns:price 42 .

:book2 dc:title "The Semantic Web" .

:book2 ns:price 23 .

title price

"The Semantic Web" 23

Data

Query

Query Result

Page 23: SPARQL- A  query language  for RDF(s)

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”

Page 24: SPARQL- A  query language  for RDF(s)

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

Page 25: SPARQL- A  query language  for RDF(s)

OPTIONAL GRAPH PATTERNS

PREFIX dc: <http://purl.org/dc/elements/1.1/>

PREFIX ns: <http://example.org/ns#>

SELECT ?title ?price

WHERE { ?x dc:title ?title .

OPTIONAL { ?x ns:price ?price .

FILTER ?price < 30 }}

@prefix dc: <http://purl.org/dc/elements/1.1/> .

@prefix : <http://example.org/book/> .

@prefix ns: <http://example.org/ns#> .

:book1 dc:title "SPARQL Tutorial" .

:book1 ns:price 42 .

:book2 dc:title "The Semantic Web" .

:book2 ns:price 23 .

title price

“SPARQL Tutorial“

"The Semantic Web" 23

Data

Query

Query Result

Page 26: SPARQL- A  query language  for RDF(s)

MULTIPLE OPTIONAL BLOCKS

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

SELECT ?name ?mbox ?hpage

WHERE { ?x foaf:name ?name .

OPTIONAL { ?x foaf:mbox ?mbox }. OPTIONAL { ?x foaf:homepage ?hpage } }

@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:homepage <http://work.example.org/alice/> .

_:b foaf:name "Bob" .

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

Data

Query

Query Result

name Mbox hpage

“Alice“ <http://work.example.org/alice/>

“Bob“ <mailto:[email protected]>

Page 27: SPARQL- A  query language  for RDF(s)

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

Page 28: SPARQL- A  query language  for RDF(s)

ALTERNATIVE GRAPH PATTERNS

PREFIX dc10: <http://purl.org/dc/elements/1.0/>

PREFIX dc11: <http://purl.org/dc/elements/1.1/>

SELECT ?x ?y

WHERE { { ?book dc10:title ?x } UNION { ?book dc11:title ?y } }

@prefix dc10: <http://purl.org/dc/elements/1.0/> .

@prefix dc11: <http://purl.org/dc/elements/1.1/> .

_:a dc10:title "SPARQL Query Language Tutorial" .

_:b dc11:title "SPARQL Protocol Tutorial" .

_:c dc10:title "SPARQL" .

_:c dc11:title "SPARQL (updated)" .

Data

Query

Query Result

x y

"SPARQL (updated)"

"SPARQL Protocol Tutorial"

"SPARQL"

"SPARQL Query Language Tutorial"

Page 29: SPARQL- A  query language  for RDF(s)

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

Page 30: SPARQL- A  query language  for RDF(s)

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.

Page 31: SPARQL- A  query language  for RDF(s)

RDF DATASET- THE RELATIONSHIP BETWEEN NAMED AND BACKGROUND GRAPHS (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 32: SPARQL- A  query language  for RDF(s)

RDF DATASET- THE RELATIONSHIP BETWEEN NAMED AND BACKGROUND GRAPHS (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 33: SPARQL- A  query language  for RDF(s)

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 34: SPARQL- A  query language  for RDF(s)

QUERYING THE DATASET - 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 35: SPARQL- A  query language  for RDF(s)

QUERYING THE DATASET - 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 36: SPARQL- A  query language  for RDF(s)

QUERYING THE DATASET - 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 37: SPARQL- A  query language  for RDF(s)

CONSTRUCTING AN OUTPUT GRAPH

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

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

Page 38: SPARQL- A  query language  for RDF(s)

ASK – A BOOLEAN QUERY

@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:homepage <http://work.example.org/alice/> .

_:b foaf:name "Bob" .

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

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

ASK { ?x foaf:name "Alice" }

TRUE.

Page 39: SPARQL- A  query language  for RDF(s)

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

Page 40: SPARQL- A  query language  for RDF(s)

40

ANY QUESTİONS?