Push turtle file in bytes form to stardog database using pystardog

215 views Asked by At
def add_graph(file, file_name):
    file.seek(0)
    file_content = file.read()
    if 'snomed' in file_name:
        conn.add(stardog.content.Raw(file_content,content_type='bytes', content_encoding='utf- 
        8'), graph_uri='sct:900000000000207008')

Here I'm facing issues in push the file which I have downloaded from S3 bucket and is in bytes form. It is throwing stardog.Exception 500 on pushing this data to stardog database.

I tried pushing the bytes directly as shown below but that also didn't help.

conn.add(content.File(file), 
       graph_uri='<http://www.example.com/ontology#concept>')

Can someone help me to push the turtle file which is in bytes form to push in stardog database using pystardog library of Python.

1

There are 1 answers

3
notsodev On

I believe this is what you are looking for:

import stardog

conn_details = {
  'endpoint': 'http://localhost:5820',
  'username': 'admin',
  'password': 'admin'
}

conn = stardog.Connection('myDb', **conn_details) # assuming you have this since you already have 'conn', just sending it to a DB named 'myDb'

file = open('snomed.ttl', 'rb') # just opening a file as a binary object to mimic
file_name = 'snomed.ttl' # adding this to keep your function as it is

def add_graph(file, file_name):
    file.seek(0)
    file_content = file.read() # this will be of type bytes
    if 'snomed' in file_name:
        conn.begin() # added this to begin a connection, but I do not think it is required
        conn.add(stardog.content.Raw(file_content, content_type='text/turtle'), graph_uri='sct:900000000000207008')
        conn.commit() # added this to commit the added data

add_graph(file, file_name) # I just ran this directly in the Python file for the example.

Take note of the conn.add line where I used text/turtle as the content-type. I added some more context so it can be a running example.

Here is the sample file as well snomed.ttl:

<http://api.stardog.com/id=1> a :person ;
   <http://api.stardog.com#first_name> "John" ;
   <http://api.stardog.com#id> "1" ;
   <http://api.stardog.com#dob> "1995-01-05" ;
   <http://api.stardog.com#email> "[email protected]" ;
   <http://api.stardog.com#last_name> "Doe" .

EDIT - Query Test

If it runs successfully and there are no errors in stardog.log you should be able to see results using this query. Note that you have to specify the Named Graph since the data was added there. If you query without specifying, it will show no results.

SELECT * {
    GRAPH <sct:900000000000207008> {
        ?s ?p ?o
    }
}

You can run that query in stardog.studio but if you want it in Python, this will print the JSON result:

print(conn.select('SELECT * { GRAPH <sct:900000000000207008> { ?s ?p ?o } }'))