Knowledge Engineering Ⅷ | KG Querying
RDF term syntax
Syntax for IRI:
PREFIX book: <http://example.org/book/book1
The general syntax for literals: language tag
@
or datatype IRI^^
, 语言标识必须加,类型会自动生成Syntax for query variables: 变量之前加
?
or$
Syntax for blank nodes: 一般可以用
?variable
代替1
2
3
4
5
6SELECT ?a ?b
WHERE
{
?a :predicate ?variable .
?variable :otherPredicate ?b .
}
Query Pattern
Triple Pattern
Triple patterns are similar to RDF triples, but any component can be a query variable?x foaf:mane ?name .
Basic Graph pattern
a set of triple patterns written as a sequence of triple patterns.
回答问题就是找 RDF terms 和 variables 匹配的项
1 | PREFIX foaf:<http://xmlns.com/foaf/0.1/> |
1 | PREFIX foaf:<http://xmlns.com/foaf/0.1/> |
Other graph pattern
Group Graph Pattern FILTER + 需要的内容
1
2
3{
?x foaf:name ?name . FILTER regex(?name, "Smith")
}Optional Graph Pattern OPTIONAL 中的内容可以没有
1
2
3
4
5
6PREFIX foaf:<http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE{
{?x foaf:name ?name .}
OPTIONAL{?x foaf:mbox ?mbox .}
}| name | mbox |
| :——-: | :————————————: |
| “Alice” | alice@example.com |
| “Bob” | |☆ 两个OPTIONAL 第一个要加句号
1
2
3
4
5
6
7PREFIX foaf:<http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE{
{?x foaf:name ?name .}
OPTIONAL{?x foaf:mbox ?mbox .}.
OPTIONAL{?x foaf:homepage ?hpage .}
}Alternative Graph Pattern UNION 满足UNION前后的条件之一即可
1
2
3
4
5
6
7
8PREFIX dc10:<http://purl.org/dc/element/1.0/>
PREFIX dc11:<http://purl.org/dc/element/1.1/>
SELECT ?title
WHERE{
{?book dc10:title ?title .}
UNION{?book dc11:title ?title .}
}
Dataset specification
SPARQL queries are executed over an RDF dataset:
- One default graph
- Zero or more named graphs (identified by an IRI)
1
2
3
4SELECT ?v
WHERE{
GRAPH ?g {?v rdf:type umbel-sc:Volcano .}
}1
2
3
4
5SELECT ?v
WHERE{
_:x rdfs:seeAlso ?g .
GRAPH ?g {?v rdf:type umbel-sc:Volcano .}
}Query Forms
SELECT
1
2
3
4
5PREFIX foaf:<http://xmlns.com/foaf/0.1/>
SELECT ?name
FROM <http://example.org/foaf/aliceFoaf>
WHERE{?x foaf:name ?name.}返回一系列搜索结果(一般以表格呈现)
DISTINCT (去除重复结果)
1
2
3
4SELECT DISTINCT ?type
WHERE{
_:x rdf:type ?type .
}ORDER BY (以某种顺序显示结果)
1
2
3
4
5SELECT ?v
WHERE{
?v rdf:type umbel:Volcano;
rdfs:label ?name.
}ORDER BY ?nameBINDINGS(筛选时生成新变量)
1
2
3
4
5
6
7SELECT ?title ?price
WHERE{
?b ex:title ?title;
ex:price ?p;
ex:discount ?r.
BIND((?p- ?r) AS ?price)
}1
2
3
4
5
6
7SELECT ?title (?p- ?r) AS ?price
WHERE{
?b ex:title ?title;
ex:price ?p;
ex:discount ?r.
}VALUES 缩小范围的
1
2
3
4
5SELECT ?title
WHERE{
?b ex:title ?title.
VALUES ?b {(ex:Book1)(ex:Book2)}
}
DESCRIBE
1
2PREFIX ent:<http://org.example.com/employees#>
DESCRIBE ?x WHERE {?x ent:employeeID "1234".}返回
?x
所有有关内容CONSTRUCT
1
2
3
4
5PREFIX foaf:<http://xmlns.com/foaf/0.1/>
PREFIX org:<tttp://example.com/ns#>
CONSTRUCT{?x foaf:name ?name.}
WHERE{?x org:employeeName ?name.}返回新构建的triple,blank node 后面的词可以改变
BOUND
1
2OPTIONAL{?v p:location ?l .FILTER(?l=dbpedia:United_States)
}FILTER(!BOUND(?l))if
?l
has value,BOUND(?l)=true
ASK
1
2PREFIX foaf:<http://xmlns.com/foaf/0.1/>
ASK{?x foaf:name "Alice".}返回 T or F , 查看是否有至少一个符合条件。
INSERT
1
2
3
4
5
6
7PREFIX foaf:<http://xmlns.com/foaf/0.1/>
PREFIX org:<tttp://example.com/ns#>
INSERT DATA{
_c: org:employeeName "John";
org:employeeID 13579.
}返回原有 RDF Graph中增加新的 RDF triples。
DELETE
1
2
3
4PREFIX foaf:<http://xmlns.com/foaf/0.1/>
PREFIX org:<tttp://example.com/ns#>
DELETE DATA{_:a foaf:name "Alice".}返回删除后的新graph
DELETE/INSERT 连用(替换)
1
2
3
4
5PREFIX foaf:<http://xmlns.com/foaf/0.1/>
WITH <http://example/addresses>
DELETE { ?person foaf:givenName 'Bill' .}
INSERT { ?person foaf:givenName 'William' .}
WHERE { ?person foaf:givenName 'Bill' .}返回替换后的graph
CLEAR
1
2
3
4PREFIX foaf:<http://xmlns.com/foaf/0.1/>
WITH <http://example/addresses>
CLEAR GRAPH {GRAPH<http://person>}返回空Graph
MOVE
1
2
3
4PREFIX foaf:<http://xmlns.com/foaf/0.1/>
WITH <http://example/addresses>
MOVE {GRAPH<http://person>} To {GRAPH<http://person2>}将一个graph的内容覆盖移动到另一个。
Graph Database
Neo4j
open source graph database
Cypher
declarative graph query language
Create
1 | CREATE( |
relationship
1 | CREATE |
Match+return 查询
1 | MATCH (n) |
Match+Create 创建关系
1 | MATCH(charlie:Person{name:'Charlie'}),(rob:Person{name:'Bob'}) |
DELETE
1 | # delete single node |
Match+Set 更新
1 | # update a property |