Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore Module4- SPARQL-

Module4- SPARQL-

Published by hibi.nav, 2020-09-30 05:55:25

Description: Module4- SPARQL-

Search

Read the Text Version

SPARQL SPARQL Protocol And RDF Query Language Prof. Viswanathan V School of Computing Science and Engineering Vellore Institute of Technology Chennai

SPARQL - Introduction Querying with SPARQL RDF Triples stores SPARQL Result Set Query Viswanathan V, VIT Chennai 2

SPARQL – Introduction….. SPARQL is the standard language to query graph data represented as RDF triples. Types of SPARQL queries • SELECT - Return a table of all X, Y, etc. satisfying the following conditions ... • CONSTRUCT - Find all X, Y, etc. satisfying the following conditions ... and substitute them into the following template in order to generate (possibly new) RDF statements, creating a new graph. • DESCRIBE - Find all statements in the dataset that provide information about the following resource(s) ... (identified by name or description) • ASK - Are there any X, Y, etc. satisfying the following conditions ... Viswanathan V, VIT Chennai 3

Structure of a SPARQL Query What does a basic SPARQL query look like? SELECT clause - to identify the values to be returned FROM clause - to identify the data sources to query WHERE clause - the triple/graph pattern to be matched against the triples/graphs of RDF - a conjunction of triples: { ?x rdf:type ex:Person ?x ex:name ?name } PREFIX - to declare the schema used in the query Viswanathan V, VIT Chennai 4

Structure of a SPARQL Query ….. Definition of Prefix PREFIX ex: <http://www.vit.ac.in#> SELECT ?name Variables, ie..What to search for Type of WHERE query { ?x ex:legalname ?name} RDF triple Patterns, ie. The conditions that have to be met Viswanathan V, VIT Chennai 5

Sample dataset represented in Turtle @prefix ex: <http://www.vit.ac.in#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . ex:raj a ex:Person ; ex:dob \"1980-05-27\"^^xsd:date ; ex:knows \"Java\" ; ex:name \"Raj Kumar\" ; ex:school ex:scope . ex:ravi a ex:Person ; ex:dob \"1970-06-17\"^^xsd:date ; ex:knows \"Java\" ; ex:name \"Ravichandaran\" ; ex:school ex:smec . ex:mary a ex:Person ; ex:dob \"2005-08-17\"^^xsd:date ; ex:knows \"Java\" , \"Python\" ; ex:name \"Mary Joseph\" ; ex:school ex:scope . ex:jayaram a ex:Person ; ex:knows \"Java\" , \"Python\" ; ex:name \"Jayaram\" ; ex:school ex:smec . ex:radha a ex:Adult ; ex:knows \"Python\" ; ex:name \"Radha kumari\" ; ex:school ex:sense . Viswanathan V, VIT Chennai 6

SPARQL Query - Simple Example Example : persons and their names PREFIX ex: <http://www.vit.ac.in#> Output : SELECT ?person ?name WHERE { ?person rdf:type ex:Person . ?person ex:name ?name . } Viswanathan V, VIT Chennai 7

SPARQL Query -Filter FILTER - to add constraints to the graph pattern (e.g., numerical like x >17 ) Example : persons at least 18-year old PREFIX ex: <http://www.vit.ac.in#> The BIND( expression SELECT ?name ?age AS ?variable ) clause WHERE { can be used to assign the ?person rdf:type ex:Person . result of an expression to a ?person ex:name ?name . variable ?person ex:dob ?d . BIND(year(now()) - year(?d ) as ?age ) . FILTER( ?age > 17 ) } F I L T E R can use many operators, functions (e.g., regular 8 expressions) Viswanathan V, VIT Chennai

SPARQL Query –Filter …. Output : Viswanathan V, VIT Chennai 9

SPARQL Query -Optional How do we allow for missing or unknown information? OPTIONAL - to make the matching of a part of the pattern optional Example : retrieve the name of the person and age(if available) PREFIX ex: <http://www.vit.ac.in#> SELECT ?name ?age WHERE { ?person rdf:type ex:Person . ?person ex:name ?name . OPTIONAL{ ?person ex:dob ?d . BIND(year(now()) - year(?d ) as ?age ) }. } Viswanathan V, VIT Chennai 10

SPARQL Query –Optional …. Output : Viswanathan V, VIT Chennai 11

