Presentation is loading. Please wait.

Presentation is loading. Please wait.

SPARQL All slides are adapted from the W3C Recommendation SPARQL Query Language for RDF Web link:

Similar presentations


Presentation on theme: "SPARQL All slides are adapted from the W3C Recommendation SPARQL Query Language for RDF Web link:"— Presentation transcript:

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/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#http://www.w3.org/1999/02/22-rdf-syntax-ns rdfs: http://www.w3.org/2000/01/rdf-schema#http://www.w3.org/2000/01/rdf-schema rdf: http://www.w3.org/2001/XMLSchema#http://www.w3.org/2001/XMLSchema fn: http://www.w3.org/2005/xpath-functions#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 @prefix dc:. @prefix :. :book1 dc:title "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 ’ ' and the variable ’z' is not bound to any RDF term “Alice” zyx

12 Conventions Terminology used –IRI’s –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 "SPARQL Tutorial". –Query SELECT ?title WHERE{ ?title.} –Result “SPARQL Tutorial” title

16 Lab A query to select a subject given its predicate and object – SELECT ?x WHERE { ?x "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 @prefix foaf:. _:a foaf:name "Johnny Lee Outlaw". _:a foaf:mbox. _:b foaf:name "Peter Goodguy". _:b foaf:mbox. _:c foaf:mbox. –Query PREFIX foaf: http://xmlns.com/foaf/0.1/ SELECT ?name ?mbox WHERE { ?x foaf:name ?name. ?x foaf:mbox ?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 foaf:name "Johnny Lee Outlaw". _:a foaf:box. _:b foaf:name "Peter Goodguy". _:b foaf:box. “Peter Goodguy” “Johnny Lee Outlaw” mboxname

20 Lab A query to select objects given any subject and a property based on some PREFIX – PREFIX info: SELECT ?z WHERE { ?element info:age ?z. }http://somewhere/peopleInfo

21 Lab A query to select objects based on a join condition – PREFIX vCard: SELECT ?x ?z WHERE { ?element vCard:Family ?x. ?element vCard:Given ?z. }http://www.w3.org/2001/vcard-rdf/3.0

22 Matching RDF Literals Consider the data below –@prefix dt: @prefix ns:. @prefix :. @prefix xsd:. :x ns:p "cat"@en. :y ns:p "42"^^xsd:integer. :z ns:p "abc"^^dt:specialDatatype. –Note that, in Turtle, "cat"@en is an RDF literal with a lexical form "cat" and a language en; "42"^^xsd:integer is a typed literal with the datatype http://www.w3.org/2001/XMLSchema#integer; and "abc"^^dt:specialDatatype is a typed literal with the datatype http://example.org/datatype#specialDatatype.

23 Matching RDF Literals Matching Literals with language tags –Language tags are expressed using @ and the language tag –The following query has no solution because “cat” is not the same as “cat”@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”@en } –Result v v

24 Matching RDF Literals Matching Literals with Numeric Types –Integers in a SPARQL query indicate an RDF typed literal with the datatype xsd:integer –Example: 42 is a shortened form of "42"^^ –The following query has a solution with the variable v bound to :y –Query SELECT ?v WHERE { ?v ?p 42 } –Result v

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"^^ } –Result v

26 Lab Select objects based on a stronger join condition with a matching literal value – PREFIX info: PREFIX vCard: SELECT ?x WHERE { ?element vCard:FN ?x. ?element info:age "43". }http://somewhere/peopleInfohttp://www.w3.org/2001/vcard-rdf/3.0

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 @prefix foaf:. _:a foaf:name "Alice". _:b foaf:name "Bob". Query PREFIX foaf: SELECT ?x ?name WHERE { ?x foaf:name ?name } Result “Bob”_:d “Alice”_:c namex

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 “Bob”_:s “Alice”_:r namex

29 Lab Finding blank nodes – PREFIX vCard: SELECT ?y WHERE { ?element vCard:N ?y. }http://www.w3.org/2001/vcard-rdf/3.0

30 Lab Finding blank nodes with a specific condition – PREFIX info: PREFIX vCard: SELECT ?x ?y WHERE { ?element info:age ?x ?element vCard:N ?y. }http://somewhere/peopleInfohttp://www.w3.org/2001/vcard-rdf/3.0

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 @prefix org:. _:a org:employeeName "Alice". _:a org:employeeId 12345. _:b org:employeeName "Bob". _:b org:employeeId 67890. Query PREFIX foaf: PREFIX org: CONSTRUCT { ?x foaf:name ?name } WHERE { ?x org:employeeName ?name } Result @prefix org:. _:x foaf:name "Alice". _:y foaf:name "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 @prefix dc:. @prefix :. @prefix ns:. :book1 dc:title "SPARQL Tutorial". :book1 ns:price 42. :book2 dc:title "The Semantic Web". :book2 ns:price 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: SELECT ?title WHERE { ?x dc:title ?title FILTER regex(?title, "^SPARQL") } Result Regular expressions can be made case-insensitive with the “i” flag Query PREFIX dc: SELECT ?title WHERE { ?x dc:title ?title FILTER regex(?title, ”web”, “i”) } Result “SPARQL Tutorial” title “The Semantic Web” title

