3

I use the jena fuseki 2 docker image to create a fuseki server.

And I want to know if there is a way to upload my dataset to fuseki not from the web interface but programmatically, from SPARQL or Python or whatever else.

And also, is there a way to work with ontology from webprotégé directly from fuseki? Thanks for your answer

mee
  • 688
  • 8
  • 18

3 Answers3

3

I tried to upload file to fuseki using CURL, WGET, ./s-post, ./s-put with no effect. I generated request with postman's help. If someone, like me, is looking for correct CURL request, this is it:

curl --location --request POST 'http://{FUSEKIADDRESS}/{YOURDATASET}/data' --header 'Content-Type: multipart/form-data' --form 'file.ttl=@{}PATHtoFILE/file.ttl'
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
AdamN94
  • 31
  • 1
2

Fuseki comes with an HTTP API that can be used to upload data. You could use CURL or a Python HTTP library to communicate with this API. Fuseki also includes command-line helper scripts that can be used for calling the HTTP API. See https://jena.apache.org/documentation/fuseki2/soh.html#soh-sparql-http for more details.

Arnout Boks
  • 664
  • 3
  • 5
  • Thank you for your answer. Please, can you show me an example of how to use python with HTTP API of fuseki to upload my dataset? – mee Feb 06 '19 at 11:47
  • @mee just load the file as stream which makes the HTTP content body and use HTTP PUT as request type. And then indeed the appropriate request URI made of dataset and graph name. – UninformedUser Feb 07 '19 at 07:46
  • I tried this with no success: ```with open('pizza.owl', 'rb') as f: response=requests.put(http://localhost:3030/mydataset/update, data=f, stream=True)``` – mee Feb 07 '19 at 14:00
  • If I understand the documentation correctly, you probably need to PUT to either `http://localhost:3030/mydataset/data` or `http://localhost:3030/mydataset/data?default`. That would use the SPARQL Graph Store Protocol (https://www.w3.org/2012/01/http-rdf-update/), which (with a PUT-call) replaces the entire dataset. If you want to use the `/update` endpoint, which only adds/updates the given triples, you probably need to use POST as the HTTP method. – Arnout Boks Feb 07 '19 at 14:58
  • 1
    I tried `http://localhost:3030/mydataset/data` as url in my put method and got `response[400]`, and when I look at my container logs, I find this: `PUT http://localhost:3030/mydataset/data` , `PUT /mydataset :: 'data' :: [multipart/form-data] ?`, `400 Only files accepted in multipart file upload (got fileName=pizza.owl)`. – mee Feb 08 '19 at 08:36
  • Does it help to pass `headers={'Content-type': 'application/octet-stream'}` as extra parameter to `requests.put()`? – Arnout Boks Feb 08 '19 at 10:03
  • No, I got this error when I look at container logs: `Unknown content type for triples: [application/octet-stream] ` – mee Feb 08 '19 at 11:20
  • Thanks for all your help, It finally works!!! I use the **MultiparEncoder** from **requests_toolbet**: `from requests_toolbelt.multipart.encoder import MultipartEncoder` `multipart_data = MultipartEncoder(fields={'file': ('pizza.owl', open('pizza.owl', 'rb'), 'text/plain')})` `response=requests.put('http://localhost:3030/mydatset/data', data=multipart_data, auth=('admin','mypassword'),headers={'Content-Type': multipart_data.content_type})` – mee Feb 08 '19 at 11:59
2

If your RDF data is in turtle format, you can use the following code:

data = open('test.ttl').read()
headers = {'Content-Type': 'text/turtle;charset=utf-8'}
r = requests.post('http://localhost:3030/mydataset/data?default', data=data, headers=headers)

If your RDF data are in other format, you should change your headers, here is a list:

n3: text/n3; charset=utf-8
nt: text/plain
rdf: application/rdf+xml
owl: application/rdf+xml
nq: application/n-quads
trig: application/trig
jsonld: application/ld+json
Adam
  • 46
  • 2