Search code examples
javasparqlfusekiapache-jena

SPARQL insert with Fuseki not working


I am using fuseki embeded from a Java application :

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. However, when I want to insert values it does return a 204 HTTP code but no data is added to the graph. 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 :

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.


Solution

  • This is maybe not a good answer but just to show you that it works for me.

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