SPARQL - PowerPoint PPT Presentation

Loading...

PPT – SPARQL PowerPoint presentation | free to view - id: 1cc84b-M2M2M



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

SPARQL

Description:

We use the TURTLE format for triples, this allows the use of prefixes in IRI's ... A prefixed name is mapped to an IRI by concatenating the IRI associated with the ... – PowerPoint PPT presentation

Number of Views:97
Avg rating:3.0/5.0
Slides: 55
Provided by: vaibhavk
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: SPARQL


1
  • SPARQL
  • All slides are adapted from the W3C
    Recommendation
  • SPARQL Query Language for RDF
  • Web link http//www.w3.org/TR/rdf-sparql-query/

2
Overview
3
SPARQL
  • RDF is a directed, labeled graph representation
    of the information on the Web
  • SPARQL expresses queries across multiple and
    highly heterogeneous data sources that contain
    RDF
  • SPARQL is used for querying the graphs for
    patterns

4
SPARQL
  • SPARQL also supports extensible value testing and
    constraining queries by source RDF graph
  • SPARQL results are result sets or RDF graphs

5
  • 1. Introduction

6
Introduction
  • RDF is a data representation language for the
    information on the Web
  • RDF is used to represent diverse information such
    as social networks, information about people etc
  • Another feature of RDF is that it can be used to
    merge these 'islands' of diverse data
  • SPARQL is a language designed to query RDF

7
Introduction
  • SPARQL is designed to meet the requirements
    identified by the RDF Data Access Working Group
  • SPARQL also relates to
  • SPARQL Protocol for RDF SPROT remote protocol
    for issuing SPARQL queries and receiving results
  • SPARQL Query Results XML Format RESULTS
    defines an XML format for representing results of
    SPARQL SELECT and ASK queries

8
  • Conventions for slides

9
Conventions
  • Namespaces
  • Prefix IRI
  • rdf http//www.w3.org/1999/02/22-rdf-syntax-
    ns
  • rdfs http//www.w3.org/2000/01/rdf-schema
  • rdf http//www.w3.org/2001/XMLSchema
  • fn http//www.w3.org/2005/xpath-functions

10
Conventions
  • Data Description
  • We use the TURTLE format for triples, this allows
    the use of prefixes in IRI's
  • _at_prefix dc lthttp//purl.org/dc/elements/1.1/gt
    .
  • _at_prefix lthttp//example.org/book/gt .
  • book1 dctitle "SPARQL Tutorial" .

11
Conventions
  • Result Descriptions
  • All results are represented in tabular form
  • We define the use of a binding which is an
    association between a variable and the RDF term
  • In the example above, the variable x' is bound
    to the RDF term Alice', the variable y' is
    bound to the RDF lthttp//example/agt' and the
    variable z' is not bound to any RDF term

12
Conventions
  • Terminology used
  • IRIs
  • Literals
  • Lexical form
  • Plain literal
  • Language tag
  • Typed Literal
  • Datatype IRI
  • Blank node

13
  • 2. Writing Simple Queries

14
Writing Simple Queries
  • The most basic form of a SPARQL query is the
    basic graph pattern which is nothing but a set of
    triples where each of the subject, predicate or
    object may be a variable
  • We find a match of the basic graph pattern in the
    given subgraph when RDF terms in a subgraph match
    the variables in the query resulting in a RDF
    graph that matches the given subgraph

15
Writing Simple Queries
  • A simple example
  • Data
  • lthttp//example.org/book/book1gt
  • lthttp//purl.org/dc/elements/1.1/titlegt
    "SPARQL Tutorial" .
  • Query
  • SELECT ?title
  • WHERE lthttp//example.org/book/book1gt
    lthttp//purl.org/dc/elements/1.1/titlegt
    ?title .
  • Result

16
Lab
  • A query to select a subject given its predicate
    and object
  • SELECT ?x
    WHERE

    ?x
    lthttp//www.w3.org/2001/vcard-rdf/3.0FNgt "Sarah
    Jones"

17
Writing Simple Queries
  • A simple example
  • We see that the given data graph contains the URI
    for a book with a predicate identifying the title
    of the book
  • The SPARQL query consists of the SELECT clause
    that identifies the variables that should appear
    in the query result and the WHERE clause that
    identifies the basic graph pattern that should be
    used to match against the data graph
  • In the above example the basic graph pattern
    consists of a simple triple pattern with a
    variable for the object

