0
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.

Nilesh Gupta
  • 71
  • 2
  • 8

1 Answers1

0

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> "john.doe@example.com" ;
   <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 } }'))
notsodev
  • 1,447
  • 2
  • 13
  • 20
  • I tried this code earlier also. This code is running successfully but data is not getting reflected in stardog database. – Nilesh Gupta Sep 23 '22 at 08:38
  • I want to avoid reading the file_content from file bytes. I tried passing directly bytes but then code is running and I'm not able to see triples in database. Note : I have downloaded the file from S3 bucket. – Nilesh Gupta Sep 23 '22 at 08:40
  • @NileshGupta I updated the answer to show query test. Try that after one of those successful runs. If you still are not seeing results, check `stardog.log` in your `STARDOG_HOME` during a run to see what may be happening. – notsodev Sep 23 '22 at 12:28