知识图谱查询

Property Graphs

定义

Also called Labeled Property Graphs (LPG)

It is a framework for representing data and metadata with a graph of nodes and links

  • both nodes and links may have additional key/value pairs (“properties”)
  • nodes are “just” nodes, not necessarily URLs

Link annotations are very useful to assign temporal, spatial, provenance, etc.

LPG vs. RDF

Similarity:
Both represent directed graphs as a basic data structure;
Both have associated graph-oriented query languages;
In practice, both are used as “graph stores”, accessible via HTTP and/or various API-s;

Differences:
RDF has an emphasis on OWA, and is rooted in the Web via URL-s. Not the case for PG, PG node is oblivious to what it “contains”: can be a URL, can be a literal; (node不一定是uri)
PG includes the possibility to add simple key/value pairs to “relationships” (i.e., RDF predicates) (关系中可以带属性)

RDF triple-stores vs. Graph DBs

In RDF triple-stores everything is expressed in terms of SPO and its predicates can’t have attributes;
In Graph DBs predicates can have attributes;
Fair to say that RDF triple-stores are a kind of Graph DB;

SPARQL (SPARQL Protocol and RDF Query Language)

SELECT/ASK/CONSTRUCT/DESCRIBE

Main idea: Pattern matching (想像成对图进行搜索)

Queries describe sub-graphs of the queried graph;
Graph patterns are RDF graphs specified in Turtle syntax, which contain variables (prefixed by either “?” or “$”);
Sub-graphs that match the graph patterns yield a result

图模式 Graph Pattern

基本匹配: Where a set of triple patterns must match
集合匹配 {}: Where a set of graph patterns must all match
可选匹配 OPTIONAL: Where additional patterns may extend the solution => 简单理解就是这个节点有属性则加,无则省略
附加匹配 UNION: Where two or more possible patterns are tried
命名图 GRAPH: Where patterns are matched against named graphs

FILTER NOT EXISTS vs. MINUS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Data
@prefix : <http://example/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

:alice rdf:type foaf:Person .
:alice foaf:name "Alice" .
:bob rdf:type foaf:Person .

Query:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?person
WHERE
{
?person rdf:type foaf:Person .
FILTER NOT EXISTS { ?person foaf:name ?name } # => 筛选出没有这条边的点
}

Result
person
<http://example/bob>


Data:
@prefix : <http://example/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

:alice foaf:givenName "Alice" ;
foaf:familyName "Smith" .
:bob foaf:givenName "Bob" ;
foaf:familyName "Jones" .
:carol foaf:givenName "Carol" ;
foaf:familyName "Smith"

Query:
PREFIX : <http://example/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT DISTINCT ?s
WHERE {
?s ?p ?o .
MINUS {
?s foaf:givenName "Bob" .
}
}

Result:
s
<http://example/carol>
<http://example/alice>

简单的说, FILTER 共享对象引用 MINUS 不共享, 例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Data: 
@prefix : <http://example/> .
:a :b :c .

Query:
SELECT *
{
?s ?p ?o
FILTER NOT EXISTS { ?x ?y ?z } # => 这里 ?s = ?x
}

Result:
Null


Data:
@prefix : <http://example/> .
:a :b :c .

Query:
SELECT *
{
?s ?p ?o
MINUS
{ ?x ?y ?z } # => 这里 ?s != ?x
}

Result
s p o
<http://example/a> <http://example/b> <http://example/c>


Aggregation

SELECT (SUM(?lprice) AS ?totalPrice)
GROUP BY ?org
GROUP BY ?a ?b ?c
HAVING (SUM(?lprice) > 10)

Subqueries

原则: 先计算内部,在算外部的

RDF Dataset

RDF Dataset =
default graph
+ named graph 1
+ named graph 2
+ …

Separate graphs enable you to reason about who said what and when

常用SPARQL语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Data:
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:givenName "John" .
_:a foaf:surname "Doe" .

Query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE {
?P foaf:givenName ?G ;
foaf:surname ?S
BIND(CONCAT(?G, " ", ?S) AS ?name) # => Creating Values with Expressions

}

Result:
name
“John Doe”


Data:
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix : <http://example.org/book/> .
@prefix ns: <http://example.org/ns#> .

:book1 dc:title "SPARQL Tutorial" .
:book1 ns:price 42 .
:book2 dc:title "The Semantic Web" .
:book2 ns:price 23 .

Query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?title
WHERE { ?x dc:title ?title
FILTER regex(?title, "^SPARQL") # => Restricting the Value of
Strings
# FILTER (?price < 30.5)
}

Result:
title
"SPARQL Tutorial"




本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!