18
Writing Simple Queries
  • An example with multiple matching results
  • Data
  • _at_prefix foaf lthttp//xmlns.com/foaf/0.1/gt .
  • _a foafname "Johnny Lee Outlaw" .
  • _a foafmbox ltmailtojlow_at_example.comgt .
  • _b foafname "Peter Goodguy" .
  • _b foafmbox ltmailtopeter_at_example.orggt .
  • _c foafmbox ltmailtocarol_at_example.orggt .
  • Query
  • PREFIX foaf http//xmlns.com/foaf/0.1/
  • SELECT ?name ?mbox
  • WHERE ?x foafname ?name .
  • ?x foafmbox ?mbox

19
Writing Simple Queries
  • An example with multiple matching results
  • Results
  • The query result may have 0, 1, or many solutions
  • Each solution gives one way to match the basic
    graph pattern against the data, in the above
    example there are two ways to bind variables to
    the RDF terms
  • _a foafname "Johnny Lee Outlaw" .
  • _a foafbox ltmailtojlow_at_example.comgt .
  • _b foafname "Peter Goodguy" .
  • _b foafbox ltmailtopeter_at_example.orggt .

20
Lab
  • A query to select objects given any subject and a
    property based on some PREFIX
  • PREFIX info lthttp//somewhere/peopleInfogt
    SELECT ?z
    WHERE
    ?element infoage ?z .

21
Lab
  • A query to select objects based on a join
    condition
  • PREFIX vCard lthttp//www.w3.org/2001/vcard-rdf/3.
    0gt SELECT ?x ?z
    WHERE



    ?element
    vCardFamily ?x .
    ?element vCardGiven ?z
    .

22
Matching RDF Literals
  • Consider the data below
  • _at_prefix dt lthttp//example.org/datatypegt
  • _at_prefix ns lthttp//example.org/nsgt .
  • _at_prefix lthttp//example.org/nsgt .
  • _at_prefix xsd lthttp//www.w3.org/2001/XMLSchemagt
    .
  • x nsp "cat"_at_en .
  • y nsp "42"xsdinteger .
  • z nsp "abc"dtspecialDatatype .
  • Note that, in Turtle, "cat"_at_en is an RDF literal
    with a lexical form "cat" and a language en
    "42"xsdinteger is a typed literal with the
    datatype http//www.w3.org/2001/XMLSchemainteger
    and "abc"dtspecialDatatype is a typed literal
    with the datatype http//example.org/datatypespec
    ialDatatype.

23
Matching RDF Literals
  • Matching Literals with language tags
  • Language tags are expressed using _at_ and the
    language tag
  • The following query has no solution because cat
    is not the same as cat_at_en
  • Query
  • SELECT ?v WHERE ?v ?p "cat"
  • Result
  • The query below finds a solution because variable
    v is bound to x and the language tag is
    specified and matches the given data
  • Query
  • SELECT ?v WHERE ?v ?p "cat_at_en
  • Result

24
Matching RDF Literals
  • Matching Literals with Numeric Types
  • Integers in a SPARQL query indicate an RDF typed
    literal with the datatype xsdinteger
  • Example 42 is a shortened form of
    "42"lthttp//www.w3.org/2001/XMLSchemaintegergt
  • The following query has a solution with the
    variable v bound to y
  • Query
  • SELECT ?v WHERE ?v ?p 42
  • Result

25
Matching RDF Literals
  • Matching Literals with Arbitrary Datatypes
  • The following query has a solution with the
    variable v bound to x
  • The query processor does not have to have any
    understanding of the values in the space of the
    datatype. The lexical form and the datatype IRI
    both match hence the literal matches
  • Query
  • SELECT ?v
  • WHERE ?v ?p "abc"lthttp//example.org/datatype
    specialDatatypegt
  • Result

26
Lab
  • Select objects based on a stronger join condition
    with a matching literal value
  • PREFIX info lthttp//somewhere/peopleInfogt
    PREFIX vCard lthttp//www.w3.org/20
    01/vcard-rdf/3.0gt SELECT ?x

    WHERE



    ?element vCardFN ?x .

    ?element infoage "43" .


27
Blank Node Labels in Query Results
  • Query results can contain blank nodes
  • Blank nodes are written as _ followed by a
    blank node label
  • Blank node labels are scoped to a result set, or
    for the CONSTRUCT query form to a result graph
  • Data
  • _at_prefix foaf lthttp//xmlns.com/foaf/0.1/gt .
  • _a foafname "Alice" .
  • _b foafname "Bob" .
  • Query
  • PREFIX foaf lthttp//xmlns.com/foaf/0.1/gt
  • SELECT ?x ?name
  • WHERE ?x foafname ?name
  • Result

