SlideShare una empresa de Scribd logo
1 de 35
Linked Data &
Semantic Web
Technology
The Semantic Web
Part 10. SPARQL
Dr. Myungjin Lee
Linked Data & Semantic Web Technology
Query Language
• What is SQL (Structured Query Language)?
– a special-purpose programming language designed for managing data
held in a relational database management system (RDBMS)
• How to access the knowledge from RDF knowledge base?
2
Linked Data & Semantic Web Technology
SPARQL
• What is SPARQL?
– SPARQL Protocol and RDF Query Language
– an RDF query language to retrieve and manipulate data stored in
Resource Description Framework format
– W3C Recommendation 15 January 2008
• SPARQL recommendations consist of:
– Query Language for RDF Graph
– Protocol Layer to use SPARQL via HTTP
– XML Output Format for SPARQL Queries
3
Linked Data & Semantic Web Technology
SPARQL Query From
• SELECT
– to identify the variables to appear in the query results
• FROM
– to specify the dataset to be used for matching
• WHERE
– to provide the basic graph pattern to match against the data graph
4
SELECT …
FROM …
WHERE {
…
}
Linked Data & Semantic Web Technology
SQL and SPARQL
5
SELECT name
FROM users
WHERE contact=„011-201-1111‟;
SELECT ?name
FROM <http://semantics.kr/user.rdf>
WHERE {
?user rdf:type :User.
?user :name ?name.
?user :contact “011-201-1111”.
}
SQL
SPARQL
Linked Data & Semantic Web Technology
Query Variables of SELECT Clause
• Query Variables
– prefixed by either "?" or "$"
– use of a given variable name anywhere in a query identifies the same
variable (global scope)
– ?name, ?title, ?author
• Query Results as a table
6
SELECT ?title ?author
title author
Novel XML Myungjin Lee
Web Tim-Berners Lee
SPARQL
Query Results
Knowledge Base
Linked Data & Semantic Web Technology
Pattern of WHERE Clause
• Triple Pattern
– written as a whitespace-separated list of a subject, predicate and
object
• Graph Pattern
– a set of triple patterns
• WHERE Clause of SPARQL
– based on RDF Turtle serialization and graph pattern matching
7
Linked Data & Semantic Web Technology
Simple Query Example
8
Data
<http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> "SPARQL Tutorial" .
Query
SELECT ?title
WHERE
{
<http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> ?title .
}
Query Result
title
“SPARQL Tutorial”
Linked Data & Semantic Web Technology
Multiple Matches
9
Data
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Johnny Lee Outlaw" .
_:a foaf:mbox <mailto:jlow@example.com> .
_:b foaf:name "Peter Goodguy" .
_:b foaf:mbox <mailto:peter@example.org> .
_:c foaf:mbox <mailto:carol@example.org> .
Query
SELECT ?name ?mbox
WHERE
{ ?x <http://xmlns.com/foaf/0.1/name> ?name .
?x <http://xmlns.com/foaf/0.1/mbox> ?mbox }
Query Result
name mbox
“Johnny Lee Outlaw” <mailto:jlow@example.com>
“Peter Goodguy” <mailto:peter@example.org>
Linked Data & Semantic Web Technology
Syntax for Triple Patterns
• Predicate-Object Lists
• Object Lists
10
?x foaf:name ?name ;
foaf:mbox ?mbox .
?x foaf:name ?name .
?x foaf:mbox ?mbox .
?x foaf:nick "Alice" , "Alice_" .
?x foaf:nick "Alice" .
?x foaf:nick "Alice_" .
?x foaf:name ?name ; foaf:nick "Alice" , "Alice_" .
?x foaf:name ?name .
?x foaf:nick "Alice" .
?x foaf:nick "Alice_" .
Linked Data & Semantic Web Technology
Syntax for Triple Patterns
• rdf:type
– the keyword "a" can be used as a predicate in a triple pattern
– an alternative for the IRI
http://www.w3.org/1999/02/22-rdf-syntax-ns#type
11
?x a :Class1 .
[ a :appClass ] :p "v" .
?x rdf:type :Class1 .
_:b0 rdf:type :appClass .
_:b0 :p "v" .
Linked Data & Semantic Web Technology
PREFIX and BASE Keywords
• PREFIX Keyword
– to associate a prefix label with an IRI
– a prefix label and a local part, separated by a colon ":“
• BASE Keyword
– to define the Base IRI
12
<http://example.org/book/book1>
BASE <http://example.org/book/>
<book1>
PREFIX book: <http://example.org/book/>
book:book1
Linked Data & Semantic Web Technology
PREFIX and BASE Keywords
13
PREFIX dc: <http://purl.org/dc/elements/purl.org/>
SELECT ?title
WHERE { <http://example.org/book/book> dc:title ?title }
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX : <http://example.org/book/>
SELECT $title
WHERE { :book1 dc:title $title }
BASE <http://example.org/book/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT $title
WHERE { <book1> dc:title $title }
Linked Data & Semantic Web Technology
• Literals with Language Tags
– expressed using @ and the
language tag
• Literals with Numeric Types
– Integers in a SPARQL query indicate
an RDF typed literal with the
datatype xsd:integer.
RDF Literals
14
Data
@prefix dt: <http://example.org/datatype#> .
@prefix ns: <http://example.org/ns#> .
@prefix : <http://example.org/ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
:x ns:p "cat"@en .
:y ns:p "42"^^xsd:integer .
:z ns:p "abc"^^dt:specialDatatype .
Query
SELECT ?v WHERE { ?v ?p "cat" }
Query Result
v
Query
SELECT ?v WHERE { ?v ?p 42 }
Query Result
v
<http://example.org/ns#y>
Linked Data & Semantic Web Technology
Syntax for Literals
• the General Syntax for Literals
– a string (enclosed in either double quotes, "...", or single quotes, '...'), with
either an optional language tag or an optional datatype IRI or prefixed
name
– integers, decimal numbers, and booleans can be written directly (without
quotation marks and an explicit datatype IRI)
• Examples
– "chat"
– 'chat'@fr with language tag "fr"
– "xyz"^^<http://example.org/ns/userDatatype>
– "abc"^^appNS:appDataType
– 1, which is the same as "1"^^xsd:integer
– 1.3, which is the same as "1.3"^^xsd:decimal
– 1.0e6, which is the same as "1.0e6"^^xsd:double
– true, which is the same as "true"^^xsd:boolean
15
Linked Data & Semantic Web Technology
RDF Term Constraints
• FILTER Keyword
– to restrict solutions to those for which the filter expression evaluates to
TRUE
16
Data
@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 .
Linked Data & Semantic Web Technology
• Restricting the Values of
Strings
– regex function
• to match only plain literals
with no language tag
• Restricting Numeric Values
RDF Term Constraints
17
Query
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?title
WHERE { ?x dc:title ?title
FILTER regex(?title, "^SPARQL")
}
Query Result
title
“SPARQL Tutorial”
Query
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.5)
?x dc:title ?title . }
Query Result
title price
“The Semantic Web” 23
Linked Data & Semantic Web Technology
Optional Pattern Matching
• OPTIONAL keyword
– to select optional elements from the RDF graph
18
Data
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
_:a rdf:type foaf:Person .
_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@example.com> .
_:a foaf:mbox <mailto:alice@work.example> .
_:b rdf:type foaf:Person .
_:b foaf:name "Bob" .
Query
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE { ?x foaf:name ?name .
OPTIONAL { ?x foaf:mbox ?mbox }
}
Query Result
name mbox
“Alice” <mailto:alice@example.com>
“Alice” <mailto:alice@work.example>
“Bob”
Linked Data & Semantic Web Technology
Matching Alternatives
• a means of combining graph patterns
19
Data
@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" .
_:a dc10:creator "Alice" .
_:b dc11:title "SPARQL Protocol Tutorial" .
_:b dc11:creator "Bob" .
_:c dc10:title "SPARQL" .
_:c dc11:title "SPARQL (updated)" .
Query
PREFIX dc10: <http://purl.org/dc/elements/1.0/>
PREFIX dc11: <http://purl.org/dc/elements/1.1/>
SELECT ?title
WHERE { { ?book dc10:title ?title } UNION { ?book dc11:title ?title } }
Query Result
name
"SPARQL Protocol Tutorial"
"SPARQL"
"SPARQL (updated)"
"SPARQL Query Language Tutorial"
Linked Data & Semantic Web Technology
ORDER BY
• ORDER BY Clause
– the order of a solution sequence
20
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE { ?x foaf:name ?name }
ORDER BY ?name
PREFIX : <http://example.org/ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?name
WHERE { ?x foaf:name ?name ; :empId ?emp }
ORDER BY DESC(?emp)
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE { ?x foaf:name ?name ; :empId ?emp }
ORDER BY ?name DESC(?emp)
Linked Data & Semantic Web Technology
Duplicate Solutions
• DISTINCT Modifier
– to eliminate duplicate solutions
21
Data
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:x foaf:name "Alice" .
_:x foaf:mbox <mailto:alice@example.com> .
_:y foaf:name "Alice" .
_:y foaf:mbox <mailto:asmith@example.com> .
_:z foaf:name "Alice" .
_:z foaf:mbox <mailto:alice.smith@example.com> .
Query
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?name WHERE { ?x foaf:name ?name }
Query Result
name
“Alice”
Linked Data & Semantic Web Technology
OFFSET and LIMIT
• LIMIT Clause
– to put an upper bound on the number of solutions returned
• OFFSET Clause
– to cause the solutions generated to start after the specified number of
solutions
22
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE { ?x foaf:name ?name }
ORDER BY ?name
LIMIT 5
OFFSET 10
Linked Data & Semantic Web Technology
Query Forms
• Four Query Forms
– SELECT
• Returns all, or a subset of, the variables bound in a query pattern match.
– CONSTRUCT
• Returns an RDF graph constructed by substituting variables in a set of triple
templates.
– ASK
• Returns a boolean indicating whether a query pattern matches or not.
– DESCRIBE
• Returns an RDF graph that describes the resources found.
23
Linked Data & Semantic Web Technology
SELECT Form
• to return variables and their bindings directly
24
Data
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" .
_:a foaf:knows _:b .
_:a foaf:knows _:c .
_:b foaf:name "Bob" .
_:c foaf:name "Clare" .
_:c foaf:nick "CT" .
Query
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?nameX ?nameY ?nickY
WHERE
{ ?x foaf:knows ?y ;
foaf:name ?nameX .
?y foaf:name ?nameY .
OPTIONAL { ?y foaf:nick ?nickY }
}
Query Result
nameX nameY nickY
“Alice” “Bob”
“Alice” “Clare” “CT”
Linked Data & Semantic Web Technology
CONSTRUCT Form
• to return a single RDF graph specified by a graph template
25
Data
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@example.org> .
Query
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
CONSTRUCT { <http://example.org/person#Alice> vcard:FN ?name }
WHERE { ?x foaf:name ?name }
Query Result
@prefix vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> .
<http://example.org/person#Alice> vcard:FN "Alice" .
Linked Data & Semantic Web Technology
ASK Form
• to test whether or not a query pattern has a solution
26
Data
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" .
_:a foaf:homepage <http://work.example.org/alice/> .
_:b foaf:name "Bob" .
_:b foaf:mbox <mailto:bob@work.example> .
Query
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
ASK { ?x foaf:name "Alice" }
Query Result
yes
Linked Data & Semantic Web Technology
SPARQL Query Results XML Format
• an XML format for the variable binding and boolean results
formats provided by the SPARQL query language for RDF
27
<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
<head>
<variable name="x"/>
<variable name="hpage"/>
<variable name="name"/>
<variable name="age"/>
<variable name="mbox"/>
<variable name="friend"/>
</head>
<results>
<result>
<binding name="x">
<bnode>r2</bnode>
</binding>
<binding name="hpage">
<uri>http://work.example.org/bob/</uri>
</binding>
<binding name="name">
<literal xml:lang="en">Bob</literal>
</binding>
<binding name="age">
<literal datatype="http://www.w3.org/2001/XMLSchema#integer">30</literal>
</binding>
</result>
...
</results>
</sparql>
Linked Data & Semantic Web Technology
SPARQL Query Results XML Format
• Document Element
• Header
– to contain a sequence of elements describing the set of Query Variable
names in the Solution Sequence
28
<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
...
</sparql>
<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
<head>
<variable name="x"/>
<variable name="hpage"/>
<variable name="name"/>
<variable name="mbox"/>
<variable name="blurb"/>
</head>
...
</sparql>
Linked Data & Semantic Web Technology
SPARQL Query Results XML Format
• Results
– the results element contains the complete sequence of query results
– a result child-element of results is added giving a document
– each result element corresponds to one Query Solution in a result
– each binding inside a solution is written as an element binding as a child of
result with the query variable name as the value of the name attribute
• the content of the binding
– RDF URI Reference U
• <binding><uri>U</uri></binding>
– RDF Literal S
• <binding><literal>S</literal></binding>
– RDF Literal S with language L
• <binding><literal xml:lang="L">S</literal></binding>
– RDF Typed Literal S with datatype URI D
• <binding><literal datatype="D">S</literal></binding>
– Blank Node label I
• <binding><bnode>I</bnode></binding>
29
Linked Data & Semantic Web Technology
an Example of a Query Solution
1. <?xml version="1.0"?>
2. <sparql xmlns="http://www.w3.org/2005/sparql-results#">
3. <head>
4. <variable name="x"/>
5. <variable name="hpage"/>
6. <variable name="name"/>
7. <variable name="age"/>
8. <variable name="mbox"/>
9. <variable name="friend"/>
10. </head>
11. <results>
12. <result>
13. <binding name="x">
14. <bnode>r2</bnode>
15. </binding>
16. <binding name="hpage">
17. <uri>http://work.example.org/bob/</uri>
18. </binding>
19. <binding name="name">
20. <literal xml:lang="en">Bob</literal>
21. </binding>
22. <binding name="age">
23. <literal datatype="http://www.w3.org/2001/XMLSchema#integer">30</literal>
24. </binding>
25. <binding name="mbox">
26. <uri>mailto:bob@work.example.org</uri>
27. </binding>
28. </result>
29. ...
30. </results>
31. </sparql>
30
Linked Data & Semantic Web Technology
SPARQL Protocol for RDF
• SPARQL Protocol
– method to query/respond of SPARQL queries via HTTP
• Two Types of SPARQL Protocol
– HTTP Bindings
– SOAP Bindings
31
Linked Data & Semantic Web Technology
HTTP Bindings
32
Query
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?book ?who
WHERE { ?book dc:creator ?who }
Query Result
HTTP/1.1 200 OK
Date: Fri, 06 May 2005 20:55:12 GMT
Server: Apache/1.3.29 (Unix) PHP/4.3.4 DAV/1.0.3
Connection: close
Content-Type: application/sparql-results+xml
<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
<head>
<variable name="book"/>
<variable name="who"/>
</head>
<results distinct="false" ordered="false">
<result>
<binding name="book"><uri>http://www.example/book/book5</uri></binding>
<binding name="who"><bnode>r29392923r2922</bnode></binding>
</result>
...
</sparql>
HTTP Message
GET /sparql/?query=EncodedQuery HTTP/1.1
Host: www.example
User-agent: my-sparql-client/0.1
Knowledge Base
Linked Data & Semantic Web Technology
SOAP Bindings
33
Query Result
HTTP/1.1 200 OK
Content-Type: application/soap+xml
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<query-result xmlns="http://www.w3.org/2005/09/sparql-protocol-types/#">
<ns1:sparql xmlns:ns1="http://www.w3.org/2005/sparql-results#">
<ns1:head>
<ns1:variable name="z"/>
</ns1:head>
<ns1:results distinct="false" ordered="false">
<ns1:result>
<ns1:binding name="z">
<ns1:literal>Harry Potter and the Chamber of Secrets</ns1:literal>
</ns1:binding>
</ns1:result>
...
</ns1:results>
</ns1:sparql>
</query-result>
</soapenv:Body>
</soapenv:Envelope>
SOAP Request
POST /services/sparql-query HTTP/1.1
Content-Type: application/soap+xml
Accept: application/soap+xml, multipart/related, text/*
User-Agent: Axis/1.2.1
Host: www.example
SOAPAction: ""
Content-Length: 438
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<query-request xmlns="http://www.w3.org/2005/09/sparql-protocol-types/#">
<query>SELECT ?z {?x ?y ?z . FILTER regex(?z, 'Harry')}</query>
</query-request>
</soapenv:Body>
</soapenv:Envelope>
Knowledge Base
Linked Data & Semantic Web Technology
References
• http://en.wikipedia.org/wiki/Sparql
• http://www.w3.org/TR/rdf-sparql-query/
• http://www.w3.org/TR/rdf-sparql-protocol/
• http://www.w3.org/TR/2008/REC-rdf-sparql-XMLres-20080115/
• http://www.slideshare.net/lysander07/openhpi-semweb03part1
• http://www.slideshare.net/lysander07/open-hpi-semweb03part2
• http://www.slideshare.net/lysander07/openhpi-33-how-to-query-rdfs-sparql3
• http://www.slideshare.net/fabien_gandon/sparql-in-a-nutshell
• http://www.slideshare.net/kwangsub/rdf-tutorial-sparql-20091031
34
Linked Data & Semantic Web Technology
3535
Dr. Myungjin Lee
e-Mail : mjlee@li-st.com
Twitter : http://twitter.com/MyungjinLee
Facebook : http://www.facebook.com/mjinlee
SlideShare : http://www.slideshare.net/onlyjiny/

Más contenido relacionado

La actualidad más candente

Linked data: spreading data over the web
Linked data: spreading data over the webLinked data: spreading data over the web
Linked data: spreading data over the web
shellac
 
SemanticWeb Nuts 'n Bolts
SemanticWeb Nuts 'n BoltsSemanticWeb Nuts 'n Bolts
SemanticWeb Nuts 'n Bolts
Rinke Hoekstra
 
An introduction to Semantic Web and Linked Data
An introduction to Semantic  Web and Linked DataAn introduction to Semantic  Web and Linked Data
An introduction to Semantic Web and Linked Data
Gabriela Agustini
 

La actualidad más candente (20)

SWT Lecture Session 2 - RDF
SWT Lecture Session 2 - RDFSWT Lecture Session 2 - RDF
SWT Lecture Session 2 - RDF
 
RDF Tutorial - SPARQL 20091031
RDF Tutorial - SPARQL 20091031RDF Tutorial - SPARQL 20091031
RDF Tutorial - SPARQL 20091031
 
FOAF
FOAFFOAF
FOAF
 
An introduction to Semantic Web and Linked Data
An introduction to Semantic Web and Linked DataAn introduction to Semantic Web and Linked Data
An introduction to Semantic Web and Linked Data
 
Saveface - Save your Facebook content as RDF data
Saveface - Save your Facebook content as RDF dataSaveface - Save your Facebook content as RDF data
Saveface - Save your Facebook content as RDF data
 
GDG Meets U event - Big data & Wikidata - no lies codelab
GDG Meets U event - Big data & Wikidata -  no lies codelabGDG Meets U event - Big data & Wikidata -  no lies codelab
GDG Meets U event - Big data & Wikidata - no lies codelab
 
Linked (Open) Data
Linked (Open) DataLinked (Open) Data
Linked (Open) Data
 
Linked Data Tutorial
Linked Data TutorialLinked Data Tutorial
Linked Data Tutorial
 
Linked data: spreading data over the web
Linked data: spreading data over the webLinked data: spreading data over the web
Linked data: spreading data over the web
 
SemanticWeb Nuts 'n Bolts
SemanticWeb Nuts 'n BoltsSemanticWeb Nuts 'n Bolts
SemanticWeb Nuts 'n Bolts
 
Introduction to RDF
Introduction to RDFIntroduction to RDF
Introduction to RDF
 
RDF Data Model
RDF Data ModelRDF Data Model
RDF Data Model
 
Querying Linked Data on Android
Querying Linked Data on AndroidQuerying Linked Data on Android
Querying Linked Data on Android
 
RDFa Introductory Course Session 2/4 How RDFa
RDFa Introductory Course Session 2/4 How RDFaRDFa Introductory Course Session 2/4 How RDFa
RDFa Introductory Course Session 2/4 How RDFa
 
Introduction to RDFa
Introduction to RDFaIntroduction to RDFa
Introduction to RDFa
 
Name That Graph !
Name That Graph !Name That Graph !
Name That Graph !
 
RDFa: an introduction
RDFa: an introductionRDFa: an introduction
RDFa: an introduction
 
Usage of Linked Data: Introduction and Application Scenarios
Usage of Linked Data: Introduction and Application ScenariosUsage of Linked Data: Introduction and Application Scenarios
Usage of Linked Data: Introduction and Application Scenarios
 
4 sw architectures and sparql
4 sw architectures and sparql4 sw architectures and sparql
4 sw architectures and sparql
 
An introduction to Semantic Web and Linked Data
An introduction to Semantic  Web and Linked DataAn introduction to Semantic  Web and Linked Data
An introduction to Semantic Web and Linked Data
 

Destacado (6)

Linked Open Data Tutorial
Linked Open Data TutorialLinked Open Data Tutorial
Linked Open Data Tutorial
 
The Semantic Web #6 - RDF Schema
The Semantic Web #6 - RDF SchemaThe Semantic Web #6 - RDF Schema
The Semantic Web #6 - RDF Schema
 
The Semantic Web #3 - URI
The Semantic Web #3 - URIThe Semantic Web #3 - URI
The Semantic Web #3 - URI
 
The Semantic Web #8 - Ontology
The Semantic Web #8 - OntologyThe Semantic Web #8 - Ontology
The Semantic Web #8 - Ontology
 
Linked Data Modeling for Beginner
Linked Data Modeling for BeginnerLinked Data Modeling for Beginner
Linked Data Modeling for Beginner
 
The Semantic Web #7 - RDF Semantics
The Semantic Web #7 - RDF SemanticsThe Semantic Web #7 - RDF Semantics
The Semantic Web #7 - RDF Semantics
 

Similar a The Semantic Web #10 - SPARQL

SPARQL 1.1 Update (2013-03-05)
SPARQL 1.1 Update (2013-03-05)SPARQL 1.1 Update (2013-03-05)
SPARQL 1.1 Update (2013-03-05)
andyseaborne
 
Creating APIs over RDF
Creating APIs over RDFCreating APIs over RDF
Creating APIs over RDF
Leigh Dodds
 
Creating APIs over RDF
Creating APIs over RDFCreating APIs over RDF
Creating APIs over RDF
Leigh Dodds
 
Querying the Semantic Web with SPARQL
Querying the Semantic Web with SPARQLQuerying the Semantic Web with SPARQL
Querying the Semantic Web with SPARQL
Emanuele Della Valle
 
A hands on overview of the semantic web
A hands on overview of the semantic webA hands on overview of the semantic web
A hands on overview of the semantic web
Marakana Inc.
 
SWT Lecture Session 4 - SW architectures and SPARQL
SWT Lecture Session 4 - SW architectures and SPARQLSWT Lecture Session 4 - SW architectures and SPARQL
SWT Lecture Session 4 - SW architectures and SPARQL
Mariano Rodriguez-Muro
 
Mon norton tut_queryinglinkeddata02
Mon norton tut_queryinglinkeddata02Mon norton tut_queryinglinkeddata02
Mon norton tut_queryinglinkeddata02
eswcsummerschool
 

Similar a The Semantic Web #10 - SPARQL (20)

Sparql
SparqlSparql
Sparql
 
Sparql a simple knowledge query
Sparql  a simple knowledge querySparql  a simple knowledge query
Sparql a simple knowledge query
 
Querying Linked Data
Querying Linked DataQuerying Linked Data
Querying Linked Data
 
Data Integration And Visualization
Data Integration And VisualizationData Integration And Visualization
Data Integration And Visualization
 
Data in RDF
Data in RDFData in RDF
Data in RDF
 
SPARQL 1.1 Update (2013-03-05)
SPARQL 1.1 Update (2013-03-05)SPARQL 1.1 Update (2013-03-05)
SPARQL 1.1 Update (2013-03-05)
 
Creating APIs over RDF
Creating APIs over RDFCreating APIs over RDF
Creating APIs over RDF
 
Creating APIs over RDF
Creating APIs over RDFCreating APIs over RDF
Creating APIs over RDF
 
Querying the Semantic Web with SPARQL
Querying the Semantic Web with SPARQLQuerying the Semantic Web with SPARQL
Querying the Semantic Web with SPARQL
 
A hands on overview of the semantic web
A hands on overview of the semantic webA hands on overview of the semantic web
A hands on overview of the semantic web
 
From SQL to SPARQL
From SQL to SPARQLFrom SQL to SPARQL
From SQL to SPARQL
 
balloon Fusion: SPARQL Rewriting Based on Unified Co-Reference Information
balloon Fusion: SPARQL Rewriting Based on  Unified Co-Reference Informationballoon Fusion: SPARQL Rewriting Based on  Unified Co-Reference Information
balloon Fusion: SPARQL Rewriting Based on Unified Co-Reference Information
 
Visualize open data with Plone - eea.daviz PLOG 2013
Visualize open data with Plone - eea.daviz PLOG 2013Visualize open data with Plone - eea.daviz PLOG 2013
Visualize open data with Plone - eea.daviz PLOG 2013
 
SWT Lecture Session 4 - SW architectures and SPARQL
SWT Lecture Session 4 - SW architectures and SPARQLSWT Lecture Session 4 - SW architectures and SPARQL
SWT Lecture Session 4 - SW architectures and SPARQL
 
A Little SPARQL in your Analytics
A Little SPARQL in your AnalyticsA Little SPARQL in your Analytics
A Little SPARQL in your Analytics
 
Sparql service-description
Sparql service-descriptionSparql service-description
Sparql service-description
 
Mon norton tut_queryinglinkeddata02
Mon norton tut_queryinglinkeddata02Mon norton tut_queryinglinkeddata02
Mon norton tut_queryinglinkeddata02
 
Semantic Web
Semantic WebSemantic Web
Semantic Web
 
Semantic Web(Web 3.0) SPARQL
Semantic Web(Web 3.0) SPARQLSemantic Web(Web 3.0) SPARQL
Semantic Web(Web 3.0) SPARQL
 
SPARQL-DL - Theory & Practice
SPARQL-DL - Theory & PracticeSPARQL-DL - Theory & Practice
SPARQL-DL - Theory & Practice
 

Más de Myungjin Lee

Más de Myungjin Lee (20)

지식그래프 개념과 활용방안 (Knowledge Graph - Introduction and Use Cases)
지식그래프 개념과 활용방안 (Knowledge Graph - Introduction and Use Cases)지식그래프 개념과 활용방안 (Knowledge Graph - Introduction and Use Cases)
지식그래프 개념과 활용방안 (Knowledge Graph - Introduction and Use Cases)
 
JSP 프로그래밍 #05 HTML과 JSP
JSP 프로그래밍 #05 HTML과 JSPJSP 프로그래밍 #05 HTML과 JSP
JSP 프로그래밍 #05 HTML과 JSP
 
JSP 프로그래밍 #04 JSP 의 기본
JSP 프로그래밍 #04 JSP 의 기본JSP 프로그래밍 #04 JSP 의 기본
JSP 프로그래밍 #04 JSP 의 기본
 
JSP 프로그래밍 #03 서블릿
JSP 프로그래밍 #03 서블릿JSP 프로그래밍 #03 서블릿
JSP 프로그래밍 #03 서블릿
 
JSP 프로그래밍 #02 서블릿과 JSP 시작하기
JSP 프로그래밍 #02 서블릿과 JSP 시작하기JSP 프로그래밍 #02 서블릿과 JSP 시작하기
JSP 프로그래밍 #02 서블릿과 JSP 시작하기
 
JSP 프로그래밍 #01 웹 프로그래밍
JSP 프로그래밍 #01 웹 프로그래밍JSP 프로그래밍 #01 웹 프로그래밍
JSP 프로그래밍 #01 웹 프로그래밍
 
관광 지식베이스와 스마트 관광 서비스 (Knowledge base and Smart Tourism)
관광 지식베이스와 스마트 관광 서비스 (Knowledge base and Smart Tourism)관광 지식베이스와 스마트 관광 서비스 (Knowledge base and Smart Tourism)
관광 지식베이스와 스마트 관광 서비스 (Knowledge base and Smart Tourism)
 
오픈 데이터와 인공지능
오픈 데이터와 인공지능오픈 데이터와 인공지능
오픈 데이터와 인공지능
 
법령 온톨로지의 구축 및 검색
법령 온톨로지의 구축 및 검색법령 온톨로지의 구축 및 검색
법령 온톨로지의 구축 및 검색
 
도서관과 Linked Data
도서관과 Linked Data도서관과 Linked Data
도서관과 Linked Data
 
공공데이터, 현재 우리는?
공공데이터, 현재 우리는?공공데이터, 현재 우리는?
공공데이터, 현재 우리는?
 
LODAC 2017 Linked Open Data Workshop
LODAC 2017 Linked Open Data WorkshopLODAC 2017 Linked Open Data Workshop
LODAC 2017 Linked Open Data Workshop
 
Introduction of Deep Learning
Introduction of Deep LearningIntroduction of Deep Learning
Introduction of Deep Learning
 
서울시 열린데이터 광장 문화관광 분야 LOD 서비스
서울시 열린데이터 광장 문화관광 분야 LOD 서비스서울시 열린데이터 광장 문화관광 분야 LOD 서비스
서울시 열린데이터 광장 문화관광 분야 LOD 서비스
 
LOD(Linked Open Data) Recommendations
LOD(Linked Open Data) RecommendationsLOD(Linked Open Data) Recommendations
LOD(Linked Open Data) Recommendations
 
Interlinking for Linked Data
Interlinking for Linked DataInterlinking for Linked Data
Interlinking for Linked Data
 
Linked Data Usecases
Linked Data UsecasesLinked Data Usecases
Linked Data Usecases
 
공공데이터와 Linked open data
공공데이터와 Linked open data공공데이터와 Linked open data
공공데이터와 Linked open data
 
공공데이터와 Linked open data
공공데이터와 Linked open data공공데이터와 Linked open data
공공데이터와 Linked open data
 
Development of Twitter Application #8 - Streaming API
Development of Twitter Application #8 - Streaming APIDevelopment of Twitter Application #8 - Streaming API
Development of Twitter Application #8 - Streaming API
 

Último

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Último (20)

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 

The Semantic Web #10 - SPARQL

  • 1. Linked Data & Semantic Web Technology The Semantic Web Part 10. SPARQL Dr. Myungjin Lee
  • 2. Linked Data & Semantic Web Technology Query Language • What is SQL (Structured Query Language)? – a special-purpose programming language designed for managing data held in a relational database management system (RDBMS) • How to access the knowledge from RDF knowledge base? 2
  • 3. Linked Data & Semantic Web Technology SPARQL • What is SPARQL? – SPARQL Protocol and RDF Query Language – an RDF query language to retrieve and manipulate data stored in Resource Description Framework format – W3C Recommendation 15 January 2008 • SPARQL recommendations consist of: – Query Language for RDF Graph – Protocol Layer to use SPARQL via HTTP – XML Output Format for SPARQL Queries 3
  • 4. Linked Data & Semantic Web Technology SPARQL Query From • SELECT – to identify the variables to appear in the query results • FROM – to specify the dataset to be used for matching • WHERE – to provide the basic graph pattern to match against the data graph 4 SELECT … FROM … WHERE { … }
  • 5. Linked Data & Semantic Web Technology SQL and SPARQL 5 SELECT name FROM users WHERE contact=„011-201-1111‟; SELECT ?name FROM <http://semantics.kr/user.rdf> WHERE { ?user rdf:type :User. ?user :name ?name. ?user :contact “011-201-1111”. } SQL SPARQL
  • 6. Linked Data & Semantic Web Technology Query Variables of SELECT Clause • Query Variables – prefixed by either "?" or "$" – use of a given variable name anywhere in a query identifies the same variable (global scope) – ?name, ?title, ?author • Query Results as a table 6 SELECT ?title ?author title author Novel XML Myungjin Lee Web Tim-Berners Lee SPARQL Query Results Knowledge Base
  • 7. Linked Data & Semantic Web Technology Pattern of WHERE Clause • Triple Pattern – written as a whitespace-separated list of a subject, predicate and object • Graph Pattern – a set of triple patterns • WHERE Clause of SPARQL – based on RDF Turtle serialization and graph pattern matching 7
  • 8. Linked Data & Semantic Web Technology Simple Query Example 8 Data <http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> "SPARQL Tutorial" . Query SELECT ?title WHERE { <http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> ?title . } Query Result title “SPARQL Tutorial”
  • 9. Linked Data & Semantic Web Technology Multiple Matches 9 Data @prefix foaf: <http://xmlns.com/foaf/0.1/> . _:a foaf:name "Johnny Lee Outlaw" . _:a foaf:mbox <mailto:jlow@example.com> . _:b foaf:name "Peter Goodguy" . _:b foaf:mbox <mailto:peter@example.org> . _:c foaf:mbox <mailto:carol@example.org> . Query SELECT ?name ?mbox WHERE { ?x <http://xmlns.com/foaf/0.1/name> ?name . ?x <http://xmlns.com/foaf/0.1/mbox> ?mbox } Query Result name mbox “Johnny Lee Outlaw” <mailto:jlow@example.com> “Peter Goodguy” <mailto:peter@example.org>
  • 10. Linked Data & Semantic Web Technology Syntax for Triple Patterns • Predicate-Object Lists • Object Lists 10 ?x foaf:name ?name ; foaf:mbox ?mbox . ?x foaf:name ?name . ?x foaf:mbox ?mbox . ?x foaf:nick "Alice" , "Alice_" . ?x foaf:nick "Alice" . ?x foaf:nick "Alice_" . ?x foaf:name ?name ; foaf:nick "Alice" , "Alice_" . ?x foaf:name ?name . ?x foaf:nick "Alice" . ?x foaf:nick "Alice_" .
  • 11. Linked Data & Semantic Web Technology Syntax for Triple Patterns • rdf:type – the keyword "a" can be used as a predicate in a triple pattern – an alternative for the IRI http://www.w3.org/1999/02/22-rdf-syntax-ns#type 11 ?x a :Class1 . [ a :appClass ] :p "v" . ?x rdf:type :Class1 . _:b0 rdf:type :appClass . _:b0 :p "v" .
  • 12. Linked Data & Semantic Web Technology PREFIX and BASE Keywords • PREFIX Keyword – to associate a prefix label with an IRI – a prefix label and a local part, separated by a colon ":“ • BASE Keyword – to define the Base IRI 12 <http://example.org/book/book1> BASE <http://example.org/book/> <book1> PREFIX book: <http://example.org/book/> book:book1
  • 13. Linked Data & Semantic Web Technology PREFIX and BASE Keywords 13 PREFIX dc: <http://purl.org/dc/elements/purl.org/> SELECT ?title WHERE { <http://example.org/book/book> dc:title ?title } PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX : <http://example.org/book/> SELECT $title WHERE { :book1 dc:title $title } BASE <http://example.org/book/> PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT $title WHERE { <book1> dc:title $title }
  • 14. Linked Data & Semantic Web Technology • Literals with Language Tags – expressed using @ and the language tag • Literals with Numeric Types – Integers in a SPARQL query indicate an RDF typed literal with the datatype xsd:integer. RDF Literals 14 Data @prefix dt: <http://example.org/datatype#> . @prefix ns: <http://example.org/ns#> . @prefix : <http://example.org/ns#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :x ns:p "cat"@en . :y ns:p "42"^^xsd:integer . :z ns:p "abc"^^dt:specialDatatype . Query SELECT ?v WHERE { ?v ?p "cat" } Query Result v Query SELECT ?v WHERE { ?v ?p 42 } Query Result v <http://example.org/ns#y>
  • 15. Linked Data & Semantic Web Technology Syntax for Literals • the General Syntax for Literals – a string (enclosed in either double quotes, "...", or single quotes, '...'), with either an optional language tag or an optional datatype IRI or prefixed name – integers, decimal numbers, and booleans can be written directly (without quotation marks and an explicit datatype IRI) • Examples – "chat" – 'chat'@fr with language tag "fr" – "xyz"^^<http://example.org/ns/userDatatype> – "abc"^^appNS:appDataType – 1, which is the same as "1"^^xsd:integer – 1.3, which is the same as "1.3"^^xsd:decimal – 1.0e6, which is the same as "1.0e6"^^xsd:double – true, which is the same as "true"^^xsd:boolean 15
  • 16. Linked Data & Semantic Web Technology RDF Term Constraints • FILTER Keyword – to restrict solutions to those for which the filter expression evaluates to TRUE 16 Data @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 .
  • 17. Linked Data & Semantic Web Technology • Restricting the Values of Strings – regex function • to match only plain literals with no language tag • Restricting Numeric Values RDF Term Constraints 17 Query PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?title WHERE { ?x dc:title ?title FILTER regex(?title, "^SPARQL") } Query Result title “SPARQL Tutorial” Query 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.5) ?x dc:title ?title . } Query Result title price “The Semantic Web” 23
  • 18. Linked Data & Semantic Web Technology Optional Pattern Matching • OPTIONAL keyword – to select optional elements from the RDF graph 18 Data @prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . _:a rdf:type foaf:Person . _:a foaf:name "Alice" . _:a foaf:mbox <mailto:alice@example.com> . _:a foaf:mbox <mailto:alice@work.example> . _:b rdf:type foaf:Person . _:b foaf:name "Bob" . Query PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name ?mbox WHERE { ?x foaf:name ?name . OPTIONAL { ?x foaf:mbox ?mbox } } Query Result name mbox “Alice” <mailto:alice@example.com> “Alice” <mailto:alice@work.example> “Bob”
  • 19. Linked Data & Semantic Web Technology Matching Alternatives • a means of combining graph patterns 19 Data @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" . _:a dc10:creator "Alice" . _:b dc11:title "SPARQL Protocol Tutorial" . _:b dc11:creator "Bob" . _:c dc10:title "SPARQL" . _:c dc11:title "SPARQL (updated)" . Query PREFIX dc10: <http://purl.org/dc/elements/1.0/> PREFIX dc11: <http://purl.org/dc/elements/1.1/> SELECT ?title WHERE { { ?book dc10:title ?title } UNION { ?book dc11:title ?title } } Query Result name "SPARQL Protocol Tutorial" "SPARQL" "SPARQL (updated)" "SPARQL Query Language Tutorial"
  • 20. Linked Data & Semantic Web Technology ORDER BY • ORDER BY Clause – the order of a solution sequence 20 PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name WHERE { ?x foaf:name ?name } ORDER BY ?name PREFIX : <http://example.org/ns#> PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT ?name WHERE { ?x foaf:name ?name ; :empId ?emp } ORDER BY DESC(?emp) PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name WHERE { ?x foaf:name ?name ; :empId ?emp } ORDER BY ?name DESC(?emp)
  • 21. Linked Data & Semantic Web Technology Duplicate Solutions • DISTINCT Modifier – to eliminate duplicate solutions 21 Data @prefix foaf: <http://xmlns.com/foaf/0.1/> . _:x foaf:name "Alice" . _:x foaf:mbox <mailto:alice@example.com> . _:y foaf:name "Alice" . _:y foaf:mbox <mailto:asmith@example.com> . _:z foaf:name "Alice" . _:z foaf:mbox <mailto:alice.smith@example.com> . Query PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT DISTINCT ?name WHERE { ?x foaf:name ?name } Query Result name “Alice”
  • 22. Linked Data & Semantic Web Technology OFFSET and LIMIT • LIMIT Clause – to put an upper bound on the number of solutions returned • OFFSET Clause – to cause the solutions generated to start after the specified number of solutions 22 PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name WHERE { ?x foaf:name ?name } ORDER BY ?name LIMIT 5 OFFSET 10
  • 23. Linked Data & Semantic Web Technology Query Forms • Four Query Forms – SELECT • Returns all, or a subset of, the variables bound in a query pattern match. – CONSTRUCT • Returns an RDF graph constructed by substituting variables in a set of triple templates. – ASK • Returns a boolean indicating whether a query pattern matches or not. – DESCRIBE • Returns an RDF graph that describes the resources found. 23
  • 24. Linked Data & Semantic Web Technology SELECT Form • to return variables and their bindings directly 24 Data @prefix foaf: <http://xmlns.com/foaf/0.1/> . _:a foaf:name "Alice" . _:a foaf:knows _:b . _:a foaf:knows _:c . _:b foaf:name "Bob" . _:c foaf:name "Clare" . _:c foaf:nick "CT" . Query PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?nameX ?nameY ?nickY WHERE { ?x foaf:knows ?y ; foaf:name ?nameX . ?y foaf:name ?nameY . OPTIONAL { ?y foaf:nick ?nickY } } Query Result nameX nameY nickY “Alice” “Bob” “Alice” “Clare” “CT”
  • 25. Linked Data & Semantic Web Technology CONSTRUCT Form • to return a single RDF graph specified by a graph template 25 Data @prefix foaf: <http://xmlns.com/foaf/0.1/> . _:a foaf:name "Alice" . _:a foaf:mbox <mailto:alice@example.org> . Query PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> CONSTRUCT { <http://example.org/person#Alice> vcard:FN ?name } WHERE { ?x foaf:name ?name } Query Result @prefix vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> . <http://example.org/person#Alice> vcard:FN "Alice" .
  • 26. Linked Data & Semantic Web Technology ASK Form • to test whether or not a query pattern has a solution 26 Data @prefix foaf: <http://xmlns.com/foaf/0.1/> . _:a foaf:name "Alice" . _:a foaf:homepage <http://work.example.org/alice/> . _:b foaf:name "Bob" . _:b foaf:mbox <mailto:bob@work.example> . Query PREFIX foaf: <http://xmlns.com/foaf/0.1/> ASK { ?x foaf:name "Alice" } Query Result yes
  • 27. Linked Data & Semantic Web Technology SPARQL Query Results XML Format • an XML format for the variable binding and boolean results formats provided by the SPARQL query language for RDF 27 <?xml version="1.0"?> <sparql xmlns="http://www.w3.org/2005/sparql-results#"> <head> <variable name="x"/> <variable name="hpage"/> <variable name="name"/> <variable name="age"/> <variable name="mbox"/> <variable name="friend"/> </head> <results> <result> <binding name="x"> <bnode>r2</bnode> </binding> <binding name="hpage"> <uri>http://work.example.org/bob/</uri> </binding> <binding name="name"> <literal xml:lang="en">Bob</literal> </binding> <binding name="age"> <literal datatype="http://www.w3.org/2001/XMLSchema#integer">30</literal> </binding> </result> ... </results> </sparql>
  • 28. Linked Data & Semantic Web Technology SPARQL Query Results XML Format • Document Element • Header – to contain a sequence of elements describing the set of Query Variable names in the Solution Sequence 28 <?xml version="1.0"?> <sparql xmlns="http://www.w3.org/2005/sparql-results#"> ... </sparql> <?xml version="1.0"?> <sparql xmlns="http://www.w3.org/2005/sparql-results#"> <head> <variable name="x"/> <variable name="hpage"/> <variable name="name"/> <variable name="mbox"/> <variable name="blurb"/> </head> ... </sparql>
  • 29. Linked Data & Semantic Web Technology SPARQL Query Results XML Format • Results – the results element contains the complete sequence of query results – a result child-element of results is added giving a document – each result element corresponds to one Query Solution in a result – each binding inside a solution is written as an element binding as a child of result with the query variable name as the value of the name attribute • the content of the binding – RDF URI Reference U • <binding><uri>U</uri></binding> – RDF Literal S • <binding><literal>S</literal></binding> – RDF Literal S with language L • <binding><literal xml:lang="L">S</literal></binding> – RDF Typed Literal S with datatype URI D • <binding><literal datatype="D">S</literal></binding> – Blank Node label I • <binding><bnode>I</bnode></binding> 29
  • 30. Linked Data & Semantic Web Technology an Example of a Query Solution 1. <?xml version="1.0"?> 2. <sparql xmlns="http://www.w3.org/2005/sparql-results#"> 3. <head> 4. <variable name="x"/> 5. <variable name="hpage"/> 6. <variable name="name"/> 7. <variable name="age"/> 8. <variable name="mbox"/> 9. <variable name="friend"/> 10. </head> 11. <results> 12. <result> 13. <binding name="x"> 14. <bnode>r2</bnode> 15. </binding> 16. <binding name="hpage"> 17. <uri>http://work.example.org/bob/</uri> 18. </binding> 19. <binding name="name"> 20. <literal xml:lang="en">Bob</literal> 21. </binding> 22. <binding name="age"> 23. <literal datatype="http://www.w3.org/2001/XMLSchema#integer">30</literal> 24. </binding> 25. <binding name="mbox"> 26. <uri>mailto:bob@work.example.org</uri> 27. </binding> 28. </result> 29. ... 30. </results> 31. </sparql> 30
  • 31. Linked Data & Semantic Web Technology SPARQL Protocol for RDF • SPARQL Protocol – method to query/respond of SPARQL queries via HTTP • Two Types of SPARQL Protocol – HTTP Bindings – SOAP Bindings 31
  • 32. Linked Data & Semantic Web Technology HTTP Bindings 32 Query PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?book ?who WHERE { ?book dc:creator ?who } Query Result HTTP/1.1 200 OK Date: Fri, 06 May 2005 20:55:12 GMT Server: Apache/1.3.29 (Unix) PHP/4.3.4 DAV/1.0.3 Connection: close Content-Type: application/sparql-results+xml <?xml version="1.0"?> <sparql xmlns="http://www.w3.org/2005/sparql-results#"> <head> <variable name="book"/> <variable name="who"/> </head> <results distinct="false" ordered="false"> <result> <binding name="book"><uri>http://www.example/book/book5</uri></binding> <binding name="who"><bnode>r29392923r2922</bnode></binding> </result> ... </sparql> HTTP Message GET /sparql/?query=EncodedQuery HTTP/1.1 Host: www.example User-agent: my-sparql-client/0.1 Knowledge Base
  • 33. Linked Data & Semantic Web Technology SOAP Bindings 33 Query Result HTTP/1.1 200 OK Content-Type: application/soap+xml <?xml version="1.0" encoding="utf-8"?> <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <query-result xmlns="http://www.w3.org/2005/09/sparql-protocol-types/#"> <ns1:sparql xmlns:ns1="http://www.w3.org/2005/sparql-results#"> <ns1:head> <ns1:variable name="z"/> </ns1:head> <ns1:results distinct="false" ordered="false"> <ns1:result> <ns1:binding name="z"> <ns1:literal>Harry Potter and the Chamber of Secrets</ns1:literal> </ns1:binding> </ns1:result> ... </ns1:results> </ns1:sparql> </query-result> </soapenv:Body> </soapenv:Envelope> SOAP Request POST /services/sparql-query HTTP/1.1 Content-Type: application/soap+xml Accept: application/soap+xml, multipart/related, text/* User-Agent: Axis/1.2.1 Host: www.example SOAPAction: "" Content-Length: 438 <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <query-request xmlns="http://www.w3.org/2005/09/sparql-protocol-types/#"> <query>SELECT ?z {?x ?y ?z . FILTER regex(?z, 'Harry')}</query> </query-request> </soapenv:Body> </soapenv:Envelope> Knowledge Base
  • 34. Linked Data & Semantic Web Technology References • http://en.wikipedia.org/wiki/Sparql • http://www.w3.org/TR/rdf-sparql-query/ • http://www.w3.org/TR/rdf-sparql-protocol/ • http://www.w3.org/TR/2008/REC-rdf-sparql-XMLres-20080115/ • http://www.slideshare.net/lysander07/openhpi-semweb03part1 • http://www.slideshare.net/lysander07/open-hpi-semweb03part2 • http://www.slideshare.net/lysander07/openhpi-33-how-to-query-rdfs-sparql3 • http://www.slideshare.net/fabien_gandon/sparql-in-a-nutshell • http://www.slideshare.net/kwangsub/rdf-tutorial-sparql-20091031 34
  • 35. Linked Data & Semantic Web Technology 3535 Dr. Myungjin Lee e-Mail : mjlee@li-st.com Twitter : http://twitter.com/MyungjinLee Facebook : http://www.facebook.com/mjinlee SlideShare : http://www.slideshare.net/onlyjiny/