9

I have been working on various relationship extraction models in python and all the relationships are currently saved in dataframes or csv files. Eventually I would like to create an RDF graph. Since I am working in python I was going to create the RDF using RDFlib and read the RDF into Apache Jena into a model that I can query. Is this a good workflow or is there a better way?

3
  • 2
    Seems reasonable. Another way is to run Jena Fuseki and send the data to Fuseki, rather than write it out and read it in again. You can query Fuseki from python.
    – AndyS
    Oct 24, 2018 at 12:52
  • Depends on what do you plan to do with RDF afterwards. I suggest to consider 3 cases: sharing – write out RDF-HDT files; quering & use in the app – use rdflib.github.io/sparqlwrapper to put the RDF data into the triplestore; one-off inferencing – write a Java/Prolog program to load the data into an in-mem Jena or Cliopatria graph and do the steps you were planning to. Does that make sense? I'd probably write an N-Triples or RDF-HDT file anyway because it will be easy to automate the conversion and then just pass RDF around to the programs or triplestores. Oct 27, 2018 at 16:01
  • What I would like is to create an online interface that can query the Triple store
    – Joe124
    Oct 30, 2018 at 14:21

1 Answer 1

4

Quite late, but as I ran into a similar problem, heres my way how to talk to Jena TDB from python.

You could also make use of JayDeBeApi and the official Jena TDB JDBC Driver. You have to make sure that the JDBC Driver is available in the Java classpath.

import jaydebeapi
jclass = "org.apache.jena.jdbc.JenaJDBC"
conn_string = "jdbc:jena:tdb:location=/path/to/tdbstore"
conn = jaydebeapi.connect(jclass, conn_string)
cursor = conn.cursor()
query = """
SELECT DISTINCT ?a
WHERE  {
    ?a ?b ?b .
}
"""
cursor.execute(query)
# do something with the results
cursor.close()
conn.close()

You can also add &must-exist=true|false to conn_string denoting whether the store must exist or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.