SlideShare uma empresa Scribd logo
1 de 34
SPARQL




1
Query Languages: SQL
(SQL Query Language)
       A language for querying collections of tuples:
               SELECT SALARY, HIRE_DATE
               FROM EMPS
               WHERE EMP_ID = 13954




                  EMP_ID   NAME     HIRE_DATE    SALARY
                  13954    Joe      2000-04-14   48000
                  10335    Mary     1998-11-23   52000
                  …        …        …            …
                  04182    Bob      2005-02-10   21750

    2
Query Languages: XQuery
(XML Query)
       A language for querying trees of XDM nodes:
            for $e in document(my_employees.xml)
            where $emp/emp/@emp-id = 13954
            return $emp/emp/salary



                                                          < e m p lo ye e s>



                                < e m p>                                   < e m p>   <e m p>   <e m p>


               e m p-id = 1 39 54
                                                                               ...      ...       ...


         < n a m e>          < h ire -d a te>   < s a la ry>



           Joe               2 0 00 -04 -14      4 80 00



    3
Why an RDF Query Language?
Different XML Representations
       XML at a lower level of abstraction than RDF
       There are various ways of syntactically representing an
        RDF statement in XML
       Thus we would require several XQuery queries, e.g.
           //uni:lecturer/uni:title if uni:title element
           //uni:lecturer/@uni:title if uni:title attribute
           Both XML representations equivalent!




    4
Two basic families of SQL-like langages for
RDF(S)
       RDQL
           Implementations: Jena, Sesame, RDFStore, ...
       RQL
           Implementations: RQL, SPARQL, ...




    5
Introduction to RDQL
       RDF Data Query Language
       JDBC/ODBC friendly

       Simple:

          SELECT
                    some information
          FROM
                    somewhere
          WHERE
                    this match
          AND
                    these constraints
          USING
                    these vocabularies


    6
Example




7
Example
       q1 contains a query:
SELECT ?x
WHERE (?x, <http://www.w3.org/2001/vcard-rdf/3.0#FN>, "John Smith")


       For executing q1with a model m1.rdf:
java jena.rdfquery --data m1.rdf --query q1


       The outcome is:
x
=============================
<http://somewhere/JohnSmith/>




    8
Example
       Return all the resources that have property FN and the
        associated values:
SELECT ?x, ?fname
WHERE (?x, <http://www.w3.org/2001/vcard-rdf/3.0#FN>, ?fname)


       The outcome is:
x                                | fname
================================================
<http://somewhere/JohnSmith/>    | "John Smith"
<http://somewhere/SarahJones/>   | "Sarah Jones"
<http://somewhere/MattJones/>    | "Matt Jones"




    9
Example
    Return the first name of Jones:
SELECT ?givenName
WHERE (?y, <http://www.w3.org/2001/vcard-rdf/3.0#Family>, "Jones"),
         (?y, <http://www.w3.org/2001/vcard-rdf/3.0#Given>, ?givenName)


    The outcome is:
givenName
=========
"Matthew"
"Sarah"




    10
URI Prefixes : USING
    RDQL has a syntactic convenience that allows prefix strings
     to be defined in the USING clause :

SELECT ?x
WHERE (?x, vCard:FN, "John Smith")
USING vCard FOR <http://www.w3.org/2001/vcard-rdf/3.0#>

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




    11
Filters
    RDQL has a syntactic convenience that allows prefix strings
     to be defined in the USING clause :

SELECT ?resource
WHERE (?resource, info:age, ?age)
AND ?age >= 24
USING info FOR <http://somewhere/peopleInfo#>




    12
Limitations
    Does not take into account semantics of RDF(S)
    For example:
        ex:human rdfs:subClassOf ex:animal
        ex:student rdfs:subClassOf ex:human
        ex:john rdf:type ex:student

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

         SELECT ?x
         WHERE (<http://example.org/#john>, rdf:type, ?x)
         USING rdf FOR <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

         Yields only:
         <http://example.org/#student>

    Solution: Inference Engines

    13
Why an RDF Query Language?
Different XML Representations
    XML at a lower level of abstraction than RDF
    There are various ways of syntactically representing an
     RDF statement in XML
    Thus we would require several XPath queries, e.g.
        //uni:lecturer/uni:title if uni:title element
        //uni:lecturer/@uni:title if uni:title attribute
        Both XML representations equivalent!




    14
SPARQL
   SPARQL is RDF Query Language
   It provides facilities to:
     extract information in the form of URIs, blank nodes, plain and typed
        literals.
     extract RDF subgraphs.
     construct new RDF graphs based on information in the queried graphs
SPARQL Basic Queries
    SPARQL is based on matching graph patterns
    The simplest graph pattern is the triple pattern :
-    like an RDF triple, but with the possibility of a variable instead of an RDF
     term in the subject, predicate, or object positions
    Combining triple patterns gives a basic graph pattern, where an exact match
     to a graph is needed to fulfill a pattern




    16
Basic graph pattern
   Set of Triple Patterns
     Triple Pattern – similar to an RDF Triple (subject, predicate, object), but any
      component can be a query variable; literal subjects are allowed

                                 ?book dc:title ?title

       Matching a triple pattern to a graph: bindings between variables and RDF
        Terms


   Matching of Basic Graph Patterns
     A Pattern Solution of Graph Pattern GP on graph G is any substitution S
      such that S(GP) is a subgraph of G.
          SELECT ?x ?v
          WHERE { ?x ?x ?v }
                                      rdf:type rdf:type rdf:Property
              x                   v
              rdf:type            rdf:Property
Examples
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?c
WHERE
{
    ?c rdf:type rdfs:Class .
}
 Retrieves all triple patterns, where:
- the property is rdf:type
- the object is rdfs:Class
 Which means that it retrieves all classes




 18
Examples (2)
    Get all instances of a particular class (e.g. course) :

PREFIX uni: <http://www.mydomain.org/uni-ns#>
SELECT ?i
WHERE
{
  ?i rdf:type uni:course .
}




    19
Multiple Matches
                              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> .

PREFIX foaf: <http://xmlns.com/foaf/0.1/>   Query
SELECT ?name ?mbox                                     Group Graph Pattern
WHERE                                                  (set of graph patterns)
{ ?x foaf:name ?name .                                 also!
 ?x foaf:mbox ?mbox }

                 Query Result     name              mbox
                                  "Johnny Lee       <mailto:jlow@example.com>
                                  Outlaw"
                                  "Peter Goodguy" <mailto:peter@example.org>
Using select-from-where
    As in SQL, SPARQL queries have a SELECT-FROM-WHERE structure:
      SELECT specifies the projection: the number and order of retrieved data
      FROM is used to specify the source being queried (optional)
      WHERE imposes constraints on possible solutions in the form of graph
       pattern templates and boolean constraints

    Retrieve all phone numbers of staff members:

            SELECT ?x ?y
            WHERE
            { ?x uni:phone ?y .}

    Here ?x and ?y are variables, and ?x uni:phone ?y represents a resource-
     property-value triple pattern




    21
Implicit Join
    Retrieve all lecturers and their phone numbers:
             SELECT ?x ?y
             WHERE
             { ?x rdf:type uni:Lecturer ;
                       uni:phone ?y . }
    Implicit join: We restrict the second pattern only to those triples, the resource
     of which is in the variable ?x
      Notice the syntax shortcut : a semicolon indicates that the following
        triple shares its subject with the previous one




    22
Implicit join (2)
    The previous query is equivalent to writing:
         SELECT ?x ?y
         WHERE
         {
             ?x rdf:type uni:Lecturer .
             ?x uni:phone ?y .
         }




    23
Explicit Join
    Retrieve the name of all courses taught by the lecturer with ID 949352
     SELECT ?n
     WHERE
     {
         ?x rdf:type uni:Course ;
            uni:isTaughtBy 949352 .
         ?c uni:name ?n .
         FILTER (?c = ?x) .
     }




    24
Basic Graph Pattern - Blank Nodes

             @prefix foaf: <http://xmlns.com/foaf/0.1/> .
    Data
             _:a foaf:name "Alice" .
             _:b foaf:name "Bob" .


PREFIX foaf: <http://xmlns.com/foaf/0.1/>       Query
SELECT ?x ?name
WHERE { ?x foaf:name ?name }


                           x      name
            Query Result   _:c    “Alice“
                           _:d    “Bob”
Group Pattern
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE
{ ?x foaf:name ?name .
 ?x foaf:mbox ?mbox }



PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE
{ {?x foaf:name ?name;
     foaf:mbox ?mbox }
 }
Value Constraints
                         @prefix dc: <http://purl.org/dc/elements/1.1/> .
                Data     @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 .

PREFIX dc: <http://purl.org/dc/elements/1.1/>        Query
PREFIX ns: <http://example.org/ns#>
SELECT ?title ?price
WHERE { ?x ns:price ?price .
         FILTER ?price < 30 .
         ?x dc:title ?title . }
                                            title                      price
                            Query Result
                                            "The Semantic Web"         23
Regular expressions can be used
•   “Find the name and email addresses of authors of a paper
    about SPARQL”



PREFIX dc:     <http://purl.org/dc/elements/1.1/>
PREFIX ldap:   <http://ldap.hp.com/people#>
PREFIX foaf:

SELECT ?name ?name2
{
 WHERE ?doc          dc:title     ?title .
    FILTER regex(?title, “SPARQL”) .
    ?doc          dc:creator   ?reseacher .
    ?researcher   ldap:email      ?email .
    ?researcher   ldap:name       ?name
}




                                                           28
Optional graph patterns
<uni:lecturer rdf:about=“949352”>
     <uni:name>Grigoris Antoniou</uni:name>
</uni:lecturer>
<uni:professor rdf:about=“94318”>
     <uni:name>David Billington</uni:name>
     <uni:email>david@work.example.org</uni:email>
</uni:professor>


    For one lecturer it only lists the name
    For the other it also lists the email address




    29
Optional graph patterns (2)
    All lecturers and their email addresses:
SELECT ?name ?email
WHERE
{ ?x rdf:type uni:Lecturer ;
           uni:name ?name ;
           uni:email ?email .
}
    The result of the previous query would be:

         ?name                          ?email
         David Billington               david@work.example.org


    Grigoris Antoniou is listed as a lecturer, but he has no e-mail address


    30
Optional graph patterns(3)
    As a solution we can adapt the query to use an optional pattern:

     SELECT ?name ?email
     WHERE
     { ?x rdf:type uni:Lecturer ;
           uni:name ?name .
           OPTIONAL { x? uni:email ?email }
     }
    The meaning is roughly “give us the names of lecturers, and if known also their e-
     mail address”
    The result looks like this:

               ?name                           ?email
               Grigoris Antoniou
               David Billington                david@work.example.org

    31
Optional graph patterns (4)
                   @prefix dc: <http://purl.org/dc/elements/1.1/> .
                   @prefix : <http://example.org/book/> .
          Data
                   @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 .

PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX ns: <http://example.org/ns#>
SELECT ?title ?price                              Query
WHERE { ?x dc:title ?title .
         OPTIONAL { ?x ns:price ?price .
                         FILTER ?price < 30 }}    title               price
                                                  “SPARQL Tutorial“
                                Query Result
                                                  "The Semantic Web" 23
Multiple Optional Blocks
                  @prefix foaf: <http://xmlns.com/foaf/0.1/> .
                  @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
           Data
                  @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
                  _:a foaf:name "Alice" .
                  _:a foaf:homepage <http://work.example.org/alice/> .
                  _:b foaf:name "Bob" .
                  _:b foaf:mbox <mailto:bob@work.example> .

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox ?hpage                           Query
WHERE { ?x foaf:name ?name .
         OPTIONAL { ?x foaf:mbox ?mbox }.
OPTIONAL { ?x foaf:homepage ?hpage } }
                                                               Query Result
name      Mbox                            hpage
“Alice“                                   <http://work.example.org/alice/>
“Bob“     <mailto:bob@example.com>
Alternative Graph Patterns
                 @prefix dc10: <http://purl.org/dc/elements/1.0/> .
                 @prefix dc11: <http://purl.org/dc/elements/1.1/> .
         Data
                 _:a dc10:title "SPARQL Query Language Tutorial" .
                 _:b dc11:title "SPARQL Protocol Tutorial" .
                 _:c dc10:title "SPARQL" .
                 _:c dc11:title "SPARQL (updated)" .

PREFIX dc10: <http://purl.org/dc/elements/1.0/>
PREFIX dc11: <http://purl.org/dc/elements/1.1/>                   Query
SELECT ?x ?y
WHERE { { ?book dc10:title ?x } UNION { ?book dc11:title ?y } }   Query Result
x                                       y
                                        "SPARQL (updated)"
                                        "SPARQL Protocol Tutorial"
"SPARQL"
"SPARQL Query Language Tutorial"

Mais conteúdo relacionado

Mais procurados

Graph Data -- RDF and Property Graphs
Graph Data -- RDF and Property GraphsGraph Data -- RDF and Property Graphs
Graph Data -- RDF and Property Graphsandyseaborne
 
Querying the Semantic Web with SPARQL
Querying the Semantic Web with SPARQLQuerying the Semantic Web with SPARQL
Querying the Semantic Web with SPARQLEmanuele Della Valle
 
Data translation with SPARQL 1.1
Data translation with SPARQL 1.1Data translation with SPARQL 1.1
Data translation with SPARQL 1.1andreas_schultz
 
From SQL to SPARQL
From SQL to SPARQLFrom SQL to SPARQL
From SQL to SPARQLGeorge Roth
 
A Preliminary survey of RDF/Neo4j as backends for KnetMiner
A Preliminary survey of RDF/Neo4j as backends for KnetMinerA Preliminary survey of RDF/Neo4j as backends for KnetMiner
A Preliminary survey of RDF/Neo4j as backends for KnetMinerRothamsted Research, UK
 
Querying XML: XPath and XQuery
Querying XML: XPath and XQueryQuerying XML: XPath and XQuery
Querying XML: XPath and XQueryKatrien Verbert
 
The Gremlin in the Graph
The Gremlin in the GraphThe Gremlin in the Graph
The Gremlin in the GraphMarko Rodriguez
 
Types and perl language
Types and perl languageTypes and perl language
Types and perl languageMasahiro Honma
 
Introduction to XPath
Introduction to XPathIntroduction to XPath
Introduction to XPathtorp42
 
What can scala puzzlers teach us
What can scala puzzlers teach usWhat can scala puzzlers teach us
What can scala puzzlers teach usDaniel Sobral
 
SPARQL in a nutshell
SPARQL in a nutshellSPARQL in a nutshell
SPARQL in a nutshellFabien Gandon
 
A Generic Mapping-based Query Translation from SPARQL to Various Target Datab...
A Generic Mapping-based Query Translation from SPARQL to Various Target Datab...A Generic Mapping-based Query Translation from SPARQL to Various Target Datab...
A Generic Mapping-based Query Translation from SPARQL to Various Target Datab...Franck Michel
 

Mais procurados (20)

SWT Lecture Session 2 - RDF
SWT Lecture Session 2 - RDFSWT Lecture Session 2 - RDF
SWT Lecture Session 2 - RDF
 
inheritance
inheritanceinheritance
inheritance
 
Introduction to SPARQL
Introduction to SPARQLIntroduction to SPARQL
Introduction to SPARQL
 
Graph Data -- RDF and Property Graphs
Graph Data -- RDF and Property GraphsGraph Data -- RDF and Property Graphs
Graph Data -- RDF and Property Graphs
 
Querying the Semantic Web with SPARQL
Querying the Semantic Web with SPARQLQuerying the Semantic Web with SPARQL
Querying the Semantic Web with SPARQL
 
Data translation with SPARQL 1.1
Data translation with SPARQL 1.1Data translation with SPARQL 1.1
Data translation with SPARQL 1.1
 
From SQL to SPARQL
From SQL to SPARQLFrom SQL to SPARQL
From SQL to SPARQL
 
A Preliminary survey of RDF/Neo4j as backends for KnetMiner
A Preliminary survey of RDF/Neo4j as backends for KnetMinerA Preliminary survey of RDF/Neo4j as backends for KnetMiner
A Preliminary survey of RDF/Neo4j as backends for KnetMiner
 
Querying XML: XPath and XQuery
Querying XML: XPath and XQueryQuerying XML: XPath and XQuery
Querying XML: XPath and XQuery
 
The Gremlin in the Graph
The Gremlin in the GraphThe Gremlin in the Graph
The Gremlin in the Graph
 
Types and perl language
Types and perl languageTypes and perl language
Types and perl language
 
Adventures in TclOO
Adventures in TclOOAdventures in TclOO
Adventures in TclOO
 
Introduction to XPath
Introduction to XPathIntroduction to XPath
Introduction to XPath
 
XSPARQL CrEDIBLE workshop
XSPARQL CrEDIBLE workshopXSPARQL CrEDIBLE workshop
XSPARQL CrEDIBLE workshop
 
5 rdfs
5 rdfs5 rdfs
5 rdfs
 
XML and XPath details
XML and XPath detailsXML and XPath details
XML and XPath details
 
What can scala puzzlers teach us
What can scala puzzlers teach usWhat can scala puzzlers teach us
What can scala puzzlers teach us
 
SPARQL in a nutshell
SPARQL in a nutshellSPARQL in a nutshell
SPARQL in a nutshell
 
Xpath presentation
Xpath presentationXpath presentation
Xpath presentation
 
A Generic Mapping-based Query Translation from SPARQL to Various Target Datab...
A Generic Mapping-based Query Translation from SPARQL to Various Target Datab...A Generic Mapping-based Query Translation from SPARQL to Various Target Datab...
A Generic Mapping-based Query Translation from SPARQL to Various Target Datab...
 

Destaque

Destaque (8)

media kit
media kitmedia kit
media kit
 
NARYBEK CITY MoU with Uijeonbu city
NARYBEK CITY MoU with Uijeonbu cityNARYBEK CITY MoU with Uijeonbu city
NARYBEK CITY MoU with Uijeonbu city
 
Hola
HolaHola
Hola
 
Metaksan full
Metaksan fullMetaksan full
Metaksan full
 
Chapter 1 introduction
Chapter 1 introductionChapter 1 introduction
Chapter 1 introduction
 
Congratulations andrew!
Congratulations andrew!Congratulations andrew!
Congratulations andrew!
 
Examples of my proffesional skills (in Danish)
Examples of my proffesional skills (in Danish)Examples of my proffesional skills (in Danish)
Examples of my proffesional skills (in Danish)
 
Brug Mindre Tid På Mails
Brug Mindre Tid På MailsBrug Mindre Tid På Mails
Brug Mindre Tid På Mails
 

Semelhante a Sparql

Semantic web meetup – sparql tutorial
Semantic web meetup – sparql tutorialSemantic web meetup – sparql tutorial
Semantic web meetup – sparql tutorialAdonisDamian
 
SPARQL introduction and training (130+ slides with exercices)
SPARQL introduction and training (130+ slides with exercices)SPARQL introduction and training (130+ slides with exercices)
SPARQL introduction and training (130+ slides with exercices)Thomas Francart
 
Semantic Web(Web 3.0) SPARQL
Semantic Web(Web 3.0) SPARQLSemantic Web(Web 3.0) SPARQL
Semantic Web(Web 3.0) SPARQLDaniel D.J. UM
 
TermPicker: Enabling the Reuse of Vocabulary Terms by Exploiting Data from th...
TermPicker: Enabling the Reuse of Vocabulary Terms by Exploiting Data from th...TermPicker: Enabling the Reuse of Vocabulary Terms by Exploiting Data from th...
TermPicker: Enabling the Reuse of Vocabulary Terms by Exploiting Data from th...JohannWanja
 
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 WebShamod Lacoul
 
Two graph data models : RDF and Property Graphs
Two graph data models : RDF and Property GraphsTwo graph data models : RDF and Property Graphs
Two graph data models : RDF and Property Graphsandyseaborne
 
Aidan's PhD Viva
Aidan's PhD VivaAidan's PhD Viva
Aidan's PhD VivaAidan Hogan
 
Towards an RDF Validation Language based on Regular Expression Derivatives
Towards an RDF Validation Language based on Regular Expression DerivativesTowards an RDF Validation Language based on Regular Expression Derivatives
Towards an RDF Validation Language based on Regular Expression DerivativesJose Emilio Labra Gayo
 
The Semantic Web #10 - SPARQL
The Semantic Web #10 - SPARQLThe Semantic Web #10 - SPARQL
The Semantic Web #10 - SPARQLMyungjin Lee
 
2011 4IZ440 Semantic Web – RDF, SPARQL, and software APIs
2011 4IZ440 Semantic Web – RDF, SPARQL, and software APIs2011 4IZ440 Semantic Web – RDF, SPARQL, and software APIs
2011 4IZ440 Semantic Web – RDF, SPARQL, and software APIsJosef Petrák
 
Rdf data-model-and-storage
Rdf data-model-and-storageRdf data-model-and-storage
Rdf data-model-and-storage灿辉 葛
 
Triplestore and SPARQL
Triplestore and SPARQLTriplestore and SPARQL
Triplestore and SPARQLLino Valdivia
 
A Semantic Multimedia Web (Part 2)
A Semantic Multimedia Web (Part 2)A Semantic Multimedia Web (Part 2)
A Semantic Multimedia Web (Part 2)Raphael Troncy
 

Semelhante a Sparql (20)

Semantic web meetup – sparql tutorial
Semantic web meetup – sparql tutorialSemantic web meetup – sparql tutorial
Semantic web meetup – sparql tutorial
 
SPARQL introduction and training (130+ slides with exercices)
SPARQL introduction and training (130+ slides with exercices)SPARQL introduction and training (130+ slides with exercices)
SPARQL introduction and training (130+ slides with exercices)
 
Semantic Web(Web 3.0) SPARQL
Semantic Web(Web 3.0) SPARQLSemantic Web(Web 3.0) SPARQL
Semantic Web(Web 3.0) SPARQL
 
TermPicker: Enabling the Reuse of Vocabulary Terms by Exploiting Data from th...
TermPicker: Enabling the Reuse of Vocabulary Terms by Exploiting Data from th...TermPicker: Enabling the Reuse of Vocabulary Terms by Exploiting Data from th...
TermPicker: Enabling the Reuse of Vocabulary Terms by Exploiting Data from th...
 
Introduction to SPARQL
Introduction to SPARQLIntroduction to SPARQL
Introduction to SPARQL
 
Semantic Web
Semantic WebSemantic Web
Semantic Web
 
Semantic Web
Semantic WebSemantic Web
Semantic Web
 
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
 
Two graph data models : RDF and Property Graphs
Two graph data models : RDF and Property GraphsTwo graph data models : RDF and Property Graphs
Two graph data models : RDF and Property Graphs
 
Aidan's PhD Viva
Aidan's PhD VivaAidan's PhD Viva
Aidan's PhD Viva
 
Sparql
SparqlSparql
Sparql
 
Towards an RDF Validation Language based on Regular Expression Derivatives
Towards an RDF Validation Language based on Regular Expression DerivativesTowards an RDF Validation Language based on Regular Expression Derivatives
Towards an RDF Validation Language based on Regular Expression Derivatives
 
The Semantic Web #10 - SPARQL
The Semantic Web #10 - SPARQLThe Semantic Web #10 - SPARQL
The Semantic Web #10 - SPARQL
 
ShEx by Example
ShEx by ExampleShEx by Example
ShEx by Example
 
2011 4IZ440 Semantic Web – RDF, SPARQL, and software APIs
2011 4IZ440 Semantic Web – RDF, SPARQL, and software APIs2011 4IZ440 Semantic Web – RDF, SPARQL, and software APIs
2011 4IZ440 Semantic Web – RDF, SPARQL, and software APIs
 
Rdf data-model-and-storage
Rdf data-model-and-storageRdf data-model-and-storage
Rdf data-model-and-storage
 
Jena Programming
Jena ProgrammingJena Programming
Jena Programming
 
Triplestore and SPARQL
Triplestore and SPARQLTriplestore and SPARQL
Triplestore and SPARQL
 
A Semantic Multimedia Web (Part 2)
A Semantic Multimedia Web (Part 2)A Semantic Multimedia Web (Part 2)
A Semantic Multimedia Web (Part 2)
 
SWT Lecture Session 5 - RDFS
SWT Lecture Session 5 - RDFSSWT Lecture Session 5 - RDFS
SWT Lecture Session 5 - RDFS
 

Sparql

  • 2. Query Languages: SQL (SQL Query Language)  A language for querying collections of tuples: SELECT SALARY, HIRE_DATE FROM EMPS WHERE EMP_ID = 13954 EMP_ID NAME HIRE_DATE SALARY 13954 Joe 2000-04-14 48000 10335 Mary 1998-11-23 52000 … … … … 04182 Bob 2005-02-10 21750 2
  • 3. Query Languages: XQuery (XML Query)  A language for querying trees of XDM nodes: for $e in document(my_employees.xml) where $emp/emp/@emp-id = 13954 return $emp/emp/salary < e m p lo ye e s> < e m p> < e m p> <e m p> <e m p> e m p-id = 1 39 54 ... ... ... < n a m e> < h ire -d a te> < s a la ry> Joe 2 0 00 -04 -14 4 80 00 3
  • 4. Why an RDF Query Language? Different XML Representations  XML at a lower level of abstraction than RDF  There are various ways of syntactically representing an RDF statement in XML  Thus we would require several XQuery queries, e.g.  //uni:lecturer/uni:title if uni:title element  //uni:lecturer/@uni:title if uni:title attribute  Both XML representations equivalent! 4
  • 5. Two basic families of SQL-like langages for RDF(S)  RDQL  Implementations: Jena, Sesame, RDFStore, ...  RQL  Implementations: RQL, SPARQL, ... 5
  • 6. Introduction to RDQL  RDF Data Query Language  JDBC/ODBC friendly  Simple: SELECT some information FROM somewhere WHERE this match AND these constraints USING these vocabularies 6
  • 8. Example  q1 contains a query: SELECT ?x WHERE (?x, <http://www.w3.org/2001/vcard-rdf/3.0#FN>, "John Smith")  For executing q1with a model m1.rdf: java jena.rdfquery --data m1.rdf --query q1  The outcome is: x ============================= <http://somewhere/JohnSmith/> 8
  • 9. Example  Return all the resources that have property FN and the associated values: SELECT ?x, ?fname WHERE (?x, <http://www.w3.org/2001/vcard-rdf/3.0#FN>, ?fname)  The outcome is: x | fname ================================================ <http://somewhere/JohnSmith/> | "John Smith" <http://somewhere/SarahJones/> | "Sarah Jones" <http://somewhere/MattJones/> | "Matt Jones" 9
  • 10. Example  Return the first name of Jones: SELECT ?givenName WHERE (?y, <http://www.w3.org/2001/vcard-rdf/3.0#Family>, "Jones"), (?y, <http://www.w3.org/2001/vcard-rdf/3.0#Given>, ?givenName)  The outcome is: givenName ========= "Matthew" "Sarah" 10
  • 11. URI Prefixes : USING  RDQL has a syntactic convenience that allows prefix strings to be defined in the USING clause : SELECT ?x WHERE (?x, vCard:FN, "John Smith") USING vCard FOR <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?givenName WHERE (?y, vCard:Family, "Smith"), (?y, vCard:Given, ?givenName) USING vCard FOR <http://www.w3.org/2001/vcard-rdf/3.0#> 11
  • 12. Filters  RDQL has a syntactic convenience that allows prefix strings to be defined in the USING clause : SELECT ?resource WHERE (?resource, info:age, ?age) AND ?age >= 24 USING info FOR <http://somewhere/peopleInfo#> 12
  • 13. Limitations  Does not take into account semantics of RDF(S)  For example: ex:human rdfs:subClassOf ex:animal ex:student rdfs:subClassOf ex:human ex:john rdf:type ex:student Query: “ To which class does the resource John belong?” Expected answer: ex:student, ex:human, ex:animal However, the query: SELECT ?x WHERE (<http://example.org/#john>, rdf:type, ?x) USING rdf FOR <http://www.w3.org/1999/02/22-rdf-syntax-ns#> Yields only: <http://example.org/#student>  Solution: Inference Engines 13
  • 14. Why an RDF Query Language? Different XML Representations  XML at a lower level of abstraction than RDF  There are various ways of syntactically representing an RDF statement in XML  Thus we would require several XPath queries, e.g.  //uni:lecturer/uni:title if uni:title element  //uni:lecturer/@uni:title if uni:title attribute  Both XML representations equivalent! 14
  • 15. SPARQL  SPARQL is RDF Query Language  It provides facilities to:  extract information in the form of URIs, blank nodes, plain and typed literals.  extract RDF subgraphs.  construct new RDF graphs based on information in the queried graphs
  • 16. SPARQL Basic Queries  SPARQL is based on matching graph patterns  The simplest graph pattern is the triple pattern : - like an RDF triple, but with the possibility of a variable instead of an RDF term in the subject, predicate, or object positions  Combining triple patterns gives a basic graph pattern, where an exact match to a graph is needed to fulfill a pattern 16
  • 17. Basic graph pattern  Set of Triple Patterns  Triple Pattern – similar to an RDF Triple (subject, predicate, object), but any component can be a query variable; literal subjects are allowed ?book dc:title ?title  Matching a triple pattern to a graph: bindings between variables and RDF Terms  Matching of Basic Graph Patterns  A Pattern Solution of Graph Pattern GP on graph G is any substitution S such that S(GP) is a subgraph of G. SELECT ?x ?v WHERE { ?x ?x ?v } rdf:type rdf:type rdf:Property x v rdf:type rdf:Property
  • 18. Examples PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?c WHERE { ?c rdf:type rdfs:Class . }  Retrieves all triple patterns, where: - the property is rdf:type - the object is rdfs:Class  Which means that it retrieves all classes 18
  • 19. Examples (2)  Get all instances of a particular class (e.g. course) : PREFIX uni: <http://www.mydomain.org/uni-ns#> SELECT ?i WHERE { ?i rdf:type uni:course . } 19
  • 20. Multiple Matches 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> . PREFIX foaf: <http://xmlns.com/foaf/0.1/> Query SELECT ?name ?mbox Group Graph Pattern WHERE (set of graph patterns) { ?x foaf:name ?name . also! ?x foaf:mbox ?mbox } Query Result name mbox "Johnny Lee <mailto:jlow@example.com> Outlaw" "Peter Goodguy" <mailto:peter@example.org>
  • 21. Using select-from-where  As in SQL, SPARQL queries have a SELECT-FROM-WHERE structure:  SELECT specifies the projection: the number and order of retrieved data  FROM is used to specify the source being queried (optional)  WHERE imposes constraints on possible solutions in the form of graph pattern templates and boolean constraints  Retrieve all phone numbers of staff members: SELECT ?x ?y WHERE { ?x uni:phone ?y .}  Here ?x and ?y are variables, and ?x uni:phone ?y represents a resource- property-value triple pattern 21
  • 22. Implicit Join  Retrieve all lecturers and their phone numbers: SELECT ?x ?y WHERE { ?x rdf:type uni:Lecturer ; uni:phone ?y . }  Implicit join: We restrict the second pattern only to those triples, the resource of which is in the variable ?x  Notice the syntax shortcut : a semicolon indicates that the following triple shares its subject with the previous one 22
  • 23. Implicit join (2)  The previous query is equivalent to writing: SELECT ?x ?y WHERE { ?x rdf:type uni:Lecturer . ?x uni:phone ?y . } 23
  • 24. Explicit Join  Retrieve the name of all courses taught by the lecturer with ID 949352 SELECT ?n WHERE { ?x rdf:type uni:Course ; uni:isTaughtBy 949352 . ?c uni:name ?n . FILTER (?c = ?x) . } 24
  • 25. Basic Graph Pattern - Blank Nodes @prefix foaf: <http://xmlns.com/foaf/0.1/> . Data _:a foaf:name "Alice" . _:b foaf:name "Bob" . PREFIX foaf: <http://xmlns.com/foaf/0.1/> Query SELECT ?x ?name WHERE { ?x foaf:name ?name } x name Query Result _:c “Alice“ _:d “Bob”
  • 26. Group Pattern PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name ?mbox WHERE { ?x foaf:name ?name . ?x foaf:mbox ?mbox } PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name ?mbox WHERE { {?x foaf:name ?name; foaf:mbox ?mbox } }
  • 27. Value Constraints @prefix dc: <http://purl.org/dc/elements/1.1/> . Data @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 . PREFIX dc: <http://purl.org/dc/elements/1.1/> Query PREFIX ns: <http://example.org/ns#> SELECT ?title ?price WHERE { ?x ns:price ?price . FILTER ?price < 30 . ?x dc:title ?title . } title price Query Result "The Semantic Web" 23
  • 28. Regular expressions can be used • “Find the name and email addresses of authors of a paper about SPARQL” PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX ldap: <http://ldap.hp.com/people#> PREFIX foaf: SELECT ?name ?name2 { WHERE ?doc dc:title ?title . FILTER regex(?title, “SPARQL”) . ?doc dc:creator ?reseacher . ?researcher ldap:email ?email . ?researcher ldap:name ?name } 28
  • 29. Optional graph patterns <uni:lecturer rdf:about=“949352”> <uni:name>Grigoris Antoniou</uni:name> </uni:lecturer> <uni:professor rdf:about=“94318”> <uni:name>David Billington</uni:name> <uni:email>david@work.example.org</uni:email> </uni:professor>  For one lecturer it only lists the name  For the other it also lists the email address 29
  • 30. Optional graph patterns (2)  All lecturers and their email addresses: SELECT ?name ?email WHERE { ?x rdf:type uni:Lecturer ; uni:name ?name ; uni:email ?email . }  The result of the previous query would be: ?name ?email David Billington david@work.example.org  Grigoris Antoniou is listed as a lecturer, but he has no e-mail address 30
  • 31. Optional graph patterns(3)  As a solution we can adapt the query to use an optional pattern: SELECT ?name ?email WHERE { ?x rdf:type uni:Lecturer ; uni:name ?name . OPTIONAL { x? uni:email ?email } }  The meaning is roughly “give us the names of lecturers, and if known also their e- mail address”  The result looks like this: ?name ?email Grigoris Antoniou David Billington david@work.example.org 31
  • 32. Optional graph patterns (4) @prefix dc: <http://purl.org/dc/elements/1.1/> . @prefix : <http://example.org/book/> . Data @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 . PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX ns: <http://example.org/ns#> SELECT ?title ?price Query WHERE { ?x dc:title ?title . OPTIONAL { ?x ns:price ?price . FILTER ?price < 30 }} title price “SPARQL Tutorial“ Query Result "The Semantic Web" 23
  • 33. Multiple Optional Blocks @prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . Data @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . _:a foaf:name "Alice" . _:a foaf:homepage <http://work.example.org/alice/> . _:b foaf:name "Bob" . _:b foaf:mbox <mailto:bob@work.example> . PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name ?mbox ?hpage Query WHERE { ?x foaf:name ?name . OPTIONAL { ?x foaf:mbox ?mbox }. OPTIONAL { ?x foaf:homepage ?hpage } } Query Result name Mbox hpage “Alice“ <http://work.example.org/alice/> “Bob“ <mailto:bob@example.com>
  • 34. Alternative Graph Patterns @prefix dc10: <http://purl.org/dc/elements/1.0/> . @prefix dc11: <http://purl.org/dc/elements/1.1/> . Data _:a dc10:title "SPARQL Query Language Tutorial" . _:b dc11:title "SPARQL Protocol Tutorial" . _:c dc10:title "SPARQL" . _:c dc11:title "SPARQL (updated)" . PREFIX dc10: <http://purl.org/dc/elements/1.0/> PREFIX dc11: <http://purl.org/dc/elements/1.1/> Query SELECT ?x ?y WHERE { { ?book dc10:title ?x } UNION { ?book dc11:title ?y } } Query Result x y "SPARQL (updated)" "SPARQL Protocol Tutorial" "SPARQL" "SPARQL Query Language Tutorial"