文章目录
- 1. 简介
- 2. 语法
1. 简介
SPARQL(SPARQL Protocol and RDF Query Language),是为RDF开发的一种查询语言和数据获取协议。
对于从数据库读取数据的查询,SPARQL语言为不同目的指定了四种不同的查询变体。
-
SELECT查询:用于从SPARQL端点提取原始值,结果以表格格式返回。
-
CONSTRUCT查询:用于从SPARQL端点提取信息并将结果转换为有效的RDF。
-
ASK查询:用于为SPARQL端点上的查询提供简单的True / False结果。
-
DESCRIBE查询:用于从SPARQL端点提取RDF图,其内容留给端点根据维护者认为有用的信息来决定。
这些查询表单中的每一个都使用WHERE块来限制查询,但是,在DESCRIBE查询的情况下,WHERE是可选的。
2. 语法
2.1 Simple example
SPARQL中:
-
变量的前缀是
?
-
用类似N3的语法表达三元组
-
查询返回一个变量的绑定结果集
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE {
?x foaf:name ?name .
?x foaf:mbox ?mbox .
}
?name | ?mbox |
---|---|
“Johnny Lee Outlaw” | mailto:jlow@example.com |
“Peter Goodguy” | mailto:peter@example.org |
2.2 约束条件
SPARQL中,限制条件可以应用于变量。
SELECT ?title
WHERE {
?x dc:title ?title .
FILTER regex(?title, "^SPARQL")
}
SELECT ?title ?price
WHERE {
?x ns:price ?price .
FILTER (?price < 30.5)
?x dc:title ?title .
}
2.3 Group Graph Patterns
Group Graph Patterns是用{ }括起来的一组模式,决定了FILTER操作符(和其他操作符)的作用范围。
2.3.1 Optional Graph Patterns
OPTIONAL允许我们说明,为了产生绑定,不需要匹配Group Graph Patterns。
SELECT ?name ?mbox
WHERE {
?x foaf:name ?name .
OPTIONAL {
?x foaf:mbox ?mbox .
}
}
?name | ?mbox |
---|---|
“Alice” | mailto:alice@example.com |
“Alice” | mailto:alice@work.example.com |
“Bob” |
2.3.2 Union Graph Patterns
UNION有效地给我们提供了一个二择一的查询:要么匹配这个,要么匹配那个。
PREFIX dc10: <http://purl.org/dc/elements/1.0/>
PREFIX dc11: <http://purl.org/dc/elements/1.1/>
SELECT ?title
WHERE {
{ ?book dc10:title ?title . }
UNION
{ ?book dc11:title ?title . }
}
2.4 Default Graph & Named Graphs
SPARQL 查询针对表示图形集合的RDF 数据集执行。一个 RDF 数据集包括一个图,即没有名称的默认图,以及0或多个命名图,其中每个命名图由 IRI 标识。
2.4.1 Default Graph
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
FROM <http://example.org/foaf/aliceFoaf>
WHERE {
?x foaf:name ?name .
}
其中,http://example.org/foaf/aliceFoaf
是默认图,除非另有说明,所有的triples都会来自该图。
2.4.2 Named Graphs
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?who ?g ?mbox
FROM <http://example.org/dft.ttl>
FROM NAMED <http://example.org/alice>
FROM NAMED <http://example.org/bob>
WHERE {
?g dc:publisher ?who .
GRAPH ?g { ?who foaf:mbox ?mbox . }
}
2.5 Ordering & LIMIT & OFFSET & DISTINCT
2.5.1 Ordering
按?name
升序排列结果,然后按?emp
降序排列。ASC():升序排序;DESC()降序排序。默认为升序。
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX ont: <http://example.org/ontology/>
SELECT ?name
WHERE {
?x foaf:name ?name ;
ont:empId ?emp .
}
ORDER BY ?name DESC(?emp)
2.5.2 LIMIT and OFFSET
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE {
?x foaf:name ?name .
}
ORDER BY ?name
LIMIT 5
OFFSET 10
2.5.3 DISTINCT
DISTINCT:删除重复的结果。
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?name
WHERE {
?x foaf:name ?name .
}
2.6 Other SPARQL Verbs
除了SELECT之外,还有CONSTRUCT、ASK和DESCRIBE。
2.6.1 CONSTRUCT
2.6.1.1 基本用法
CONSTRUCT查询返回一个RDF图,由一个图模板指定。对于结果集中的每一行,用该行的值替换模板中的变量,将得到的图合并为一个图(set union of triples)。
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
CONSTRUCT {
?x vcard:FN ?name .
?x vcard:EMAIL ?mail .
}
WHERE {
?x foaf:name ?name .
?x foaf:mbox ?mail .
}
2.6.1.2 CONSTRUCT and bNodes
模板可以创建包含空白节点的 RDF 图。空白节点标签的范围仅限于每个解决方案的模板。如果同一个标签在模板中出现两次,那么每个查询解决方案都会创建一个空白节点,但不同查询解决方案生成的三元组会有不同的空白节点。
CONSTRUCT {
?x vcard:N _:v .
_:v vcard:givenName ?gname .
_:v vcard:familyName ?fname .
}
WHERE {
{ ?x foaf:firstname ?gname } UNION { ?x foaf:givenname ?gname } .
{ ?x foaf:surname ?fname } UNION { ?x foaf:family_name ?fname } .
}
2.6.1.3 访问 RDF 数据集中的图
使用CONSTRUCT
,可以从目标 RDF 数据集中提取部分或全部图形。
CONSTRUCT {
?s ?p ?o .
}
WHERE {
GRAPH <http://example.org/aGraph> { ?s ?p ?o } .
}
2.6.2 ASK
ASK查询是用来检查一个查询模式是否有解决方案。不返回关于查询解决方案的信息(没有变量绑定),只有一个布尔值。
_:a foaf:name "Alice" .
_:a foaf:homepage <http://work.example.org/alice/> .
_:b foaf:name "Bob" ._:b foaf:mbox <mailto:bob@work.example> .
ASK { ?x foaf:name "Alice" }
(returns true)
2.6.3 DESCRIBE
DESCRIBE
返回包含一个或多个资源信息的单一graph。返回数据的结构由SPARQL查询处理器决定。
DESCRIBE <http://example.org/>
DESCRIBE ?x
WHERE {
?x foaf:name "Alice" .
}
DESCRIBE ?x ?y <http://example.org/>
WHERE {
?x foaf:knows ?y .
}
3. SPARQL 1.1
SPARQL 1.0 在2008年被 W3C 认可为官方推荐,SPARQL 1.1在其基础上增加了一些新功能,2013年SPARQL 1.1被W3C认可。
SPARQL只提供了一种查询数据库的方法,但许多应用程序需要的其他功能。SPARQL 1.1的更新源于早期的SPARUL提案。
3.1 Update
3.1.1 INSERT DATA
PREFIX dc: <http://purl.org/dc/elements/1.1/>
INSERT DATA
{
<http://example/book1> dc:title "A new book" ;
dc:creator "A.N.Other" .
}
3.1.2 DELETE DATA
PREFIX dc: <http://purl.org/dc/elements/1.1/>
DELETE DATA
{
<http://example/book2> dc:title "David Copperfield" ;
dc:creator "Edmund Wells" .
}
3.1.3 DELETE/INSERT
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
WITH <http://example/addresses>
DELETE
{
?person foaf:givenName 'Bill'
}
INSERT
{
?person foaf:givenName 'William'
}
WHERE
{
?person a foaf:Person .
?person foaf:givenName 'Bill'
}
3.1.4 Graph Operations
-
LOAD: load triples from URI into graph
-
CLEAR: clear all triples from graph
-
CREATE: create new graph in store
-
DROP: remove graph from store
-
COPY: copy all triples from one graph to another
-
MOVE: move all triples from one graph to another (remove from source)
-
ADD: add all triples in one graph to another
3.2 Aggregates
PREFIX : <http://books.example/>
SELECT (SUM(?lprice) AS ?totalPrice)
WHERE {
?org :affiliates ?auth .
?auth :writesBook ?book .
?book :price ?lprice .
}
GROUP BY ?org
HAVING (SUM(?lprice) > 10)
GROUP BY - 计算出的组的合计值(single implicit group if no GROUP BY statement)。
HAVING - 对组的过滤。
AS - 聚合投影限制。
其他聚合函数: COUNT, MIN, MAX, GROUP_CONCAT, SAMPLE
3.3 Subqueries
PREFIX : <http://people.example/>
PREFIX : <http://people.example/>
SELECT ?y ?minName
WHERE {
:alice :knows ?y .
{
SELECT ?y (MIN(?name) AS ?minName)
WHERE {
?y :name ?name .
}
GROUP BY ?y
}
}
3.4 SELECT Expressions
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX ns: <http://example.org/ns#>
SELECT ?title (?p*(1-?discount) AS ?price)
WHERE {
?x ns:price ?p .
?x dc:title ?title .
?x ns:discount ?discount
}
3.5 Property Paths
3.5.1 Alternatives
“|” 是 UNION 的语法糖。
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?displayString
WHERE {
:book1 dc:title|rdfs:label ?displayString
}
3.5.2 Sequences
?x foaf:knows [ foaf:name ?name]
的语法糖。
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE {
?x foaf:mbox <mailto:alice@example> .
?x foaf:knows/foaf:name ?name .
}
3.5.3 Arbitrary Sequences
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE {
?x foaf:mbox <mailto:alice@example> .
?x foaf:knows+/foaf:name ?name .
}
Repeat operators:
-
* (zero or more occurrences)
-
+ (one or more occurrences)
-
{n} (exactly n occurrences)
-
{n,m} (between n and m occurrences)
3.5.4 Inverses
反转属性方向,交换主体和对象的角色。以下例子表示查找所有认识?x
认识的人(Find all the people who know someone ?x knows)。
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?y
WHERE {
<mailto:alice@example> ^foaf:mbox ?x .
?x foaf:knows/^foaf:knows ?y .
FILTER(?x != ?y)
}
3.6 Entailment Regimes
到目前为止,我们只看了没有引用的图的查询。引用改变了问题:我们的模式可以与推理出的三元组相匹配,哪些三元组被推断出来取决于Entailment Regimes
。
多种Entailment Regimes
,分别对应于:
-
使用的本体语言:RDF、RDFS、OWL
-
数据类型的使用
-
规则语言的使用
SELECT ?pub WHERE { ?pub rdf:type ex:Publication }
Without entailment: ex:book1
With entailment: ex:book1, ex:book2, ex:book3