sparql 查詢語句快速入門


介紹

RDF is a directed, labeled graph data format for representing information in the Web. RDF is often used to represent, among other things, personal information, social networks, metadata about digital artifacts, as well as to provide a means of integration over disparate sources of information. This specification defines the syntax and semantics of the SPARQL query language for RDF.
The SPARQL query language for RDF is designed to meet the use cases and requirements identified by the RDF Data Access Working Group in RDF Data Access Use Cases and Requirements [UCNR].

SPARQL即SPARQL Protocol and RDF Query Language的遞歸縮寫,被專門設計用來訪問和操作RDF數據,是語義網的核心技術之一。W3C的RDF數據存取小組(RDF Data Access Working Group, RDAWG)對其進行了標准化。2008年1月15日,SPARQL正式成為一項W3C推薦標准。

我們可以將抽取的RDF三元組導入Apache Jena Fuseki,通過SPARQL進行查詢:

SParql demo

簡單查詢

SQL sparql
SELECT title from book where id='book1' SELECT ?title
WHERE
{ <http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> ?title .
}

Query Result:

title
"SPARQL Tutorial"

多字段匹配

RDF 數據

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

_:a  foaf:name   "Johnny Lee Outlaw" .
_:a  foaf:mbox   <mailto:jlow@example.com> .
_:b  foaf:name   "Peter Goodguy" .
_:b  foaf:mbox   <mailto:peter@example.org> .
_:c  foaf:mbox   <mailto:carol@example.org> .

sparql:

PREFIX foaf:   <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE
  { ?x foaf:name ?name .
    ?x foaf:mbox ?mbox }

SQL:

SELECT ?name ?mbox
from foaf

查詢結果:

name mbox
"Johnny Lee Outlaw" mailto:jlow@example.com
"Peter Goodguy" mailto:peter@example.org

數據屬性匹配

對於string類型,需要用雙引號包裹起來。

sparql:

SELECT ?v WHERE { ?v ?p "cat" }

SQL:

SELECT *
from ns
where p='cat'

對於數字類型:

sparql:

SELECT ?v WHERE { ?v ?p 42 }

SQL:

SELECT * from ns where p= 42

另外,在spaql里可以指定匹配的類型:

SELECT ?v WHERE { ?v ?p "abc"^^<http://example.org/datatype#specialDatatype> }

條件過濾

模糊匹配

通過regex函數可以進行字符串正則匹配,通過FILTER進行過濾


PREFIX  dc:  <http://purl.org/dc/elements/1.1/>
SELECT  ?title
WHERE   { ?x dc:title ?title
          FILTER regex(?title, "web", "i" ) 
        }

SQL:

SELECT * from table where title like '%web%'

數字比較


PREFIX  dc:  <http://purl.org/dc/elements/1.1/>
PREFIX  ns:  <http://example.org/ns#>
SELECT  ?title ?price
WHERE   { ?x ns:price ?price .
          FILTER (?price < 30.5)
          ?x dc:title ?title . }

SQL:

SELECT title,price from table where price <30.5

OPTIONAL(可選值)

RDF 數據,用戶Bob沒有mbox,而用戶Alice有兩個mbox

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

_:a  rdf:type        foaf:Person .
_:a  foaf:name       "Alice" .
_:a  foaf:mbox       <mailto:alice@example.com> .
_:a  foaf:mbox       <mailto:alice@work.example> .

_:b  rdf:type        foaf:Person .
_:b  foaf:name       "Bob" .

正常查詢,因為Bob沒有mbox,所以查詢不出來,可以通過OPTIONAL標記mbox為可選,這樣Bob就可以查詢出來。

sparql:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
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
"Bob"

可以看到, "Bob"的 mbox是空值。

對於關系型數據庫,可以假設兩個表

User { id,name}
Mbox {id,uid,name} (uid為外鍵)

對應的sql:

SELECT user.name AS name,mbox.name AS mboxName
FROM User user
LEFT OUTER JOIN Mbox mbox ON mbox.uid=user.id

OPTIONAL + FILTER

OPTIONAL 可以和FILTER 組合使用

PREFIX  dc:  <http://purl.org/dc/elements/1.1/>
PREFIX  ns:  <http://example.org/ns#>
SELECT  ?title ?price
WHERE   { ?x dc:title ?title .
          OPTIONAL { ?x ns:price ?price . FILTER (?price < 30) }
        }        

UNION

Data:

@prefix dc10:  <http://purl.org/dc/elements/1.0/> .
@prefix dc11:  <http://purl.org/dc/elements/1.1/> .

_:a  dc10:title     "SPARQL Query Language Tutorial" .
_:a  dc10:creator   "Alice" .

_:b  dc11:title     "SPARQL Protocol Tutorial" .
_:b  dc11:creator   "Bob" .

_:c  dc10:title     "SPARQL" .
_:c  dc11:title     "SPARQL (updated)" 

查詢:

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 } }

Query result:

title
"SPARQL Protocol Tutorial"
"SPARQL"
"SPARQL (updated)"
"SPARQL Query Language Tutorial"

排序

和sql一樣,使用ORDER BY 排序,示例如下:

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

SELECT ?name
WHERE { ?x foaf:name ?name ; :empId ?emp }
ORDER BY ?name DESC(?emp)

去重

和sql一樣,使用DISTINCT來去重,示例如下:

PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?name WHERE { ?x foaf:name ?name }

判斷是否存在

使用ask來判斷是否有解決方案

PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
ASK  { ?x foaf:name  "Alice" ;
          foaf:mbox  <mailto:alice@work.example> }

作者:Jadepeng
出處:jqpeng的技術記事本--http://www.cnblogs.com/xiaoqi
您的支持是對博主最大的鼓勵,感謝您的認真閱讀。
本文版權歸作者所有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM