我一直在Python中研究各种关系提取模型,所有关系当前都保存在数据帧或csv文件中。最终我想创建一个 RDF 图。由于我使用 python 工作,因此我将使用 RDFlib 创建 RDF,并将 RDF 读入 Apache Jena 中,形成我可以查询的模型。这是一个好的工作流程还是有更好的方法?
最佳答案
很晚了,但当我遇到类似的问题时,这是我如何从 python 与 Jena TDB 对话的方法。
您还可以使用JayDeBeApi和官方Jena TDB JDBC Driver 。您必须确保 JDBC 驱动程序在 Java 类路径中可用。
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()
您还可以将 &must-exist=true|false
添加到 conn_string
中,表示存储是否必须存在。
关于python - Apache Jena 和 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52948493/