SPARQL

From BC$ MobileTV Wiki
Revision as of 19:08, 27 February 2020 by Bcmoney (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
SPARQL Semantic Pattern-based Aspect-oriented Relational Query Language

SPARQL (pronounced "sparkle" [1]) is an [RDF] query language; its name is a recursive acronym [2] that stands for SPARQL Protocol and RDF Query Language. It is standardized by the RDF Data Access Working Group (DAWG) of the W3C. Initially released as a Candidate Recommendation in April 2006, but returned to Working Draft status in October 2006, due to two open issues. [3] In June 2007, SPARQL advanced to Candidate Recommendation once again. [4] On 12th November 2007 the status of SPARQL changed into Proposed Recommendation. [5] On 15th January 2008, SPARQL became an official W3C Recommendation. [6]

The RDF Data Access Working Group has published three SPARQL recommendations (Query Language, Protocol, and Results Format) in January 2008. SPARQL has become very widely implemented and used since then (and, in fact, even before the specification achieved a W3C Recommendation status). Usage and implementation of SPARQL have revealed requirements for extensions to the query langauge that are needed by applications. Most of these were already known and recorded when developing the current Recommendation, but there was not enough implementation and and usage experience at the time for standardization. Current implementation experience and feedback from the user community makes it now feasible to handle some of those issues in a satisfactory manner. [7]


Specifications

Extensions


Language

Syntax

DESCRIBE

SELECT

DISTINCT

OPTIONAL

The OPTIONAL keyword provides support for querying relationships that may or may not occur in the data (a la SQL's LEFT JOINs; but rather than referring to relationships between specific tables that may or may not contain data that meet all the specific requirements, it refers to relationships to any data source whose data may meet a broad set of requirements).

ASK

GRAPH

The GRAPH keyword is a prime example, as it can be used to discover the URI of the graph that contains the data that matches a given query. [8]


Comparison to SQL

SPARQL is very similar to SQL in syntax and functionality, however, it adds a few new constructs and concepts. The first big difference to note is that SQL was designed to query a single native relational database (which may contain several separate databases but usually resides on a single domain, or is spread across server resources using techniques like replication and sharding), while SPARQL has been built to support queries in a networked, web environment across many disparate data sources (which are almost always hosted on multiple different domains, with a single query often requiring several GET or POST HTTP requests to resolve diverse entity relationships or make inferences about data locations).

Here is a side-by-side chart looking at the most common queries of each language (top entry in each row is SQL, bottom row is SPARQL verson): <style>ul.comparison{list-style-type:none;} ul.comparison li:first-child{background:Moccasin} li:last-child{background:AliceBlue}</style>

SPARQL .vs. SQL
OperationSPARQLSQL
Create
Read

SELECT title FROM book WHERE title LIKE "%title%"

PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT  ?title WHERE {

    <http://example.org/book/book1> dc:title ?title 

}

Update
Delete
Join
Left Join
Right Join
Inner Join
Describe

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX : <http://purl.uniprot.org/core/> DESCRIBE ?prot WHERE {

   ?prot rdf:type :Protein .
   ?prot :mnemonic "ARL6_HUMAN" . 

}

Matching

WHERE { ?o <orderDate> ?od .

       ?o <shipDate> "20060508"
       FILTER (?od < "20060501") 

}

WHERE o.orderDate < "20060501"

 AND o.shipDate = "20060508"
Unique

PREFIX table: <http://www.daml.org/2003/01/periodictable/PeriodicTable#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT DISTINCT ?element ?an ?clr ?state WHERE {

   ?element rdf:type table:Element .
   ?element table:atomicWeight ?aw .
   ?element table:atomicNumber ?an .
   ?element table:color ?clr .
   ?element table:standardState ?state .
   FILTER ( ?aw > 140.0 && ?aw < 160.0) }

}

Index
View
Trigger
Stored Procedure

[9] [10] [11] [12]



EXAMPLES

SPARQL Query in AllegroGraph

The following simple SPARQL query returns all country capitals in Africa:

PREFIX abc: <http://example.com/exampleOntology#> 
SELECT ?capital ?country
WHERE {
  ?x abc:cityname ?capital ;
     abc:isCapitalOf ?y .
  ?y abc:countryname ?country ;
     abc:isInContinent abc:Africa .
}

Variables are indicated by a "?" or "$" prefix. Bindings for ?capital and the ?country will be returned.

The SPARQL query processor will search for sets of triples that match these four triple patterns, binding the variables in the query to the corresponding parts of each triple. Important to note here is the "property orientation" (class matches can be conducted solely through class-attributes / properties) [13]

To make queries concise, SPARQL allows the definition of prefixes and base URIs in a fashion similar to Turtle. In this query, the prefix "abc" stands for “http://example.com/exampleOntology#”.


The following SPARQL query finds all US States:

 SELECT DISTINCT ?subject WHERE {
 ?subject owl:sameAs ?link.
 ?subject skos:subject <http://dbpedia.org/resource/Category:States_of_the_United_States>
 FILTER (?subject != "U.S._state")
 } ORDER BY ASC(?subject)

[14]



EndPoints




Tools

SPARQLbot

SPARQLscript

SPARQL Engine

SNORQL


Resources


Tutorial


External Links


References

  1. SPARQL Will Make the Web Shine
  2. wikipedia:recursive acronym
  3. SPARQL Query Language for RDF - W3C Working Draft: http://www.w3.org/TR/2006/WD-rdf-sparql-query-20061004/
  4. W3C Semantic Web Activity News - SPARQL is a Candidate Recommendation: http://www.w3.org/blog/SW/2007/06/15/sparql_is_a_candidate_recommendation
  5. Archive of W3C News in 2007: http://www.w3.org/News/2007#item247
  6. W3C Semantic Web Activity News - SPARQL is a Recommendation: http://www.w3.org/blog/SW/2008/01/15/sparql_is_a_recommendation
  7. SPARQL Charter: http://www.w3.org/2009/01/sparql-charter.html
  8. Adding SPARQL Support to MySQL: http://www.w3.org/2006/Talks/0518-SPASQL/
  9. Semantics preserving SPARQL-to-SQL translation:
  10. Semantics Preserving SPARQL-to-SQL Query Translation for Optional Graph Patterns: http://www.cs.panam.edu/~artem/main/research/TR-DB-052006-CLJF.pdf
  11. Berlin SPARQL Benchmark (BSBM0): http://www4.wiwiss.fu-berlin.de/bizer/BerlinSPARQLBenchmark/
  12. What are the benefits/drawbacks of SPARQL vis a vis SQL and XQuery?: http://www.thefigtrees.net/lee/sw/sparql-faq#benefits
  13. See: wikipedia:Duck typing
  14. Example queries against DBpedia database: http://wiki.dbpedia.org/Datasets#h18-12
  15. Is SNORQL still actively developed?: http://answers.semanticweb.com/questions/877/is-snorql-still-actively-developed

See Also

Semantic Web | Linked Data | SQL | NoSQL | OWL | RDF | N3 | XML | XSL | XSLT | JSON