2017-07-20 33 views
0

我使用定式從Java應用程序內嵌:SPARQL插入與定式不工作

Dataset ds = DatasetFactory.createTxnMem() ; 

FusekiEmbeddedServer server = FusekiEmbeddedServer.create() 
     .setPort(3333) 
     .add("/ds", ds, true) 
     .build() ; 
server.start() ; 

查詢終端工作正常,我可以執行SELECT請求。但是,當我想要插入值時,它會返回一個204 HTTP代碼,但是沒有數據被添加到圖中。下面是我做的:

PREFIX dc: <http://purl.org/dc/elements/1.1/>INSERT DATA{ <http://example/book3> dc:title "A new book"} 

<Response [204]> 

然後我選擇的一切,看看它的工作:

SELECT DISTINCT * WHERE {?s ?q ?o} 

,我得到

<?xml version="1.0"?> 
<sparql xmlns="http://www.w3.org/2005/sparql-results#"> 
    <head> 
    <variable name="s"/> 
    <variable name="q"/> 
    <variable name="o"/> 
    </head> 
    <results> 
    </results> 
</sparql> 

在客戶端,我有一個基本的Python腳本:

port = 3333 
test_add = 'PREFIX dc: <http://purl.org/dc/elements/1.1/>INSERT DATA{ <http://example/book3> dc:title "A new book"}' 
try : 
    print requests.post("http://localhost:"+str(port)+"/ds", data={'update': test_add}) 
    print urllib2.urlopen("http://localhost:"+str(port)+"/ds?query=SELECT%20DISTINCT%20*%20WHERE%20{?s%20?q%20?o}").read() 

except Exception as e : 
    print e 

這個python腳本現在可以工作,它已經根據下面的答案進行了改編。

+0

請問您可以展示客戶端代碼進行操作嗎? – AndyS

+0

這只是一個非常基本的Python腳本(我將它添加到問題中)。我在test_add變量中測試了幾個東西 – Yotm

+0

您可以嘗試在沒有布爾參數的情況下創建服務器嗎?也許它只能通過HTTP進行讀取。 – AKSW

回答

0

這可能不是一個好的答案,但只是爲了告訴你,它適用於我。

耶拿定式2.6.0

啓動嵌入式服務器

public class FusekiTestServer { 
    public static void main(String[] args) { 
     Dataset ds = DatasetFactory.createTxnMem() ; 

     FusekiEmbeddedServer server = FusekiEmbeddedServer.create() 
       .setPort(3333) 
       .add("/ds", ds, true) 
       .build() ; 
     server.start() ; 
    } 
} 

插入數據

請求

curl --request POST http://localhost:3333/ds --data-urlencode 'update=PREFIX dc: <http://purl.org/dc/elements/1.1/>INSERT DATA{ <http://example/book3> dc:title "A new book"}' 

輸出

<html> 
<head> 
</head> 
<body> 
<h1>Success</h1> 
<p> 
Update succeeded 
</p> 
</body> 
</html> 

查詢數據

請求

curl --request GET http://localhost:3333/ds --data-urlencode 'query=SELECT DISTINCT * WHERE {?s ?q ?o}' 

輸出

<http://example/book3> 
     <http://purl.org/dc/elements/1.1/title> 
       "A new book" . 

診斷

我不是一個Python的專家,但不能查詢字符串被放入數據數組,因爲它是一個POST請求?類似於

requests.post("http://localhost:"+str(port)+"/ds, data={'update': 'PREFIX dc: <http://purl.org/dc/elements/1.1/>INSERT DATA{ <http://example/book3> dc:title "A new book"}'}) 
+0

你是完全正確的,使用像GET這樣的POST是沒有意義的。我不知道我爲什麼這樣做。我試圖在之前將數據放入數據庫中,但它不起作用。我當時一定忘記了一些東西。當我今天早上嘗試它時,它正在工作!非常感謝,我會更新我的代碼 – Yotm