0

Can anyone please let me know how to save the results of SPARQL queries in AWS Neptune to CSV file. I am using sagemaker notebook to connect to database cluster. Please find the below query. Any help would be greatly appreciated.

%%sparql

PREFIX mo: <>

SELECT (strafter(str(?s), '#') as ?sample)WHERE { ?s mo:belongs_to_project ?o.
    FILTER(regex(str(?o), "PROJECT_00001"))}
Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
Meghana Kb
  • 43
  • 5

1 Answers1

3

The results from a query can be saved to a Python variable using:

%%sparql --store-to result

Then in another cell you could write a little Python code that takes the result (which will be in JSON) and creates a CSV containing whatever data you need from the result using the Python CSV helper classes and methods (https://docs.python.org/3/library/csv.html).

UPDATED: to add that if you just want the whole result as a CSV file you can achieve that by running a curl command from a cell using the %%bash magic. Here is an example:

%%bash
curl -X POST --data-binary 'query=SELECT ?s ?p ?o WHERE {?s ?p ?o} LIMIT 10' -H "Accept: text/csv" https://{cluster endpoint}:8182/sparql > results.csv
Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38