Apache Jena and Python
Asked Answered
D

1

10

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?

Derward answered 23/10, 2018 at 11:57 Comment(3)
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.Jaxartes
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.Mallon
What I would like is to create an online interface that can query the Triple storeDerward
J
5

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.

Jitney answered 9/4, 2019 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.