前言
自由百科全書(shū)不僅僅應(yīng)當(dāng)可以自由編寫(xiě),而更應(yīng)該可以自由獲得。
DBpedia對(duì)Wikipedia的數(shù)據(jù)變成Linked Data形式,使得機(jī)器也能讀懂并自由獲得這些數(shù)據(jù)。
本文的主要目的是利用Javascript從DBpedia中獲取我們想要的數(shù)據(jù)。
對(duì)Linked Data不太了解的請(qǐng)參考: 關(guān)聯(lián)數(shù)據(jù)入門(mén)——RDF 。
?
SPARQL
Trying to use the Semantic Web without SPARQL is like trying to use a relational database without SQL.
——?Tim Berners-Lee
SPARQL是Semantic Web(語(yǔ)義網(wǎng))的SQL,用于數(shù)據(jù)查詢(xún)的語(yǔ)言。
?
SPARQL Endpoint
SPARQL查詢(xún)終端,是一種HTTP綁定協(xié)議,用于通過(guò)HTTP進(jìn)行SPARQL查詢(xún),并返回相應(yīng)數(shù)據(jù)。
DBpedia的SPARQL Endpoint地址是:
http://dbpedia.org/sparql
大家可以通過(guò)瀏覽器打開(kāi)這個(gè)頁(yè)面,進(jìn)行SPARQL查詢(xún)(最好FQ,沒(méi)FQ查詢(xún)經(jīng)常失敗,不太明白為什么= =)。?
不過(guò)這種查詢(xún)最終返回結(jié)果是HTML頁(yè)面,并不是我們想要的,我們可以通過(guò)設(shè)置Request Header的Accept屬性來(lái)指定返回?cái)?shù)據(jù)類(lèi)型。
例如如果指定為:text/xml,那么返回的便是RDF格式數(shù)據(jù)。
那么我們?nèi)绾屋斎隨PARQL查詢(xún)代碼呢?
只需通過(guò)get或者post方法用參數(shù)query,將代碼傳過(guò)去。例如:
如果想查詢(xún):select distinct ?Concept where {[] a ?Concept} LIMIT 100
則可利用該鏈接得到數(shù)據(jù):
http://dbpedia.org/sparql?query=select%20distinct %20 ?Concept %20 where %20 {[] %20 a %20 ?Concept} %20 LIMIT %20 100
其中空格被轉(zhuǎn)成%20。
?
實(shí)現(xiàn)細(xì)節(jié)
- 跨域
我們可以通過(guò)AJAX實(shí)現(xiàn)這一功能,但是AJAX在部分瀏覽器中無(wú)法跨域,然而很顯然我們想要的Linked Data幾乎都是跨域的。
實(shí)際上,在一些較老版本的瀏覽器,我們沒(méi)有不改變其數(shù)據(jù)形式的方法在前端進(jìn)行動(dòng)態(tài)跨域異步讀取。
不過(guò)我們可以通過(guò)服務(wù)器代理的方法來(lái)解決跨域問(wèn)題。
- GET or POST?
使用GET還POST呢?
這個(gè)可能出于很多方面考慮,但是考慮到GET可能被緩存,所以我們使用POST來(lái)避免數(shù)據(jù)被緩存。
- 以什么形式返回?cái)?shù)據(jù)
前面我們說(shuō)到用text/xml可以返回RDF數(shù)據(jù),但是RDF在Javascript中并不好處理,所以我們使用json方式返回,也就是需要將Accept設(shè)置成application/sparql-results+json。
?
實(shí)現(xiàn)
接口參考Python的SPARQL Wrapper
(
function
(root, factory) {
if
(
typeof
define === "function"
){
define(
"SPARQLWrapper", factory);
//
AMD || CMD
}
else
{
root.SPARQLWrapper
= factory();
//
<script>
}
}(
this
,
function
(){
'use strict'
function
SPARQLWrapper(endpoint){
this
.endpoint =
endpoint;
this
.queryPart = ""
;
this
.type = "json"
;
}
SPARQLWrapper.prototype
=
{
constructor: SPARQLWrapper,
setQuery:
function
(query){
this
.queryPart = "query=" +
encodeURI(query);
},
setType:
function
(type){
this
.type =
type.toLowerCase();
},
query:
function
(type, callback){
callback
= callback === undefined ? type :
this
.setType(type) ||
callback;
var
xhr =
new
XMLHttpRequest();
xhr.open(
'POST',
this
.endpoint,
true
);
xhr.setRequestHeader(
'Content-type', 'application/x-www-form-urlencoded'
);
switch
(
this
.type){
case
"json"
:
type
= "application/sparql-results+json"
;
break
;
case
"xml"
:
type
= "text/xml"
;
break
;
case
"html"
:
type
= "text/html"
;
break
;
default
:
type
= "application/sparql-results+json"
;
break
;
}
xhr.setRequestHeader(
"Accept"
, type);
xhr.onreadystatechange
=
function
(){
if
(xhr.readyState == 4
){
var
sta =
xhr.status;
if
(sta == 200 || sta == 304
){
callback(xhr.responseText);
}
else
{
console
&& console.error("Sparql query error: " + xhr.status + " " +
xhr.responseText);
}
window.setTimeout(
function
(){
xhr.onreadystatechange
=
new
Function();
xhr
=
null
;
},
0
);
}
}
xhr.send(
this
.queryPart);
}
}
return
SPARQLWrapper;
}));
使用方法,例如需要查詢(xún):
select distinct ?Concept where {[] a ?Concept} LIMIT 100
則該頁(yè)面為:
<!
DOCTYPE html
>
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
>
<
meta
http-equiv
="Content-Type"
content
="text/html; charset=gb2312"
/>
<
script
src
="SPARQLWrapper.js"
type
="text/javascript"
></
script
>
</
head
>
<
body
>
<
script
>
var
sparql
=
new
SPARQLWrapper(
"
http://dbpedia.org/sparql
"
);
sparql.setQuery(
'
select distinct ?Concept where {[] a ?Concept} LIMIT 100
'
);
sparql.query(
function
(json){
console.log(
json
);
});
</
script
>
</
body
>
</
html
>
?
小例子
http://pan.baidu.com/share/link?shareid=293219&uk=855675565
?
修訂記錄
- 修正關(guān)于跨域的錯(cuò)誤 2003.2.21
?
Javascript實(shí)現(xiàn)關(guān)聯(lián)數(shù)據(jù)(Linked Data)查詢(xún)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

