58
SPARQL Intro: A query language for RDF Alexandra Cristea 1

Alexandra Cristea 1. pronounced "sparkle“ recursive acronym for: ◦ SPARQL Protocol and RDF Query Language a semantic query language a query language

Embed Size (px)

Citation preview

Page 1: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

SPARQL Intro:A query language for RDF

Alexandra Cristea

1

Page 2: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

pronounced "sparkle“ recursive acronym for:

◦ SPARQL Protocol and RDF Query Language a semantic query language a query language for RDF(S) W3C Recommendation 15th January 2008

◦ provides a standard format for writing queries that target RDF data ◦ and a set of standard rules for processing queries and returning the results

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

2

What is SPARQL

Page 3: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

New version: 21st of March 2013: http://www.w3.org/TR/sparql11-overview/ Query language at: http://www.w3.org/TR/sparql11-query/ Implemented in various programming

languages

3

SPARQL now

Page 4: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

4

RDF Statements

SubjectPredicate

author http://www.w3schools.com/RDF

Object

Jan Egil Refsnes

SPARQL searches for all sub-graphs that match the graph described by the triples in the query.

?subject ?predicate ?object

Page 5: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

SELECT ?studentWHERE { ?student b:studies bmod:CS414 }

~ SQL – why?Which are the namespace prefixes?

5

A sample of SPARQL

Page 6: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

In RDF:<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-

syntax-ns#" xmlns:cd="http://www.recshop.fake/cd#">

In SPARQL:PREFIX b: <http://...>

PREFIX bmod: <http://www2.warwick.ac.uk/fac/sci/dcs/teaching/material/>

The PREFIX keyword is SPARQL’s version of an xmlns:namespace declaration and works in basically the same way.

6

Prefixes & namespaces

Page 7: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

Prefix IRI

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

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

xsd: http://www.w3.org/2001/XMLSchema#

fn: http://www.w3.org/2005/xpath-functions#

sfn: http://www.w3.org/ns/sparql#

7

Frequently used namespaces

Prefixes are not optional in SPARQL!

Page 8: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

Capitals of Europe: http://liris.cnrs.fr/~pchampin/spark/gmapv3.html

8

Dbpedia + Google maps mashup

Page 9: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

http://dbpedia.org/sparql http://dbpedia.org/isparql/ http://dbpedia.org/snorql/ http://querybuilder.dbpedia.org/index.php

9

DbPedia Query points

Page 10: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

http://dbpedia.org/resource/The_Lord_of_the_Rings http://dbpedia.org/resource/Berlin http://dbpedia.org/resource/Category:Cities_in_Englan

d http://dbpedia.org/resource/The_Beatles - Fluidops http://dbpedia.org/resource/Paul_McCartney http://dbpedia.org/resource/Category:English_musician

s http://dbpedia.org/resource/Semantic_Web http://dbpedia.org/resource/Tetris http://dbpedia.org/resource/SPARQL

10

Sample Resources

Page 11: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

OpenLink Query point◦ http://demo.openlinksw.com/sparql

Semanticweb.org data:◦ http://data.semanticweb.org/snorql/

Diseases:◦ http://wifo5-03.informatik.uni-mannheim.de/diseasome/snorql/

Country statistics:◦ http://wifo5-03.informatik.uni-mannheim.de/factbook/snorql/

Nobelprize info:◦ http://data.nobelprize.org/snorql/

W3C’s list of endpoints:◦ http://www.w3.org/wiki/SparqlEndpoints

11

Other SPARQL query points

Page 12: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

http://www.sparql.org/query.html http://sparql.org/sparql.html http://demo.openlinksw.com/sparql

◦ make sure to allow at ‘Sponging’ to ‘Retrieve remote RDF data for all missing source graphs’

http://librdf.org/query

12

Online SPARQL queries

Page 13: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

Different syntax from XMLVariables: begin with ? or $ triples patterns enclosed within braces {}Result: variables after SELECT (~SQL)

13

SPARQL basics

Page 14: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

PREFIX foo: PREFIX bar: SELECT … FROM … WHERE {…} GROUP BY … HAVING … ORDER BY … LIMIT … OFFSET … VALUES …

14

Anatomy of a Query

~ SQL

Page 15: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

SELECT queries CONSTRUCT queries ASK queries DESCRIBE queries

15

4 Types of SPARQL Queries

Page 16: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

PREFIX foaf: <http://xmlns.com/foaf/0.1/>SELECT ?x ?y ?zFROM <http://www.w3.org/People/Berners-Lee/card>WHERE { ?x ?y ?z }

16

PREFIX SELECT FROM WHERE

Page 17: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

PREFIX b: <http://www2.warwick.ac.uk/rdf/>PREFIX bmod:

<http://www2.warwick.ac.uk/fac/sci/dcs/teaching/material/>

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

SELECT ?name

WHERE { ?student b:studies bmod:CS328 .

?student foaf:name ?name }

17

Combining conditions

Page 18: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

FOAF (Friend Of A Friend) experimental project using RDF, defining a

standardised vocabulary. Goal: make personal homepages machine-

readable & understandable, create an internet-wide connected database of people.

Defined at:http://xmlns.com/foaf/0.1/

18

FOAF

Page 19: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

Idea: most personal homepages contain similar info.◦E.g, person’s name, living place, work place, details

on work of the moment, links to friends.◦define RDF predicates to represent them. ◦So: Pages: understood/ manipulated by computers.

So a db can be queried for: ◦“what projects are my friends working on?”, ◦“do any of my friends know the director of

BigCorp?” etc..

19

FOAF

Page 20: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-

ns#" xmlns:foaf="http://xmlns.com/foaf/0.1#"> <foaf:Person> <foaf:name>Alexandra I. Cristea</foaf:name> <foaf:mbox rdf:resource="mailto:[email protected]"/> </foaf:Person> </rdf:RDF>

20

A sample FOAF RDF document

Page 21: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:foaf="http://xmlns.com/foaf/0.1/"> <foaf:Person> <foaf:name>Peter Brusilovsky</foaf:name> <foaf:firstName>Peter</foaf:firstName> <foaf:surname>Brusilovsky</foaf:surname> <foaf:mbox_sha1sum>1fa1a097b3fae4594fa0ec18e2e3da11ac7e0349</foaf:mbox_sha1sum> <foaf:knows> <foaf:Person> <foaf:name>Alexandra I. Cristea</foaf:name> <foaf:mbox_sha1sum>dba246a0d9ec4a6238198f9a035a7a608ff227f8</foaf:mbox_sha1sum> <rdfs:seeAlso rdf:resource="http://www.dcs.warwick.ac.uk/~acristea/"/> </foaf:Person> </foaf:knows> </foaf:Person> </rdf:RDF>

21

FOAF: another example

Page 22: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

PREFIX b: <http://www2.warwick.ac.uk/rdf/>PREFIX bmod: <

http://www2.warwick.ac.uk/fac/sci/dcs/teaching/material/>PREFIX foaf:http://xmlns.com/foaf/0.1/

SELECT ?module ?nameWHERE { ?student b:studies ?module . ?student foaf:name ?name }

22

Extracting multiple results

Page 23: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

PREFIX b: <http://www2.warwick.ac.uk/rdf/>PREFIX bmod: <

http://www2.warwick.ac.uk/fac/sci/dcs/teaching/material/ >

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

SELECT ?module ?name

WHERE { ?student b:studies ?module ; foaf:name ?name }

23

Using the same subject

Page 24: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

SELECT ?module ?nameWHERE { ?student b:studies ?module . ?student b:studies bmod:CS328 ; foaf:name ?name }

is identical to:

SELECT ?module ?name

WHERE { ?student b:studies ?module , bmod:CS328 ;

foaf:name ?name }

24

Abbreviating multiple objects

Page 25: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

SELECT ?student ?emailWHERE { ?student b:studies mod:CS328 . ?student foaf:mbox ?email }

PB: if a student does not have an e-mail address registered, with a foaf:mbox predicate, then the query will not match !!

25

Optional graph components

Page 26: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

SELECT ?student ?emailWHERE { ?student b:studies mod:CS328 .OPTIONAL { ?student foaf:mbox ?email } }

OPTIONAL: match it if it can, but otherwise not reject the overall pattern.

26

Optional graph components

Page 27: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

SPARQL basics SPARQL endpoints: dbpedia, foaf, others SPARQL type of queries:

◦ SELECT; CONSTRUCT; ASK; DESCRIBE SQL-like constructs:

◦ SELECT; FROM; WHERE; GROUP BY; HAVING; ORDER BY; LIMIT; OFFSET; VALUES

Some specific constructs:◦ PREFIX not optional; FROM not always used; OPTIONAL◦ We are going to look at more specific constructs

27

We have studied:

Page 28: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

Try out at: http://www.sparql.org/query.html

PREFIX books: <http://example.org/book/>PREFIX dc: <http://purl.org/dc/elements/1.1/>SELECT $book WHERE {$book title $title } >> this gives an empty answer/ error.

Versus:PREFIX books: <http://example.org/book/>PREFIX dc: <http://purl.org/dc/elements/1.1/>SELECT $book WHERE {$book dc:title $title } 28

Prefixes are not optional

Page 29: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

try out at : http://www.sparql.org/query.htmlWith or without optionalPREFIX books: <http://example.org/book/>PREFIX dc: <http://purl.org/dc/elements/1.1/>SELECT ?book ?author ?title ?dateWHERE { ?book dc:creator ?author . ?book dc:title ?title . ?book dc:date ?date }

PREFIX books: <http://example.org/book/>PREFIX dc: <http://purl.org/dc/elements/1.1/>SELECT ?book ?author ?title ?dateWHERE { ?book dc:creator ?author . ?book dc:title ?title . OPTIONAL { ?book

dc:date ?date } } 29

OPTIONAL query example

Page 30: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

Try analysing RDF documents e.g.: http://athena.ics.forth.gr:9090/RDF/VRP/Examples/tap.rdf (on http://sparql.org/sparql.html or Protégé, or specific endpoints) Using SPARQL to find Concepts endpoints Dbpedia SNORQL query explorer (http://dbpedia.org/snorql/),

the BBC Backstage SPARQL Editor (http://bbc.openlinksw.com/sparql) or GeoSparql (http://geosparql.org/) to analyse content.

Listing DISTINCT RDF Types The following query lists the distinct rdf:types used in a dataset PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>SELECT DISTINCT ?typeWHERE { ?s rdf:type ?type } Listing OWL ClassesPREFIX owl: <http://www.w3.org/2002/07/owl#>SELECT ?classWHERE { ?class a owl:Class } 30

Analysing documents

Page 31: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

SELECT ?module ?name ?phoneWHERE { ?student b:studies ?module . ?student foaf:name ?name .OPTIONAL { ?student b:contactpermission true .?student b:phone ?phone} }

SELECT ?module ?name ?ageWHERE { ?student b:studies ?module . ?student foaf:name ?name .OPTIONAL { ?student b:age ?age . FILTER (?age > 25) } }

31

More optional graph components …and filters:

Page 32: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

SELECT ?student ?email ?homeWHERE { ?student b:studies mod:CS328 .OPTIONAL { ?student foaf:mbox ?email . ?student foaf:homepage ?home } }

SELECT ?student ?email ?homeWHERE { ?student b:studies mod:CS328 .OPTIONAL { ?student foaf:mbox ?email } .OPTIONAL { ?student foaf:homepage ?home } }

32

Further optional examples …

Page 33: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

SELECT ?studentWHERE { ?student foaf:mbox ?email . { ?student b:studies mod:CS328 } UNION { ?student b:studies mod:CS909 } }

When patterns are combined using the UNION keyword, the resulting combined pattern will match if any of the subpatterns is matched.

33

Combining matches

Page 34: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

Consider A and B as graph patterns. { A } UNION { B }

◦ Either / or graph patterns◦ Disjunction; include both the results of solving A and the

results of solving B { A } MINUS { B }

◦ “subtracted” graph patterns (SPARQL 1.1)◦ Negation; Solve A; Solve B; Include only those results from A

that are not compatible with any of the results from B.

34

Combining SPARQL Graph Patterns

Page 35: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

All the previous queries: single RDF graphs. Often: multiple RDF graphs. RDF graphs: identified by IRI.

◦ Note: IRI that represents the graph does not have to be the actual IRI of the graph file

◦ although the program processing the query will need to somehow relate the IRI to an actual RDF graph stored somewhere.

35

Multiple graphs and the dataset

Page 36: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

SELECT ?student ?email ?homeFROM <http://www2.warwick.ac.uk/rdf/student>WHERE { ?student b:studies mod:CS414 . OPTIONAL { ?student foaf:mbox ?email . ?student foaf:homepage ?home } }

By using several FROM declarations, you can combine several graphs in the dataset:

SELECT ?student ?email ?homeFROM <http://www2.warwick.ac.uk/rdf/student>FROM <http://www2.warwick.ac.uk/rdf/foaf>WHERE { ?student b:studies mod:CS414 . OPTIONAL { ?student foaf:mbox ?email . ?student foaf:homepage ?home } }

36

Stating the dataset

Page 37: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

one (optional) default graph + any number of named graphs. FROM specifies default graph.

◦Many FROM keywords, the graphs are merged into the default graph.

Additionally: named graphs, w FROM NAMED.◦However, to match patterns you must use GRAPH

keyword to state which graph !

37

Multiple graphs

Page 38: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

SELECT ?student ?email ?homeFROM NAMED <http://www2.warwick.ac.uk/rdf/student>FROM NAMED<http://www2.warwick.ac.uk/rdf/foaf>

WHERE { GRAPH <http://www2.warwick.ac.uk/rdf/student> { ?student b:studies mod:CS909 } . GRAPH <http://www2.warwick.ac.uk/rdf/foaf> { OPTIONAL { ?student foaf:mbox ?email . ?student foaf:homepage ?home } } }

38

Examples of named graphs

Page 39: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

PREFIX brdf: <http://www2.warwick.ac.uk/rdf/>SELECT ?student ?email ?homeFROM NAMED <http://www2.warwick.ac.uk/rdf/student>FROM NAMED <http://www2.warwick.ac.uk/rdf/foaf>

WHERE { GRAPH brdf:student { ?student b:studies mod:CS909 } . GRAPH brdf:foaf{ OPTIONAL { ?student foaf:mbox ?email . ?student foaf:homepage ?home } } }

39

Abbreviation using prefixes

Page 40: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

PREFIX brdf: <http://www2.warwick.ac.uk/rdf/>SELECT ?student ?email ?homeFROM <http://www2.warwickac.uk/rdf/student>FROM NAMED <http://www2.warwickac.uk/rdf/foaf>

WHERE { ?student b:studies mod:CS909 . GRAPH brdf:foaf{ OPTIONAL { ?studentfoaf:mbox?email . ?studentfoaf:homepage ?

home } } } 40

Using named and default graph together

Page 41: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

the GRAPH can also be a variable.◦to query which graph in the dataset holds a

particular relationship, ◦or which graph to search based on data in

another graph.

not mandatory to declare all graphs◦even if specified, the dataset can be

overridden on a per-query basis.

41

Graph as a query

Page 42: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

PREFIX brdf: <http://www2.warwick.ac.uk/rdf/>SELECT ?student ?graphWHERE { ?student b:studies mod:CS909 . GRAPH ?graph { ?student foaf:mbox ?email } }

output var graph holds graph URL (matching student to e-mail address).

Presumption: query processor has knowledge of a finite set of graphs + their locations

42

Which graph is it in?

Page 43: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

PREFIX brdf: <http://www2.warwickac.uk/rdf/>SELECT ?student ?emailWHERE { ?student b:studies mod:CS909 . ?student rdfs:seeAlso ?graph . GRAPH ?graph { ?student foaf:mbox ?email } } Note: if student doesn’t have a rdfs:seeAlso property

which points to a graph holding their e-mail address, they will not appear in the result at all.

43

Re-using the graph reference

Page 44: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

Specific SPARQL constructs: ◦ Revisited OPTIONAL, PREFIX, FROM - single◦ New ones: FILTER, UNION, MINUS, FROM –multiple,

FROM NAMED, GRAPH, GRAPH with prefix, GRAPH as (query-able + reusable) variable

More online/ offline queries:◦ Endpoints◦ Sparql.com◦ Protégé

Next: other SQL-like constructs; non-SQL-like behaviour: ASK, DESCRIBE, CONSTRUCT

44

We have learned:

Page 45: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

SELECT ?name ?moduleWHERE { ?student b:studies ?module . ?student foaf:name ?name }ORDER BY ?name

SELECT ?name ?ageWHERE { ?student b:age ?age . ?student foaf:name ?name }ORDER BY DESC (?age) ASC (?name)

45

Sorting results of a query

Page 46: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

SELECT ?name ?moduleWHERE { ?student b:studies ?module .?student foaf:name ?name }LIMIT 20

46

Limiting the number of results

Page 47: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

SELECT ?name ?moduleWHERE { ?student b:studies ?module . ?student foaf:name ?name }ORDER BY ?nameOFFSET 10LIMIT 20

47

Extracting subsets of the results

Page 48: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

SELECT ?studentWHERE { ?student b:studies ?module }

versus:

SELECT DISTINCT ?studentWHERE { ?student b:studies ?module }

48

Obtaining unique results

Page 49: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

Is any student studying any module?ASK { ?student b:studies ?module }

Is any student studying CS909?ASK { ?student b:studies bmod:CS909 }

Is student 029389 studying CS909?ASK { bstu:029389 b:studies bmod:CS909 }

Is anyone whom 029389 knows, studying CS909?ASK {bstu:029389 foaf:knows ?x .

?x b:studies bmod: CS909 } Is any student aged over 30 studying CS909? ASK { ?student b:studies bmod:CS909 . ?student b:age ?age . FILTER { ?age > 30 } }

49

Obtaining a Boolean result

Page 50: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

CONSTRUCT { ?student b:studyFriend ?friend }WHERE { ?student b:studies ?module . ?student foaf:knows ?friend . ?friend b:studies ?module } }

If there is more than one search result, the triples from each result are combined.

50

Constructing an RDF result

Page 51: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

DESCRIBE ?country

Result is in RDF triples, in any RDF serialization

51

DESCRIBE queries

Page 52: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

52

Page 53: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

53

Page 54: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

54

Page 55: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

[1] Dean Allemang & Jim Hendler. 2008 Semantic Web for the working ontologist. Morgan Kaufmann publishers. ISBN 978-0-12-373556-0

[2] Eric Prud'hommeaux ; & Andy Seaborne . 2008 SPARQL Query Language for RDF , [Online] http://www.w3.org/TR/rdf-sparql-query/

55

References

Page 56: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

Download Jena from http://jena.sourceforge.net/ Protégé 3.4 and above

http://protege.stanford.edu/ Fuseki:

http://jena.apache.org/documentation/serving_data/index.html

56

Tools to process

Page 57: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

SPARQL language is used for querying RDF. SPARQL is not based on XML, but on SQL-

like syntax. Building blocks of SPARQL queries are graph

patterns that include variables. The result of the query will be the values the vars must take to match the RDF graph.

A SPARQL query can return results in several different ways, as determined by the query.

SPARQL queries can also be used for OWL querying.

57

Summary

Page 58: Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language

58

Questions?