简体   繁体   English

具有Fuseki的SPARQL插入不起作用

[英]SPARQL insert with Fuseki not working

I am using fuseki embeded from a Java application : 我正在使用从Java应用程序嵌入的fuseki:

Dataset ds = DatasetFactory.createTxnMem() ;

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

The query endpoint is working fine and I can execute SELECT requests. 查询端点工作正常,我可以执行SELECT请求。 However, when I want to insert values it does return a 204 HTTP code but no data is added to the graph. 但是,当我要插入值时,它确实返回204 HTTP代码,但没有数据添加到图形中。 Here is what I did : 这是我所做的:

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

<Response [204]>

then I select everything to see if it worked : 然后我选择所有内容以查看是否有效:

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

and I get 我得到

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

On the client side I have a basic python script : 在客户端,我有一个基本的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

This python script works now, it has been adapted from the answer below. 这个python脚本现在可以工作了,它已经从下面的答案中改编了。

This is maybe not a good answer but just to show you that it works for me. 这可能不是一个很好的答案,而只是告诉您它对我有用。

Jena Fuseki 2.6.0 耶拿·富塞基2.6.0

Start embedded server 启动嵌入式服务器

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() ;
    }
}

Insert data 插入资料

Request 请求

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

Output 产量

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

Query data 查询数据

Request 请求

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

Output 产量

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

Diagnosis 诊断

I'm not a Python expert, but shouldn't the query string be put into the data array as it's a POST request? 我不是Python专家,但是由于它是POST请求,因此不应该将查询字符串放入数据数组中吗? Something like 就像是

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"}'})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM