通过RDFLib-SQLAlChemy将RDF数据加载到PostgreSQL表 [英] Loading RDF data to PostgreSQL Table through RDFLib-SQLAlchemy

查看:49
本文介绍了通过RDFLib-SQLAlChemy将RDF数据加载到PostgreSQL表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NT格式的大型RDF数据集(Geonames数据集:18 GB)。我想使用rdflib_sqlalchemy.SQLAlchemy将其加载到PostgreSQL关系表中。我知道这是可行的(performing sparql query on the rdf data stored in relational database)。然而,我不确定如何做到这一点。你能给我举个例子吗?

我的下一个目标是使用RDFLib从python编写一个SPARQL查询。我知道怎么做。提前感谢您的帮助。

推荐答案

安装以下Python库:

pip install rdflib
pip install rdflib-sqlalchemy
pip install psycopg2

运行以下Python代码:

from rdflib import plugin
from rdflib.graph import Graph
from rdflib.store import Store
from rdflib_sqlalchemy import registerplugins

registerplugins()

SQLALCHEMY_URL ="postgresql+psycopg2://user:password@hostname:port/databasename"

store = plugin.get("SQLAlchemy", Store)(identifier="my_store")
graph = Graph(store, identifier="my_graph")
graph.open(SQLALCHEMY_URL, create=True)

graph.parse("demo.nt", format="nt")

result = graph.query("select * where {?s ?p ?o} limit 10")

for subject, predicate, object_ in result:
    print(subject, predicate, object_)

graph.close()

"demo.nt"是要导入的N-Triples文件。我使用此工具进行测试:

<http://example.org/a> <http://example.org/b> <http://example.org/c> .
成功导入后,您的数据库包含五个表(例如,kb_[ome_id]_asserted_Statements),其中填充了三元组。主机最多打印10个三元组。

在Windows 10、PostgreSQL 10.5、Python 3.5.4(全部为64位)、rdflib-4.2.2、rdflib-sqlalChemy-0.3.8和mental copg2-2.7.5上测试。

这篇关于通过RDFLib-SQLAlChemy将RDF数据加载到PostgreSQL表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