28
Blank Node Labels in Query Results
  • The results could equally be given with different
    blank node labels because the labels in the
    results only indicate if RDF terms in the
    solutions are the same or not
  • These two results have the same information the
    blank nodes used to match the query are different
    in the two solutions
  • There need not be any relation between the label
    _a in the result set and a blank node in the
    data graph with the same label
  • An application writer should not expect blank
    node labels in a particular query to refer to a
    particular blank node in the data

29
Lab
  • Finding blank nodes
  • PREFIX vCard lthttp//www.w3.org/2001/vcard-rdf/3.
    0gt SELECT ?y
    WHERE




    ?element vCardN ?y .

30
Lab
  • Finding blank nodes with a specific condition
  • PREFIX info lthttp//somewhere/peopleInfogt
    PREFIX vCard lthttp//www.w3.org/20
    01/vcard-rdf/3.0gt SELECT ?x ?y

    WHERE



    ?element infoage ?x

    ?element vCardN ?y .


31
Building RDF Graphs
  • SPARQL has several query forms
  • SELECT query returns variable bindings
  • CONSTRUCT query returns an RDF graph
  • The graph is built on a template which is used to
    generate RDF triples based on the results of
    matching the graph pattern of the query
  • Data
  • _at_prefix org lthttp//example.com/nsgt .
  • _a orgemployeeName "Alice" .
  • _a orgemployeeId 12345 .
  • _b orgemployeeName "Bob" .
  • _b orgemployeeId 67890 .
  • Query
  • PREFIX foaf lthttp//xmlns.com/foaf/0.1/gt
  • PREFIX org lthttp//example.com/nsgt
  • CONSTRUCT ?x foafname ?name
  • WHERE ?x orgemployeeName ?name
  • Result
  • _at_prefix org lthttp//example.com/nsgt .
  • _x foafname "Alice" .
  • _y foafname "Bob" .

32
  • 3. RDF Term Constraints

33
RDF Term Constraints
  • Graph pattern matching produces a solution
    sequence, where each solution has a set of
    bindings of variables to RDF terms
  • SPARQL FILTERS restrict solutions to those for
    which the filter expression evaluates to TRUE
  • Data
  • _at_prefix dc lthttp//purl.org/dc/elements/1.1/gt
    .
  • _at_prefix lthttp//example.org/book/gt .
  • _at_prefix ns lthttp//example.org/nsgt .
  • book1 dctitle "SPARQL Tutorial" .
  • book1 nsprice 42 .
  • book2 dctitle "The Semantic Web" .
  • book2 nsprice 23 .

34
Restricting the Values of Strings
  • SPARQL FILTER functions like regex can test RDF
    literals
  • regex matches only plain literals with no
    language tags
  • regex can be used to match the lexical forms of
    other literals using str
  • Query
  • PREFIX dc lthttp//purl.org/dc/elements/1.1/gt
  • SELECT ?title
  • WHERE ?x dctitle ?title FILTER
    regex(?title, "SPARQL")
  • Result
  • Regular expressions can be made case-insensitive
    with the i flag
  • Query
  • PREFIX dc lthttp//purl.org/dc/elements/1.1/gt
  • SELECT ?title
  • WHERE ?x dctitle ?title FILTER
    regex(?title, web , i)
  • Result

35
Restricting Numeric Values
  • SPARQL FILTERs can restrict on arithmetic
    expressions
  • Query
  • PREFIX dc lthttp//purl.org/dc/elements/1.1/gt
  • PREFIX ns lthttp//example.org/nsgt
  • SELECT ?title ?price
  • WHERE ?x nsprice ?price .
  • FILTER (?price lt 30.5) ?x dctitle
    ?title .
  • Result
  • By constraining the price variable, only book2
    matches the query because only book2 has a price
    less than 30.5, as the filter condition requires

36
Other Term Constraints
  • In addition to numeric types, SPARQL supports
    type xsdstring, xsdboolean, and, xsddateTime

37
  • 4. SPARQL Syntax

38
Syntax for IRIs
  • IRIs are a generalization of URIs and are fully
    compatible with URIs and URLs
  • SPARQL terms include IRIs
  • RDF URI references containing "lt", "gt", '"'
    (double quote), space, "", "", "", "", "",
    and "" are not IRIs
  • The behavior of a SPARQL query against RDF
    statements composed of such RDF URI references is
    not defined
  • Prefixed Names
  • The PREFIX keyword associates a prefix label with
    an IRI
  • A prefixed name is a prefix label and a local
    part, separated by a colon "
  • A prefixed name is mapped to an IRI by
    concatenating the IRI associated with the prefix
    and the local part
  • The prefix label or the local part may be empty
  • Relative IRIs
  • Relative IRIs are combined with base IRIs using
    the keyword BASE
  • The following all represent the same IRI
  • lthttp//example.org/book/book1gt
  • BASE lthttp//example.org/book/gt
  • ltbook1gt
  • PREFIX book lthttp//example.org/book/gt
  • bookbook1

39
Syntax for Literals
  • The general syntax for literals is a string (
    enclosed in either double quotes, , or single
    quotes, ), with either an optional language
    tag ( introduced by _at_ ) or an optional datatype
    IRI or prefixed name ( introduced by )?
  • Integers can be written directly ( without
    quotation marks and an explicit datatype IRI )
    and are interpreted as typed literals of datatype
    xsdinteger, decimal numbers for which there is
    . in the number but no exponent are interpreted
    as xsddecimal and numbers with exponents are
    interpreted as xsddouble.
  • Values of xsdboolean can be written as true or
    false
  • To facilitate writing literal values which
    themselves contain quotation marks or which are
    long and contain newline characters, SPARQL
    provides an additional quoting construct in which
    literals are enclosed in three single- or
    double-quotation marks
  • Examples
  • chat
  • chat_at_en
  • Integer, Decimal, Double, and, Boolean are
    equivalent to a typed literal with the lexical
    value of the token and the corresponding datatype
    ( xsdinteger, xsddecimal, xsddouble, and,
    xsdboolean )?

40
Syntax for Query Variables
  • Query variables in SPARQL have global scope use
    of a given variable name anywhere in a query
    identifies the same variable
  • Variables are prefixed by either ? or "" the
    "?" or "" is not part of the variable name
  • In a query, abc and ?abc identify the same
    variable

41
Syntax for Blank Nodes
  • Blank nodes in graph patterns act as
    non-distinguished variables, not as references to
    specific blank nodes in the data being queried
  • Blank nodes are indicated by either the label
    form, such as "_abc", or the abbreviated form "
    ". A blank node that is used in only one place
    in the query syntax can be indicated with . A
    unique blank node will be used to form the triple
    pattern. Blank node labels are written as "_abc"
    for a blank node with label "abc". The same blank
    node label cannot be used in two different basic
    graph patterns in the same query.
  • The p v construct can be used in triple
    patterns. It creates a blank node label which is
    used as the subject of all contained
    predicate-object pairs. The created blank node
    can also be used in further triple patterns in
    the subject and object positions.
  • p v . and p v . allocate a blank
    node _b57 p v .
  • This blank node can be used as the subject or
    object of further triple patterns
  • As subject, p v q w . _b57 p v .
    and _b57 q w .
  • As object, x q p v . x q _b57 . and
    _b57 q w .
  • Abbreviations can be combined with other
    abbreviations for common subjects and predicates
  • foafname ?name foafmbox ltmailtoalice_at_exampl
    e.orggt is equivalent to _b18 foafname ?name .
  • _b18 foafmbox ltmailtoalice_at_example.orggt .

42
Syntax for Triple Patterns
  • Triple patterns are written as a
    whitespace-separated list of a subject,
    predicate, and, object there are abbreviated
    ways of writing some common triple pattern
    constructs
  • The following represent the same query
  • PREFIX dc lthttp//purl.org/dc/elements/1.1/gt
  • SELECT ?title
  • WHERE lthttp//example.org/book/book1gt
    dctitle ?title
  • PREFIX dc lthttp//purl.org/dc/elements/1.1/gt
  • PREFIX lthttp//example.org/book/gt
  • SELECT title
  • WHERE book1 dctitle title
  • BASE lthttp//example.org/book/gt
  • PREFIX dc lthttp//purl.org/dc/elements/1.1/gt
  • SELECT title
  • WHERE ltbook1gt dctitle ?title

43
Predicate-Object Lists
  • Triple patterns with a common subject can be
    written so that the subject is only written once
    and is used for more than one triple pattern by
    employing the notation
  • The triple pattern
  • ?x foafname ?name
  • foafmbox ?mbox .
  • This is the same as writing the triple patterns
  • ?x foafname ?name .
  • ?x foafmbox ?mbox .

44
Object Lists
  • If triple patterns share both subject and
    predicate, the objects may be separated by ,
  • The triple pattern
  • ?x foafnick "Alice" , "Alice_" .
  • This is the same as writing the triple patterns
  • ?x foafnick "Alice" .
  • ?x foafnick "Alice_" .
  • Object lists can be combined with
    predicate-object lists
  • ?x foafname ?name foafnick "Alice" ,
    "Alice_" .
  • This is equivalent to
  • ?x foafname ?name .
  • ?x foafnick "Alice" .
  • ?x foafnick "Alice_" .

45
RDF Collections
  • RDF Collections can be written in patterns using
    the syntax (element1, element2, )
  • The form "()" is an alternative for the IRI
    http//www.w3.org/1999/02/22-rdf-syntax-nsnil.
  • When used with collection elements, such as (1 ?x
    3 4), triple patterns with blank nodes are
    allocated for the collection.
  • The blank node at the head of the collection can
    be used as a subject or object in other triple
    patterns.
  • The blank nodes allocated by the collection
    syntax do not occur elsewhere in the query.
  • The triple pattern
  • (1 ?x 3 4) p "w" .
  • is syntactic sugar for (noting that b0, b1, b2
    and b3 do not occur anywhere else in the query)
  • _b0 rdffirst 1 rdfrest _b1 .
  • _b1 rdffirst ?x rdfrest _b2 .
  • _b2 rdffirst 3 rdfrest _b3 .
  • _b3 rdffirst 4 rdfrest rdfnil
    .
  • _b0 p "w" .

46
RDF Collections
  • RDF Collections can be nested and can involve
    other syntatic forms
  • The triple pattern
  • (1 p q ( 2 ) ) .
  • is syntactic sugar for
  • _b0 rdffirst 1 rdfrest _b1 .
  • _b1 rdffirst _b2 .
  • _b2 p q .
  • _b1 rdfrest _b3 .
  • _b3 rdffirst _b4 .
  • _b4 rdffirst 2 rdfrest rdfnil
    .
  • _b3 rdfrest rdfnil .

47
Lab
  • Query to find the given name when the family name
    is given using a collection
  • SELECT ?givenName
    WHERE (?y,
    lthttp//www.w3.org/2001/vcard-rdf/3.0Familygt,
    "Smith") , (?y, lthttp//www.w3.org/200
    1/vcard-rdf/3.0Givengt, ?givenName)?

48
Lab
  • Query to find the top level properties for John
    Smith
  • SELECT ?property
    WHERE (?person,
    ltvCardFNgt, "John Smith") ,
    (?person, ?property, ?obj)
    USING

    vCard FOR lthttp//www.w3.org/2001/vcar
    d-rdf/3.0gt rdf FOR
    lthttp//www.w3.org/1999/02/22-rdf-syntax-nsgt

49
rdftype
  • The keyword "a" can be used as a predicate in a
    triple pattern and is an alternative for the IRI 
    http//www.w3.org/1999/02/22-rdf-syntax-nstype.
  • This keyword is case-sensitive.
  • The triple pattern
  • ?x a Class1 .
  • a appClass p "v" .
  • is syntactic sugar for
  • ?x rdftype Class1 .
  • _b0 rdftype appClass .
  • _b0 p "v" .

50
Loading Data into MySQL using Jena
  • The following steps load data into MySQL with
    Jena
  • Create a connection to the database
  • IDBConnection conn new DBConnection (
    DB_URLDB_NAME, DB_USER, DB_PASSWD, DB )
  • Create the named model
  • ModelMaker maker ModelFactory.createModelRDBMake
    r(conn)
  • Model model maker.createModel("montery")

51
Loading Data into MySQL using Jena
  • The following steps load data into MySQL with
    Jena
  • Load the data
  • model.read(new FileInputStream("/home/vaibhav/Desk
    top/montery-test.rdf"), null)
  • Close the connection since the data is loaded
  • conn.close()

52
Querying Data from MySQL using Jena
  • The following steps query data in MySQL with Jena
  • Create a connection to the database
  • IDBConnection conn new DBConnection (
    DB_URLDB_NAME, DB_USER, DB_PASSWD, DB )
  • Open the named model
  • ModelMaker maker ModelFactory.createModelRDBMake
    r(conn)
  • Model model maker.openModel("montery")

53
Querying Data from MySQL using Jena
  • The following steps query data in MySQL with Jena
  • Create a query string
  • "PREFIX dc lt" DC.getURI() "gt"
    "SELECT ?incident "
    "WHERE ?incident
    dcidentifier \"1\""
  • Create a query statement and execution
  • Query query QueryFactory.create(queryString)
    QueryExecution qexec QueryExecutionFactory.creat
    e(query, model)

54
Querying Data from MySQL using Jena
  • The following steps query data in MySQL with Jena
  • Execute the query and get the results
  • ResultSet rs qexec.execSelect()
    System.out.println("incident
    number")
    for ( rs.hasNext() )



    QuerySolution rb rs.nextSolution()
    Resource
    x rb.getResource("incident")
    System.out.println("The uri
    for incident 1 is "x"\n")
About PowerShow.com