Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Jena and Python

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?

like image 591
Joe124 Avatar asked Oct 23 '18 11:10

Joe124


People also ask

What is Apache Jena used for?

Apache Jena is an open source Semantic Web framework for Java. It provides an API to extract data from and write to RDF graphs. The graphs are represented as an abstract "model". A model can be sourced with data from files, databases, URLs or a combination of these.

What is Apache Jena Fuseki?

Apache Jena Fuseki is a SPARQL server. It can run as a operating system service, as a Java web application (WAR file), and as a standalone server.

How do I download Apache Jena?

Link to download apache Jena: https://jena.apache.org/download/inde... Link to download Java: https://www.java.com/en/download/manu...


1 Answers

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.

like image 162
user Avatar answered Nov 12 '22 18:11

user