Successfully reported this slideshow.

The Semantic Web #10 - SPARQL

1,609 views

Published on

This is a lecture note #10 for my class of Graduate School of Yonsei University, Korea.
It describes SPARQL to retrieve and manipulate data stored in Resource Description Framework format

Published in: Education
  • Be the first to comment

The Semantic Web #10 - SPARQL

  1. 1. Linked Data & Semantic Web Technology The Semantic Web Part 10. SPARQL Dr. Myungjin Lee
  2. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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/

×