Chapter 3 Why an RDF ? 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: l W3C began developing a spec for a query SELECT ?name ?age language in 2004 WHERE { l There were/are other RDF query languages, and ?person a foaf:Person. extensions, e.g., RQL, Jena’s ARQ, ?person foaf:name ?name. l SPARQL a W3C recommendation in 2008 ?person foaf:age ?age – Query language + protocol + result format } l SPARQL 1.1 currently a last-call working draft ORDER BY ?age DESC – Includes updates, aggregation functions, federation, … LIMIT 10 l Most triple stores support SPARQL

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 , 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: l Note difference with relational model SELECT ?name ?age PREFIX foaf: WHERE { SELECT ?name ?age ?person a foaf:Person; WHERE { foaf:name ?name; ?person a foaf:Person; foaf:age ?age foaf:name ?name. OPTIONAL {?person foaf:age ?age} } }

2 Example of a Generic Endpoint Example l Use the 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: PREFIX dbpo: SELECT distinct ?Property ?Place l Use the sparql endpoint at WHERE {dbp:Barack_Obama ?Property ?Place . – http://dbpedia.org/sparql ?Place rdf:type dbpo:Place .} l To query DBpedia l To discover places associated with President Obama PREFIX dbp: PREFIX dbpo: SELECT distinct ?Property ?Place WHERE {dbp:Barack_Obama ?Property ?Place . ?Place rdf:type dbpo:Place .} http://dbpedia.org/sparql/

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: l SELECT * returns all PREFIX type:

PREFIX prop: PREFIX foaf: SELECT ?country_name ?population SELECT * WHERE { FROM ?country a type:LandlockedCountries ; WHERE { rdfs:label ?country_name ; ?P1 foaf:knows ?p2 prop:populationEstimate ?population . } FILTER (?population > 15000000) .

}

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: l Conditionals (SPARQL 1.1): IF, COALESCE PREFIX vCard: l Constructors (SPARQL 1.1): URI, BNODE, STRDT, STRLANG SELECT ?name l Strings (SPARQL 1.1): STRLEN, SUBSTR, UCASE, … l More math (SPARQL 1.1): abs, round, ceil, floor, RAND WHERE l Date/time (SPARQL 1.1): now, year, month, day, hours, … { l Hashing (SPARQL 1.1): MD5, SHA1, SHA224, SHA256, … { [ ] foaf:name ?name } UNION { [ ] vCard:FN ?name } }

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