sparql - free university of bozen-bolzanonutt/teaching/semtechs1415/semtechsslides/5-sp… ·...

64
Master Informatique 1 Semantic Technologies Part 5 SPARQL SPARQL Werner Nutt

Upload: others

Post on 11-Oct-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    1 Semantic Technologies

Part  5   SPARQL  

SPARQL

Werner Nutt

Page 2: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    2 Semantic Technologies

Part  5   SPARQL  

Acknowledgment These slides are based on the slide set •  Jena

By Mariano Rodriguez (see http://www.slideshare.net/marianomx) (CC Attribution Share Alike 3.0 License)

which are based on the slide set •  An introduction to SPARQL and Queries over Linked Data.

Chapter 2 SPARQL. By Olaf Hartig (CC Attribution Share Alike 3.0 License)

License •  This work is licensed under a

Creative Commons Attribution-Share Alike 3.0 License (http://creativecommons.org/licenses/by-sa/3.0/)

Page 3: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    3 Semantic Technologies

Part  5   SPARQL  

SPARQL in General •  A family of W3C recommendations •  SPARQL Query

–  Declarative query language for RDF data •  SPARQL Update

–  Declarative update language for RDF data •  SPARQL Protocol

–  Communication between SPARQL processing services (a.k.a. SPARQL endpoints) and clients

•  SPARQL Query Results XML Format –  XML Format for serializing query results

Page 4: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    4 Semantic Technologies

Part  5   SPARQL  

Idea of SPARQL queries •  Pattern matching

–  Describe subgraphs of the queried RDF graph –  Subgraphs that match your description

contribute an answer –  Building blocks: graph patterns

(i.e. RDF graphs with variables)

Page 5: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    5 Semantic Technologies

Part  5   SPARQL  

Idea of SPARQL queries

?v dbpedia:Mount_Baker dbpedia:Mount_Etna

Page 6: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    6 Semantic Technologies

Part  5   SPARQL  

Components of SPARQL Queries

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX umbel-sc: <http://umbel.org/umbel/sc/> SELECT ?v FROM <http://example.org/myGeoData> WHERE { ?v rdf:type umbel-sc:Volcano . } ORDER BY ?name

Page 7: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    7 Semantic Technologies

Part  5   SPARQL  

Components of SPARQL Queries

•  Prologue –  Prefix definitions for compact URIs –  Attention (difference w.r.t. Turtle):

No period (“.”) character as separator

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX umbel-sc: <http://umbel.org/umbel/sc/> SELECT ?v FROM <http://example.org/myGeoData> WHERE { ?v rdf:type umbel-sc:Volcano . } ORDER BY ?name

Page 8: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    8 Semantic Technologies

Part  5   SPARQL  

Components of SPARQL Queries

•  Result form specification –  SELECT, DESCRIBE, CONSTRUCT or ASK

(more later)

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX umbel-sc: <http://umbel.org/umbel/sc/> SELECT ?v FROM <http://example.org/myGeoData> WHERE { ?v rdf:type umbel-sc:Volcano . } ORDER BY ?name

Page 9: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    9 Semantic Technologies

Part  5   SPARQL  

Components of SPARQL Queries

•  Dataset specification –  Specify the RDF dataset to be queries

(more on that later)

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX umbel-sc: <http://umbel.org/umbel/sc/> SELECT ?v FROM <http://example.org/myGeoData> WHERE { ?v rdf:type umbel-sc:Volcano . } ORDER BY ?name

Page 10: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    10 Semantic Technologies

Part  5   SPARQL  

Components of SPARQL Queries

•  Query pattern –  WHERE clause specifies the graph pattern

to be matched

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX umbel-sc: <http://umbel.org/umbel/sc/> SELECT ?v FROM <http://example.org/myGeoData> WHERE { ?v rdf:type umbel-sc:Volcano . } ORDER BY ?name

Page 11: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    11 Semantic Technologies

Part  5   SPARQL  

Components of SPARQL Queries

•  Solution modifiers –  ORDER BY, LIMIT, OFFSET

(more about that later) –  Only available in some of the result forms

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX umbel-sc: <http://umbel.org/umbel/sc/> SELECT ?v FROM <http://example.org/myGeoData> WHERE { ?v rdf:type umbel-sc:Volcano . } ORDER BY ?name

Page 12: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    12 Semantic Technologies

Part  5   SPARQL  

Graph patterns •  Different types of graph patterns for the query pattern

(WHERE clause): –  Basic graph pattern (BGP) –  Group graph pattern –  Optional graph pattern –  Union graph pattern –  Named Graph pattern –  (Constraints)

Page 13: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    13 Semantic Technologies

Part  5   SPARQL  

Basic Graph Patterns •  Set of triple patterns in Turtle syntax

(i.e., RDF triples with variables) •  Variable names prefixed with “?” or “$”

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX umbel-sc: <http://umbel.org/umbel/sc/> SELECT ?name WHERE { ?v rdf:type umbel-sc:Volcano . ?v rdfs:label ?name }

Page 14: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    14 Semantic Technologies

Part  5   SPARQL  

Basic Graph Patterns •  Set of triple patterns in Turtle syntax

(i.e., RDF triples with variables) •  Variable names prefixed with “?” or “$”

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX umbel-sc: <http://umbel.org/umbel/sc/> SELECT ?name WHERE { ?v rdf:type umbel-sc:Volcano ; rdfs:label ?name }

Page 15: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    15 Semantic Technologies

Part  5   SPARQL  

Example

•  What are the names of all (known) volcanos?

SELECT ?name WHERE { ?v rdf:type umbel-sc:Volcano ; rdfs:label ?name }

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano. dbpedia:Beerenberg rdf:type umbel-sc:Volcano,

umbel-sc:NaturalElevation ; rdfs:label "Beerenberg"@en ; rdfs:label "Беренберг"@ru .

?name “Etna” "Беренберг"@ru "Beerenberg"@en

Page 16: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    16 Semantic Technologies

Part  5   SPARQL  

Example

•  List all types of the volcano called “Beerenberg”

SELECT ?type WHERE { ?v rdf:type ?type ; rdfs:label “Beerenberg” }

?type

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano. dbpedia:Beerenberg rdf:type umbel-sc:Volcano,

umbel-sc:NaturalElevation ; rdfs:label "Beerenberg"@en ; rdfs:label "Беренберг"@ru .

Page 17: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    17 Semantic Technologies

Part  5   SPARQL  

Example

•  List all types of the volcano called “Beerenberg”

SELECT ?type WHERE { ?v rdf:type ?type ; rdfs:label “Beerenberg”@en }

?type umbel-sc:Volcano umbel-sc:NaturalElevation

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano. dbpedia:Beerenberg rdf:type umbel-sc:Volcano,

umbel-sc:NaturalElevation ; rdfs:label "Beerenberg"@en ; rdfs:label "Беренберг"@ru .

Page 18: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    18 Semantic Technologies

Part  5   SPARQL  

Example

•  Where are all (known) volcanos located? (List the names of these locations)

SELECT ?name WHERE { _:x rdf:type umbel-sc:Volcano ; p:location [ rdfs:label ?name ] . }

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano; p:location dbpedia:United_States.

dbpedia:United_States rdfs:label “United States”

?name “United States”

n  Blank nodes in SPARQL queries n  As subject or object of triple patterns n  Non-selectable variables

Page 19: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    19 Semantic Technologies

Part  5   SPARQL  

Example

•  Blank nodes in the queried data –  Blank node identifiers may appear in the results

SELECT ?l ?name WHERE { ?v rdf:type umbel-sc:Volcano ; p:location ?l . ?l rdfs:label ?name . }

dbpedia:Mount_ Baker rdf:type umbel-sc:Volcano ; p:location [ rdfs:label "United States"@en , "États-Unis"@fr ] .

dbpedia:Mount_ Etna rdf:type umbel-sc:Volcano ; p:location [ rdfs:label "Italy" ] .

?l ?name _:n0 “United States”@en _:n0 "États-Unis"@fr _:n1 "Italy"

Page 20: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    20 Semantic Technologies

Part  5   SPARQL  

Optional Graph Patterns

•  What are all volcanos and their names?

SELECT ?v ?name WHERE { ?v rdf:type umbel-sc:Volcano ; rdfs:label ?name . }

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano. dbpedia:Beerenberg rdf:type umbel-sc:Volcano,

umbel-sc:NaturalElevation ; rdfs:label "Beerenberg"@en .

?v ?name dbpedia:Mount_Etna “Etna” dbpedia:Beerenberg "Beerenberg"@en

n  Problem: Missing Mount Baker (it has no name)

Page 21: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    21 Semantic Technologies

Part  5   SPARQL  

Optional Graph Patterns

SELECT ?v ?name WHERE { ?v rdf:type umbel-sc:Volcano . OPTIONAL { ?v rdfs:label ?name } }

?v ?name dbpedia:Mount_Etna “Etna” dbpedia:Mount_Baker dbpedia:Beerenber "Beerenberg"@en

n  Optional patterns may result in unbound variables

Page 22: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    22 Semantic Technologies

Part  5   SPARQL  

Union Graph Patterns

•  List all volcanos located in Italy or Norway

SELECT ?v WHERE { ?v rdf:type umbel-sc:Volcano; p:location ??????? }

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna” ; p:location dbpedia:Italy .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano ; p:location dbpedia:United_States.

dbpedia:Beerenberg rdf:type umbel-sc:Volcano, rdfs:label "Beerenberg"@en ; p:location dbpedia:Norway .

Page 23: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    23 Semantic Technologies

Part  5   SPARQL  

Union Graph Patterns

SELECT ?v WHERE { ?v rdf:type umbel-sc:Volcano. { ?v p:location dbpedia:Italy } UNION { ?v p:location dbpedia:Norway } }

SELECT ?v WHERE { { ?v rdf:type umbel-sc:Volcano; p:location dbpedia:Italy } UNION {?v rdf:type umbel-sc:Volcano; p:location dbpedia:Norway. } }

semantically equivalent

Page 24: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    24 Semantic Technologies

Part  5   SPARQL  

Union Graph Patterns

SELECT ?v WHERE { ?v rdf:type umbel-sc:Volcano. { ?v p:location dbpedia:Italy } UNION { ?v p:location dbpedia:Norway } }

SELECT ?v WHERE { { ?v rdf:type umbel-sc:Volcano. } { { ?v p:location dbpedia:Italy } UNION { ?v p:location dbpedia:Norway } } }

semantically equivalent

Page 25: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    25 Semantic Technologies

Part  5   SPARQL  

Constraints on Solutions •  Syntax: Keyword FILTER followed by a filter expression

•  Filter expressions are formed by operators and functions

•  Operators and functions operate on RDF terms

SELECT ?v WHERE { ?v rdf:type umbel-sc:Volcano ; p:lastEruption ?le FILTER ( ?le > 1900 ) }

Page 26: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    26 Semantic Technologies

Part  5   SPARQL  

Constraints (Truth Table) •  Filter expressions evaluate to true, false or error •  Truth table:

A B A || B A && B T T T T T F T F F T T F F F F F T E T E E T T E F E E F E F E F E E E E

Page 27: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    27 Semantic Technologies

Part  5   SPARQL  

Unary Operators in Constraints

Operator Type(A) Result type !A xsd:boolean xsd:boolean +A numeric numeric -A numeric numeric BOUND(A) variable xsd:boolean isURI(A) RDF term xsd:boolean isBLANK(A) RDF term xsd:boolean isLITERAL(A) RDF term xsd:boolean STR(A) literal / URI simple literal LANG(A) literal simple literal DATATYPE(A) literal simple literal

Page 28: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    28 Semantic Technologies

Part  5   SPARQL  

Example

•  List all types of the volcano called “Beerenberg”

SELECT ?type WHERE { ?v rdf:type ?type ; rdfs:label “Beerenberg” }

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano. dbpedia:Beerenberg rdf:type umbel-sc:Volcano,

umbel-sc:NaturalElevation ; rdfs:label "Beerenberg"@en ; rdfs:label "Беренберг"@ru .

?type

Page 29: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    29 Semantic Technologies

Part  5   SPARQL  

Example

•  List all types of the volcano called “Beerenberg”

SELECT ?type WHERE { ?v rdf:type ?type ; rdfs:label ?name . FILTER( STR(?name) = “Beerenberg) }

?type umbel-sc:Volcano umbel-sc:NaturalElevation

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano. dbpedia:Beerenberg rdf:type umbel-sc:Volcano,

umbel-sc:NaturalElevation ; rdfs:label "Beerenberg"@en ; rdfs:label "Беренберг"@ru .

Page 30: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    30 Semantic Technologies

Part  5   SPARQL  

Constraints (Further operators) •  Binary operators:

–  Logical connectives && and || for xsd:boolean –  Comparison operators =, !=, <, >, <=, and >= for numeric

datatypes, xsd:dateTime, xsd:string, and xsd:boolean –  Comparison operators = and != for other datatypes –  Arithmetic operators +, -, *, and / for numeric datatypes

•  Furthermore: –  REGEX(String,Pattern) or REGEX(String,Pattern,Flags) –  sameTERM(A,B) –  langMATCHES(A,B)

Page 31: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    31 Semantic Technologies

Part  5   SPARQL  

Example

•  List all types of the volcano with “e” in their name

SELECT ?v WHERE { ?v rdf:type umbel-sc:Volcano ; rdfs:label ?name . FILTER( REGEX(STR(?name), "e") ) }

?v dbpedia:Beerenberg dbpedia:Beerenberg

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano. dbpedia:Beerenberg rdf:type umbel-sc:Volcano,

umbel-sc:NaturalElevation ; rdfs:label "Beerenberg"@en ; rdfs:label "Беренберг"@ru .

Page 32: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    32 Semantic Technologies

Part  5   SPARQL  

Example

•  List all types of the volcano with “e” in their name (case insensitive)

SELECT ?v WHERE { ?v rdf:type umbel-sc:Volcano ; rdfs:label ?name . FILTER( REGEX(STR(?name), "e", "i") ) }

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano. dbpedia:Beerenberg rdf:type umbel-sc:Volcano,

umbel-sc:NaturalElevation ; rdfs:label "Beerenberg"@en ; rdfs:label "Беренберг"@ru .

?v dbpedia:Mount_Etna dbpedia:Beerenberg dbpedia:Beerenberg

Page 33: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    33 Semantic Technologies

Part  5   SPARQL  

Named Graph Patterns

•  SPARQL queries are executed over an RDF dataset: –  One default graph and –  Zero or more named graphs (identified by an URI)

dbpedia:Mount_Etna rdfs:seeAlso <http://example.org/d1> dbpedia:Mount_Baker rdfs:seeAlso <http://example.org/d2>

dbpedia:Beerenberg rdf:type umbel-sc:Volcano ; rdfs:label "Beerenberg"@en ;

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano.

Default graph

http://example.org/d3

http://example.org/d2

http://example.org/d1

Page 34: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    34 Semantic Technologies

Part  5   SPARQL  

Graphs in RDF •  Currently no official specification of named graphs in

RDF •  However, most RDF triple stores support them

–  Loading data into graphs in Jena, use “ng4j”

Page 35: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    35 Semantic Technologies

Part  5   SPARQL  

Named Graphs in Jena (Example)

// Create a new graphset NamedGraphSet graphset = new NamedGraphSetImpl(); NamedGraph graph = graphset.createGraph("http://example.org/persons/123"); // Add information to the NamedGraph graph.add(new Triple(Node.createURI("http://fariz.darari.it/� foaf.rdf#FarizDarari"), Node.createURI("http://xmlns.com/foaf/0.1/name") , Node.createLiteral("FarizDarari", null, null))); // Create a quad Quad quad = new Quad(Node.createURI("http://www.werner.de/

InformationAboutFariz"), Node.createURI("http://fariz.darari.it/foaf.rdf#FarizDarari"), Node.createURI("http://xmlns.com/foaf/0.1/mbox") , Node.createURI("mailto:[email protected]")); // Add the quad to the graphset. This will create a new NamedGraph in the // graphset. graphset.addQuad(quad);

Page 36: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    36 Semantic Technologies

Part  5   SPARQL  

Components of SPARQL Queries

•  Dataset specification –  Specify the RDF dataset to be queried

(more on that later) •  Specification using FROM and FROM NAMED

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX umbel-sc: <http://umbel.org/umbel/sc/> SELECT ?v FROM <http://example.org/myGeoData> WHERE { ?v rdf:type umbel-sc:Volcano . } ORDER BY ?name

Page 37: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    37 Semantic Technologies

Part  5   SPARQL  

Named Graph Patterns

•  Evaluation of patterns w.r.t. the active graph •  GRAPH clause for making a named graph

the active graph

dbpedia:Mount_Etna rdfs:seeAlso <http://example.org/d1> dbpedia:Mount_Baker rdfs:seeAlso <http://example.org/d2>

dbpedia:Beerenberg rdf:type umbel-sc:Volcano ; rdfs:label "Beerenberg"@en ;

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano.

Default graph

http://example.org/d3

http://example.org/d2

http://example.org/d1

Page 38: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    38 Semantic Technologies

Part  5   SPARQL  

Named Graph Patterns

SELECT ?v WHERE { GRAPH ?g { ?v rdf:type umbel-sc:Volcano } }

?v dbpedia:Mount_Etna dbpedia:Mount_Baker dbpedia:Beerenberg

dbpedia:Mount_Etna rdfs:seeAlso <http://example.org/d1> dbpedia:Mount_Baker rdfs:seeAlso <http://example.org/d2>

dbpedia:Beerenberg rdf:type umbel-sc:Volcano ; rdfs:label "Beerenberg"@en ;

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano.

Default graph

http://example.org/d3

http://example.org/d2

http://example.org/d1

Page 39: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    39 Semantic Technologies

Part  5   SPARQL  

Named Graph Patterns

dbpedia:Mount_Etna rdfs:seeAlso <http://example.org/d1> dbpedia:Mount_Baker rdfs:seeAlso <http://example.org/d2>

dbpedia:Beerenberg rdf:type umbel-sc:Volcano ; rdfs:label "Beerenberg"@en ;

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano.

Default graph

http://example.org/d3

http://example.org/d2

http://example.org/d1

SELECT ?v ?g WHERE { GRAPH ?g { ?v rdf:type umbel-sc:Volcano } }

?v ?g dbpedia:Mount_Etna <http://example.org/d1> dbpedia:Mount_Baker <http://example.org/d2> dbpedia:Beerenberg <http://example.org/d3>

Page 40: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    40 Semantic Technologies

Part  5   SPARQL  

Named Graph Patterns

SELECT ?v WHERE { _:x rdfs:seeAlso ?g GRAPH ?g { ?v rdf:type umbel-sc:Volcano } }

?v dbpedia:Mount_Etna dbpedia:Mount_Baker

dbpedia:Mount_Etna rdfs:seeAlso <http://example.org/d1> dbpedia:Mount_Baker rdfs:seeAlso <http://example.org/d2>

dbpedia:Beerenberg rdf:type umbel-sc:Volcano ; rdfs:label "Beerenberg"@en ;

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano.

Default graph

http://example.org/d3

http://example.org/d2

http://example.org/d1

Page 41: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    41 Semantic Technologies

Part  5   SPARQL  

Negation

•  What volcanos do not have a name in our data?

SELECT ?v WHERE { ?v rdf:type umbel-sc:Volcano . OPTIONAL { ?v rdfs:label ?name } FILTER ( ! BOUND(?name) ) }

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano. dbpedia:Beerenberg rdf:type umbel-sc:Volcano,

umbel-sc:NaturalElevation ; rdfs:label "Beerenberg"@en ; rdfs:label "Беренберг"@ru .

?v dbpedia:Mount_Baker

Negation as failure

Page 42: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    42 Semantic Technologies

Part  5   SPARQL  

Negation

•  What volcanos are not called “Beerenberg”?

SELECT ?v WHERE { ?v rdf:type umbel-sc:Volcano ; rdfs:label ?name . FILTER ( STR(?name) != “Beerenberg” ) } }

?name dbpedia:Mount_Etna dbpedia:Beerenberg

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano. dbpedia:Beerenberg rdf:type umbel-sc:Volcano,

umbel-sc:NaturalElevation ; rdfs:label "Beerenberg"@en ; rdfs:label "Беренберг"@ru .

Page 43: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    43 Semantic Technologies

Part  5   SPARQL  

Negation

•  What volcanos are not called “Beerenberg”?

SELECT ?v WHERE { ?v rdf:type umbel-sc:Volcano . OPTIONAL { ?v rdfs:label ?name . FILTER ( STR(?name) = “Beerenberg” )

} FILTER ( ! BOUND (?name) ) }

?name dbpedia:Mount_Etna dbpedia:Mount_Baker

Negation as failure

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano. dbpedia:Beerenberg rdf:type umbel-sc:Volcano,

umbel-sc:NaturalElevation ; rdfs:label "Beerenberg"@en ; rdfs:label "Беренберг"@ru .

Page 44: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    44 Semantic Technologies

Part  5   SPARQL  

Named Graph Patterns and Negation

•  Which named graphs contain the name of a volcano that is not referenced in the default graph

dbpedia:Mount_Etna rdfs:seeAlso <http://example.org/d1> dbpedia:Mount_Baker rdfs:seeAlso <http://example.org/d2>

dbpedia:Beerenberg rdf:type umbel-sc:Volcano ; rdfs:label "Beerenberg"@en ;

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano.

Default graph

http://example.org/d3

http://example.org/d2

http://example.org/d1

SELECT ?g WHERE { GRAPH ?g { ?v rdf:type umble-sc:Volcano ;

rdfs:label ?name . } OPTIONAL { ?v rdfs:seeAlso ?r } FILTER ( ! BOUND(?r) ) }

Page 45: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    45 Semantic Technologies

Part  5   SPARQL  

Summary – Graph Patterns •  Different types of graph patterns for the query

pattern (WHERE clause): –  Basic graph pattern (BGP) –  Group graph pattern –  Optional graph pattern – keyword OPTIONAL –  Union graph pattern – keyword UNION –  Named Graph pattern – keyword GRAPH –  Constraints – keyword FILTER

Page 46: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    46 Semantic Technologies

Part  5   SPARQL  

Components of SPARQL Queries

•  Result form specification –  SELECT, DESCRIBE, CONSTRUCT or ASK

(more later)

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX umbel-sc: <http://umbel.org/umbel/sc/> SELECT ?v FROM <http://example.org/myGeoData> WHERE { ?v rdf:type umbel-sc:Volcano . } ORDER BY ?name

Page 47: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    47 Semantic Technologies

Part  5   SPARQL  

Result Forms •  SELECT

–  Result: sequence of solutions (i.e. sets of variable bindings)

–  Selected variables separated by space (not by comma!)

–  Asterisk character (“*”) selects all variables in the pattern

•  ASK –  Check whether there is at least one result –  Result: true or false –  Example: Do we have data about volcanos?

ASK WHERE { ?v rdf:type umble-sc:Volcano }

Page 48: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    48 Semantic Technologies

Part  5   SPARQL  

Result Forms •  DESCRIBE

–  Result: an RDF graph with data about resources –  Non-deterministic (i.e. query processor defines the

actual structure of the resulting RDF graph) –  Example: just name the resource

–  Possible to specify the resource by query pattern DESCRIBE <http://dbpedia.org/resource/Beerenberg>

DESCRIBE ?v WHERE { ?v rdf:type umbel-sc:Volcano .

}

Page 49: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    49 Semantic Technologies

Part  5   SPARQL  

Result Forms •  CONSTRUCT

–  Result: an RDF graph constructed from a template –  Template: graph pattern with variables from the query

pattern

CONSTRUCT { ?v rdfs:label ?name ; rdf:type myTypes:VolcanosOutsideTheUS } WHERE { ?v rdf:type umbel-sc:Volcano ; rdfs:label ?name . OPTIONAL { ?v p:location ?l FILTER ( ?l = dbpedia:United_States ) } FILTER ( ! BOUND(?l) ) }

Page 50: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    50 Semantic Technologies

Part  5   SPARQL  

Result Forms

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna” ; p:location dbpedia:Italy .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano ; rdfs:label "Mount Baker" ; p:location dbpedia:United_States.

dbpedia:Beerenberg rdf:type umbel-sc:Volcano, rdfs:label "Beerenberg"@en ; p:location dbpedia:Norway .

dbpedia:Mount_Etna rdfs:label "Etna” ; rdf:type myTypes:VolcanosOutsideTheUS

dbpedia:Beerenberg rdfs:label "Beerenberg"@en ; rdf:type myTypes:VolcanosOutsideTheUS

Result

Data

Page 51: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    51 Semantic Technologies

Part  5   SPARQL  

Solution Modifiers

•  Solution modifiers –  Only for SELECT queries –  Modify the result set as a whole (not single solutions) –  Keywords: DISTINCT, ORDER BY, LIMIT, OFFSET

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX umbel-sc: <http://umbel.org/umbel/sc/> SELECT ?v FROM <http://example.org/myGeoData> WHERE { ?v rdf:type umbel-sc:Volcano . } ORDER BY ?name

Page 52: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    52 Semantic Technologies

Part  5   SPARQL  

Solution Modifiers

•  DISTINCT removes duplicates from the result set

SELECT ?type WHERE { _:x rdf:type ?type }

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano. dbpedia:Beerenberg rdf:type umbel-sc:Volcano,

umbel-sc:NaturalElevation ; rdfs:label "Beerenberg"@en ; rdfs:label "Беренберг"@ru .

?name umbel-sc:Volcano umbel-sc:Volcano umbel-sc:NaturalElevation umbel-sc:Volcano

Page 53: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    53 Semantic Technologies

Part  5   SPARQL  

Solution Modifiers

•  DISTINCT removes duplicates from the result set

SELECT DISTINCT ?type WHERE { _:x rdf:type ?type }

?name umbel-sc:Volcano umbel-sc:NaturalElevation

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano. dbpedia:Beerenberg rdf:type umbel-sc:Volcano,

umbel-sc:NaturalElevation ; rdfs:label "Beerenberg"@en ; rdfs:label "Беренберг"@ru .

Page 54: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    54 Semantic Technologies

Part  5   SPARQL  

Solution Modifiers •  ORDER BY orders the results

•  How do you order different kinds of elements? –  unbound < blank node < URI < literal

•  ASC for ascending (default) and DESC for descending

•  Hierarchical order criteria

SELECT ?v WHERE { ?v rdf:type umbel-sc:Volcano ; rdfs:label ?name } ORDER BY ?name

SELECT ?name WHERE { ?v rdf:type umbel-sc:Volcano ; p:lastEruption ?le ; rdfs:label ?name }

ORDER BY DESC(?le), ?name

Page 55: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    55 Semantic Technologies

Part  5   SPARQL  

Solution Modifiers

•  LIMIT – limits the number of result

•  OFFSET – position/index of the first reported results

•  Order of the result should be predictable (combine with ORDER BY)

SELECT ?v WHERE { ?v rdf:type umbel-sc:Volcano ; rdfs:label ?name } ORDER BY ?name LIMIT 5

SELECT ?v WHERE { ?v rdf:type umbel-sc:Volcano ; rdfs:label ?name } ORDER BY ?name LIMIT 5 OFFSET 10

Page 56: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    56 Semantic Technologies

Part  5   SPARQL  

Exercise Consider the example data set: !@prefix ex: <http://eg.org/> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . !ex:Hamlet ! !ex:author !ex:Shakespeare ;

! ! !ex:price !"10.50"^^xsd:decimal . ex:Macbeth ! !ex:author !ex:Shakespeare . ex:Tamburlaine !ex:author !ex:Marlowe ;

! ! !ex:price !"17"^^xsd:integer . ex:DoctorFaustus ex:author !ex:Marlowe ;

! ! !ex:price !"12"^^xsd:integer ; ; !ex:title "The Tragical History of Doctor Faustus" .

ex:RomeusJuliet !ex:author !ex:Brooke ; ! ! !ex:price !”12"^^xsd:integer .!!

Page 57: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    57 Semantic Technologies

Part  5   SPARQL  

Exercise Formulate SPARQL queries for the vocabulary in the data set: •  Who are the authors of some book? •  Who are the authors of a book that costs less than 15? •  Who are the authors of a book that costs less than 15, and what are

the titles, if available? •  Which is the most expensive book? •  Which is the most expensive book by Shakespeare?

Page 58: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    58 Semantic Technologies

Part  5   SPARQL  

SPARQL in Jena •  What components would you expect in a framework like

Jena?

Page 59: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    59 Semantic Technologies

Part  5   SPARQL  

SPARQL in Jena •  SPARQL functionality bundled with Jena has separate

Javadocs: !http://jena.apache.org/documentation/javadoc/arq/ !

•  Main classes in package com.hp.hpl.jena.query –  Query a SPARQL query –  QueryFactory for creating queries in various ways –  QueryExecution for the execution state of a query –  QueryExecutionFactory for creating query executions –  ResultSet for results of a SELECT

•  CONSTRUCT and DESCRIBE return Models, ASK a Java boolean.

•  SPARQL with ARQ is generally slow because queries are broken down into triple patterns.

Page 60: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    60 Semantic Technologies

Part  5   SPARQL  

Constructing a Query and a QueryExecution

Query objects are usually constructed by parsing: String qStr =

"PREFIX foaf: <" + foafNS + ">" + "SELECT ?a ?b WHERE {" + " ?a foaf:knows ?b ." + "} ORDER BY ?a ?b"; Query q = QueryFactory.create(qStr); •  Programming interface deprecated and badly documented •  A Query can be used several times, on multiple models •  For each execution, a new QueryExecution is needed •  To produce a QueryExecution for a given Query and Model:

QueryExecution qe = QueryExecutionFactory.create(q, model);

Page 61: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    61 Semantic Technologies

Part  5   SPARQL  

Executing a Query •  QueryExecution contains methods to execute different

kinds of queries (SELECT, CONSTRUCT, etc.) •  E.g. for a SELECT query: ResultSet res = qe.execSelect(); •  ResultSet is a sub-interface of

Iterator<QuerySolution> •  Also has methods to get list of variables •  QuerySolution has methods to get list of variables,

value of single variables, etc. •  Important to call close() on query executions when no

longer needed (Why?)

Page 62: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    62 Semantic Technologies

Part  5   SPARQL  

Example: SPARQL in Jena String qStr = "SELECT ?a ?b ..."; Query q = QueryFactory.create(qStr); QueryExecution qe = QueryExecutionFactory.create(q,model); try { res = qe.execSelect(); while( res.hasNext()) { QuerySolution soln = res.next(); RDFNode a = soln.get("?a"); RDFNode b = soln.get("?b"); System.out.println(""+a+" knows "+b); } } finally { qe.close(); }

Page 63: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    63 Semantic Technologies

Part  5   SPARQL  

SPARQL on the Web •  Many sites (DBLP, dbpedia, dbtunes,. . . ) publish SPARQL endpoints •  I.e. SPARQL queries can be submitted to a database server that sends

back the results •  Uses HTTP to submit URL-encoded queries to server

GET /sparql/?query=... HTTP/1.1 •  Actually defined via W3C Web Services, see

http://www.w3.org/TR/rdf-sparql-protocol/ •  For SELECT queries you can get a XML or JSON result set, see

http://www.w3.org/TR/rdf-sparql-XMLres/ http://www.w3.org/TR/rdf-sparql-json-res/

•  Nothing you would want to do manually!

Page 64: SPARQL - Free University of Bozen-Bolzanonutt/Teaching/SemTechs1415/SemTechsSlides/5-SP… · Part5! SPARQL! SPARQL in General • A family of W3C recommendations • SPARQL Query

Master Informatique                                                                    64 Semantic Technologies

Part  5   SPARQL  

Remote SPARQL with Jena •  Jena can send SPARQL queries to a remote endpoint! •  Use one of the sparqlService methodes in QueryExecutionFactory •  E.g. String endpoint = "http://dblp.l3s.de/d2r/sparql"; String qStr = "SELECT ?a ?b ..."; Query q = QueryFactory.create(qStr); QueryExecution qe = QueryExecutionFactory.sparqlService(endpoint,q); try { res = qe.execSelect(); ... } finally { qe.close();

}