35 Restricting Numeric Values SPARQL FILTERs can restrict on arithmetic expressions Query PREFIX dc: PREFIX ns: SELECT ?title ?price WHERE { ?x ns:price ?price. FILTER (?price < 30.5) ?x dc:title ?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 23“The Semantic Web” pricetitle

36 Other Term Constraints In addition to numeric types, SPARQL supports type xsd:string, xsd:boolean, and, xsd:dateTime

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 " ", '"' (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 BASE PREFIX book: book:book1

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 @ ) 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 xsd:integer, decimal numbers for which there is ‘.’ in the number but no exponent are interpreted as xsd:decimal; and numbers with exponents are interpreted as xsd:double. Values of xsd:boolean 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’@en Integer, Decimal, Double, and, Boolean are equivalent to a typed literal with the lexical value of the token and the corresponding datatype ( xsd:integer, xsd:decimal, xsd:double, and, xsd:boolean )‏

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 –[ foaf:name ?name ; foaf:mbox ] is equivalent to _:b18 foaf:name ?name. _:b18 foaf:mbox.

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: SELECT ?title WHERE { dc:title ?title } –PREFIX dc: PREFIX : SELECT $title WHERE { :book1 dc:title $title } –BASE PREFIX dc: SELECT $title WHERE { dc:title ?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 foaf:name ?name ; foaf:mbox ?mbox. This is the same as writing the triple patterns: ?x foaf:name ?name. ?x foaf:mbox ?mbox.

44 Object Lists If triple patterns share both subject and predicate, the objects may be separated by “,” The triple pattern ?x foaf:nick "Alice", "Alice_". This is the same as writing the triple patterns: ?x foaf:nick "Alice". ?x foaf:nick "Alice_". Object lists can be combined with predicate-object lists: ?x foaf:name ?name ; foaf:nick "Alice", "Alice_". This is equivalent to: ?x foaf:name ?name. ?x foaf:nick "Alice". ?x foaf:nick "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-ns#nil. http://www.w3.org/1999/02/22-rdf-syntax-ns#nil 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 rdf:first 1 ; rdf:rest _:b1. _:b1 rdf:first ?x ;rdf:rest _:b2. _:b2 rdf:first 3 ; rdf:rest _:b3. _:b3 rdf:first 4 ; rdf:rest rdf:nil. _: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 rdf:first 1 ; rdf:rest _:b1. _:b1 rdf:first _:b2. _:b2 :p :q. _:b1 rdf:rest _:b3. _:b3 rdf:first _:b4. _:b4 rdf:first 2 ; rdf:rest rdf:nil. _:b3 rdf:rest rdf:nil.

47 Lab Query to find the given name when the family name is given using a collection – SELECT ?givenName WHERE (?y,, "Smith"), (?y,, ?givenName)‏

48 Lab Query to find the top level properties for “John Smith” – SELECT ?property WHERE (?person,, "John Smith"), (?person, ?property, ?obj) USING vCard FOR rdf FOR

49 rdf:type 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-ns#type.http://www.w3.org/1999/02/22-rdf- syntax-ns#type This keyword is case-sensitive. The triple pattern ?x a :Class1. [ a :appClass ] :p "v". is syntactic sugar for: ?x rdf:type :Class1. _:b0 rdf:type :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_URL+DB_NAME, DB_USER, DB_PASSWD, DB ); – Create the named model ModelMaker maker = ModelFactory.createModelRDBMaker(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/Desktop/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_URL+DB_NAME, DB_USER, DB_PASSWD, DB ); – Open the named model ModelMaker maker = ModelFactory.createModelRDBMaker(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: " + "SELECT ?incident " + "WHERE {?incident dc:identifier \"1\"}" ; – Create a query statement and execution Query query = QueryFactory.create(queryString) ; QueryExecution qexec = QueryExecutionFactory.create(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") ; }


Download ppt "SPARQL All slides are adapted from the W3C Recommendation SPARQL Query Language for RDF Web link:"

Similar presentations


Ads by Google