Chapter 3 Why an RDF Query Language? l Why not use an XML query language? Querying RDF l XML at a lower level of abstraction than stores with RDF l There are various ways of syntactically SPARQL representing an RDF statement in XML l 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!
Enter SPARQL SPARQL Example l SPARQL Protocol and RDF Query Language PREFIX foaf:
1 SPARQL Protocol, Endpoints, APIs SPARQL Basic Queries l SPARQL query language l SPARQL is based on matching graph patterns l SPROT = SPARQL Protocol for RDF l The simplest graph pattern is the triple pattern - – Among other things specifies how results can be ?person foaf:name ?name encoded as RDF, XML or JSON - Like an RDF triple, but variables can be in any position l SPARQL endpoint - Variables begin with a question mark – A service that accepts queries and returns results via HTTP l Combining triple patterns gives a graph pattern; an exact match to a graph is needed – Either generic (fetching data as needed) or specific (querying an associated triple store) l Like SQL, a set of results is returned, not just one – May be a service for federated queries
Turtle Like Syntax Optional Data
As in N# and Turtle, we can omit a common subject l The query fails unless the entire patter matches in a graph pattern. l We often want to collect some information that might not always be available PREFIX foaf:
2 Example of a Generic Endpoint Example l Use the sparql endpoint at – http://demo.openlinksw.com/sparql l To query graph at – http://ebiq.org/person/foaf/Tim/Finin/foaf.rdf l For foaf knows relations SELECT ?name ?p2 WHERE { ?person a foaf:Person; foaf:name ?name; foaf:knows ?p2. }
Query results as HTML Other result format options
3 Example of a dedicated Endpoint PREFIX dbp:
SELECT FROM FILTER l The FROM clause lets us specify the target graph Find landlocked countries with a population >15 million in the query PREFIX rdfs:
PREFIX prop:
}
4 FILTER Functions Union l Logical: !, &&, || l Math: +, -, *, / l The UNION keyword forms a disjunction of two l Comparison: =, !=, >, <, ... graph patterns l SPARQL tests: isURI, isBlank, isLiteral, bound l Both subquery results are included l SPARQL accessors: str, lang, datatype l Other: sameTerm, langMatches, regex PREFIX foaf:
Query forms SPARQL 1.1
Each form takes a WHERE block to restrict the query SPARQL 1.1 is in last draft status & includes l SELECT: Extract raw values from a SPARQL endpoint, l Updated 1.1 versions of SPARQL Query the results are returned in a table format and SPARQL Protocol l CONSTRUCT: Extract information from the SPARQL endpoint and transform the results into valid RDF l SPARQL 1.1 Update l ASK: Returns a simple True/False result for a query on a l SPARQL 1.1 Graph Store HTTP Protocol SPARQL endpoint l l DESCRIBE Extract an RDF graph from the SPARQL SPARQL 1.1 Service Descriptions endpoint, the contents of which is left to the endpoint to l SPARQL 1.1 Entailments decide based on what the maintainer deems as useful information l SPARQL 1.1 Basic Federated Query
5