- Source: SPARQL
SPARQL (pronounced "sparkle", a recursive acronym for SPARQL Protocol and RDF Query Language) is an RDF query language—that is, a semantic query language for databases—able to retrieve and manipulate data stored in Resource Description Framework (RDF) format. It was made a standard by the RDF Data Access Working Group (DAWG) of the World Wide Web Consortium, and is recognized as one of the key technologies of the semantic web. On 15 January 2008, SPARQL 1.0 was acknowledged by W3C as an official recommendation, and SPARQL 1.1 in March, 2013.
SPARQL allows for a query to consist of triple patterns, conjunctions, disjunctions, and optional patterns.
Implementations for multiple programming languages exist. There exist tools that allow one to connect and semi-automatically construct a SPARQL query for a SPARQL endpoint, for example ViziQuer.
In addition, tools exist to translate SPARQL queries to other query languages, for example to SQL and to XQuery.
Advantages
SPARQL allows users to write queries that follow the RDF specification of the W3C. Thus, the entire dataset is "subject-predicate-object" triples. Subjects and predicates are always URI identifiers, but objects can be URIs or literal values. This single physical schema of 3 "columns" is hyperdenormalized in that what would be 1 relational record with 4 fields is now 4 triples with the subject being repeated over and over, the predicate essentially being the column name, and the object being the field value. Although this seems unwieldy,
the SPARQL syntax offers these features:
1. Subjects and Objects can be used to find the other including recursively.
Below is a set of triples. It should be clear that
ex:sw001 and ex:sw002 link to ex:sw003, which itself has links:
In SPARQL, the first time a variable is encountered in the expression pipeline, it is populated with result. The second and subsequent times it is seen, it is used as an input. If we assign ("bind") the URI ex:sw003 to the ?targets variable, then it drives a
result into ?src; this tells us all the things that link to ex:sw003 (upstream dependency):
But with a simple switch of the binding variable, the behavior is reversed. This will produce all the things upon which ex:sw003 depends (downstream dependency):
Even more attractive is that we can easily instruct SPARQL to recursively follow the path:
Bound variables can therefore also be lists and will be operated upon without complicated syntax. The effect of this is similar to the following:
2. SPARQL expressions are a pipeline
Unlike SQL which has subqueries and CTEs, SPARQL is much more like MongoDB or SPARK. Expressions are evaluated exactly in the order they are declared including filtering and joining of data. The programming model becomes what a SQL statement would be like with multiple WHERE clauses. The combination of list-aware subjects and objects plus a pipeline approach can yield extremely expressive queries spanning many different domains of data.
Unlike relational databases, the object column is heterogeneous: the object data type, if not an URI, is usually implied (or specified in the ontology) by the predicate value. Literal nodes carry type information consistent with the underlying XSD namespace including signed and unsigned short and long integers, single and double precision floats, datetime, penny-precise decimal, Boolean, and string. Triple store implementations on traditional relational databases will typically store the value as a string and a fourth column will identify the real type. Polymorphic databases such as MongoDB and SQLite can store the native value directly into the object field.
Thus, SPARQL provides a full set of analytic query operations such as JOIN, SORT, AGGREGATE for data whose schema is intrinsically part of the data rather than requiring a separate schema definition. However, schema information (the ontology) is often provided externally, to allow joining of different datasets unambiguously. In addition, SPARQL provides specific graph traversal syntax for data that can be thought of as a graph.
The example below demonstrates a simple query that leverages the ontology definition foaf ("friend of a friend").
Specifically, the following query returns names and emails of every person in the dataset:
This query joins all of the triples with a matching subject, where the type predicate, "a", is a person (foaf:Person), and the person has one or more names (foaf:name) and mailboxes (foaf:mbox).
For the sake of readability, the author of this query chose to reference the subject using the variable name "?person". Since the first element of the triple is always the subject, the author could have just as easily used any variable name, such as "?subj" or "?x". Whatever name is chosen, it must be the same on each line of the query to signify that the query engine is to join triples with the same subject.
The result of the join is a set of rows – ?person, ?name, ?email. This query returns the ?name and ?email because ?person is often a complex URI rather than a human-friendly string. Note that any ?person may have multiple mailboxes, so in the returned set, a ?name row may appear multiple times, once for each mailbox.
This query can be distributed to multiple SPARQL endpoints (services that accept SPARQL queries and return results), computed, and results gathered, a procedure known as federated query.
Whether in a federated manner or locally, additional triple definitions in the query could allow joins to different subject types, such as automobiles, to allow simple queries, for example, to return a list of names and emails for people who drive automobiles with a high fuel efficiency.
Query forms
In the case of queries that read data from the database, the SPARQL language specifies four different query variations for different purposes.
SELECT query
Used to extract raw values from a SPARQL endpoint, the results are returned in a table format.
CONSTRUCT query
Used to extract information from the SPARQL endpoint and transform the results into valid RDF.
ASK query
Used to provide a simple True/False result for a query on a SPARQL endpoint.
DESCRIBE query
Used to extract an RDF graph from the SPARQL endpoint, the content of which is left to the endpoint to decide, based on what the maintainer deems as useful information.
Each of these query forms takes a WHERE block to restrict the query, although, in the case of the DESCRIBE query, the WHERE is optional.
SPARQL 1.1 specifies a language for updating the database with several new query forms.
Example
Another SPARQL query example that models the question "What are all the country capitals in Africa?":
Variables are indicated by a ? or $ prefix. Bindings for ?capital and the ?country will be returned. When a triple ends with a semicolon, the subject from this triple will implicitly complete the following pair to an entire triple. So for example ex:isCapitalOf ?y is short for ?x ex:isCapitalOf ?y.
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 or properties – see Duck typing).
To make queries concise, SPARQL allows the definition of prefixes and base URIs in a fashion similar to Turtle. In this query, the prefix "ex" stands for “http://example.com/exampleOntology#”.
Extensions
GeoSPARQL defines filter functions for geographic information system (GIS) queries using well-understood OGC standards (GML, WKT, etc.).
SPARUL is another extension to SPARQL. It enables the RDF store to be updated with this declarative query language, by adding INSERT and DELETE methods.
XSPARQL is an integrated query language combining XQuery with SPARQL to query both XML and RDF data sources at once.
Implementations
Open source, reference SPARQL implementations
Eclipse RDF4J, formerly OpenRDF Sesame
Apache Jena
OpenLink Virtuoso
See List of SPARQL implementations for more comprehensive coverage, including triplestore, APIs, and other storages that have implemented the SPARQL standard.
See also
Semantic Integration
SPARQL Query Results XML Format
SPARQL Syntax Expressions
Wikidata
References
External links
Wikidata Query Service; example SPARQL queries are here
Wikidata Query Service Tutorial
DBpedia
W3C Data Activity Blog
W3C SPARQL 1.1 Working Group - closed - mailing lists and archives, was RDF Data Access Working Group
SPARQL 1.1 Recommendation
SPARQL 1.0 Query language (legacy)
SPARQL 1.0 Protocol (legacy)
SPARQL 1.0 Query XML Results Format (legacy)
SPARQL2XQuery Mappings between OWL-RDF/S & XML Schemas, and XML Schema to OWL Transformation.
SPARQL Syntax Expressions in the ARQ query engine
James (8 September 2011). "DAWG Test Suite for SPOCQ". Dydra. Archived from the original on 7 June 2015. Retrieved 2 December 2014.
James (8 September 2011). "RSpec Code Examples / Results: 425 examples, 1 failure / Finished in 287.385157145 seconds". Dydra. Archived from the original on 11 December 2011. Retrieved 2 December 2014.
Kata Kunci Pencarian:
- Web semantik
- Rangkong badak
- Wikidata
- Rangkong gading
- Rangkong
- Julang sulawesi
- Rangkong papan
- Kangkareng perut-putih
- Julang emas
- Cekakak sungai
- SPARQL
- RDF Schema
- TinySPARQL
- GeoSPARQL
- Triplestore
- QLever
- ChEBI
- SPARQL Syntax Expressions
- Resource Description Framework
- RDF query language