Presentation is loading. Please wait.

Presentation is loading. Please wait.

SPARQL RDF Query.

Similar presentations


Presentation on theme: "SPARQL RDF Query."— Presentation transcript:

1 SPARQL RDF Query

2 Query RDF: SPARQL The Simple Protocol and RDF Query Language (SPARQL) is a SQL-like language for querying RDF data. For expressing RDF graphs in the matching part of the query, TURTLE syntax is used.

3 A Query Template I want these pieces of information from the subset of the data that meets these conditions. You describe the conditions with triple patterns, which are similar to RDF triples but may include variables to add flexibility in how they match against the data.

4 Craig has two email addresses:

5

6

7 The query to find Craig’s email addresses would then look like this:

8 A SELECT query PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox WHERE { ?x foaf:name ?name . ?x foaf:mbox ?mbox . } first line defines namespace prefix the last two lines use the prefix to express a RDF graph to be matched Identifiers beginning with question mark ? identify variables In this query, we are looking for resource ?x participating in triples with predicates foaf:name and foaf:mbox and want the objects of these triples

9 PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?url FROM <bloggers.rdf> WHERE { ?contributor foaf:name "Jon Foobar" . ?contributor foaf:weblog ?url . }

10 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 . } Result “SPARQL Tutorial"

11 The result of a query is a solution sequence, corresponding to the ways in which the query's graph pattern matches the data. There may be zero, one or multiple solutions to a query.

12 Data: @prefix foaf: <http://xmlns.com/foaf/0.1/> . _: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 } Query Result: name mbox "Johnny Lee Outlaw" "Peter Goodguy"

13 SELECT ?x WHERE { ?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> "John Smith" } SELECT ?x ?fname WHERE {?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> ?fname} SELECT ?givenName WHERE { ?y <http://www.w3.org/2001/vcard-rdf/3.0#Family> "Smith" . ?y <http://www.w3.org/2001/vcard-rdf/3.0#Given> ?givenName . } PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?givenName WHERE { ?y vcard:Family "Smith" . ?y vcard:Given ?givenName . }

14 SPARQL Tutorial - Filters
SPARQL provides an operation to test strings, based on regular expressions.  Syntax is FILTER regex(?x, "pattern" [, "flags"]) PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?g WHERE { ?y vcard:Given ?g . FILTER regex(?g, "r", "i") } Result: "Rebecca" "Sarah" The flag "i" means a case-insensitive pattern match is done

15 PREFIX info: <http://somewhere/peopleInfo#>
SELECT ?resource WHERE { ?resource info:age ?age . FILTER (?age >= 24) } PREFIX info: <http://somewhere/peopleInfo#> PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?name ?age WHERE { ?person vcard:FN ?name . OPTIONAL { ?person info:age ?age } } gets the name of a person and also their age if that piece of information is available.

16 PREFIX info: <http://somewhere/peopleInfo#>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?name ?age WHERE { ?person vcard:FN ?name . OPTIONAL { ?person info:age ?age . FILTER ( ?age > 24 ) } }

17 A query to find people described in two named FOAF graphs
PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT ?name FROM NAMED <jon-foaf.rdf> FROM NAMED <liz-foaf.rdf> WHERE { GRAPH <jon-foaf.rdf> { ?x rdf:type foaf:Person . ?x foaf:name ?name . } . GRAPH <liz-foaf.rdf> { ?y rdf:type foaf:Person . ?y foaf:name ?name . }


Download ppt "SPARQL RDF Query."

Similar presentations


Ads by Google