SPARQL Query - UNION How do we allow for alternatives or variations in the graph? UNION - to give alternative patterns in a query Example : List of all adult name - explicit or implicit adults PREFIX ex: <http://www.vit.ac.in#> Output : SELECT ?name WHERE { ?person ex:name ?name . { { ?person rdf:type ex:Adult } UNION { ?person ex:dob ?d BIND(year(now()) - year(?d ) as ?age ) FILTER (?age > 17) } } } Viswanathan V, VIT Chennai 12

SPARQL Query – Sorting and restrict the results How do we apply a sort order to the results? How can we restrict the number of results returned? ORDER BY to sort LIMIT result number OFFSET rank of first result Example: results 11 to 30 ordered by name PREFIX ex: <http://www.vit.ac.in#> SELECT ?name WHERE { ?person rdf:type ex:Person . ?person ex:name ?name . } ORDER BY DESC(?name) LIMIT 20 OFFSET 10 Viswanathan V, VIT Chennai 13

SPARQL Query - DISTINCT How do we remove duplicate results? PREFIX ex: <http://www.vit.ac.in#> SELECT distinct ?person ?name WHERE { ?person rdf:type ex:Person . ?person ex:name ?name . } Viswanathan V, VIT Chennai 14

SPARQL Query – group by Example : retrieve the number of persons in each school who knows “Java” SELECT ?sc (count(?sc) AS ?co) WHERE { ?person ex:knows \"Java\" . ?person ex:school ?sc . } GROUP BY ?sc Output : Viswanathan V, VIT Chennai 15

SPARQL – Bound() The function BOUND(variable) returns true if variable is bound to a value. It returns false otherwise. Consider the example: PREFIX ex: <http://www.vit.ac.in#> SELECT ?name WHERE { ?person ex:name ?name . ?person ex:knows ?x FILTER ( ?x != \"Java\" ) } Does this find persons who do not know \"java\" ? Answer: NO! - It will return persons who know something else ! Viswanathan V, VIT Chennai 16

SPARQL – Bound() …. 17 PREFIX ex: <http://www.vit.ac.in#> SELECT ?name WHERE { ?person ex:name ?name . ?person ex:knows ?x FILTER ( ?x != \"Java\" ) } ex:raj ex:knows \"Java\" ex:raj ex:knows \"C++“ raj is a answer... Viswanathan V, VIT Chennai

SPARQL – Bound() …. Persons who are not known to know \"java\" ... negation of an option PREFIX ex: <http://inria.fr/schema#> Output : SELECT ?name WHERE { ?person ex:name ?name . OPTIONAL { ?person ex:knows ?x FILTER ( ?x = \"Java\" ) } FILTER ( ! bound(?x) ) } More details about SPARQL/Expressions and Functions https://en.wikibooks.org/wiki/SPARQL/Expressions_and_Functions Viswanathan V, VIT Chennai 18

SPARQL - ASK Query Test whether the graph contains some data of interest ASK - to check just if there is at least one answer ; result is \"true\" or \"false“ Example: Is there a person born after the year 2000 ? PREFIX ex: <http://www.vit.ac.in#> Output : ASK False { ?person ex:dob ?d FILTER (year(?d) > 2010) } Viswanathan V, VIT Chennai 19

SPARQL – Construct Query Create a custom RDF graph based on query criteria can be used to transform RDF data CONSTRUCT - return a specific RDF graph for each result Example : return instances of adults for persons older than 17 PREFIX ex: <http://www.vit.ac.in#> CONSTRUCT Output : { @prefix ex: <http://www.vit.ac.in#> . ?person rdf:type ex:Adult @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. } @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . WHERE ex:ravi a ex:Adult . { ex:raj a ex:Adult . ?person ex:dob ?d BIND(year(now()) - year(?d ) as ?age ) FILTER (?age > 17) } Viswanathan V, VIT Chennai 20

SPARQL – DESCRIBE Generate an RDF description of a resource(s) Example : This query returns an RDF graph that describes all the persons who knows Python. OUTPUT : DESCRIBE ?x @prefix ex: <http://www.vit.ac.in#> . WHERE @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . { @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . ?x ex:knows \"Python\" } ex:radha a ex:Adult ; ex:knows \"Python\" ; ex:name \"Radha kumari\" ; ex:school ex:sense . ex:mary a ex:Person ; ex:dob \"2005-08-17\"^^xsd:date ; ex:knows \"Python\" , \"Java\" ; ex:name \"Mary Joseph\" ; ex:school ex:scope . ex:jayaram a ex:Person ; ex:knows \"Python\" , \"Java\" ; ex:name \"Jayaram\" ; ex:school ex:smec . Viswanathan V, VIT Chennai 21

Viswanathan V, VIT Chennai 22


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook