gpt4 book ai didi

python - rdflib - 查询持久性存储

转载 作者:太空宇宙 更新时间:2023-11-04 08:46:27 26 4
gpt4 key购买 nike

我对链接数据和 rdflib 很陌生,我很迷茫。我正在尝试使用 rdflib 与“Sleepycat”建立持久性存储以加载 DBLP 数据库 rdf 文件,然后开始查询它。这就是我所做的:

import rdflib

graph = rdflib.Graph("Sleepycat")
graph.open("C:\Users\Maral\Desktop\Springer-DBLP\Mydblp", create=True)
graph.parse("C:\Users\Maral\Desktop\dblp.rdf", format = 'xml')

花了将近 2 个小时,但现在看来 dblp.rdf 已加载、解析并存储在 Mydblp 中。但是 len(graph) 返回 0,我不知道如何访问和查询数据。

我是否遗漏了任何步骤?数据是否正确加载?所有示例都是关于向图形添加三元组,但我只想查询已经存在的内容。

谢谢。

最佳答案

这是一个工作示例,

from rdflib import ConjunctiveGraph, Namespace, Literal
from rdflib.store import NO_STORE, VALID_STORE

graph = ConjunctiveGraph('Sleepycat')

rt = graph.open("C:\Users\Maral\Desktop\Springer-DBLP\Mydblp", create=False)

if rt == NO_STORE:
# There is no underlying Sleepycat infrastructure, create it
graph.open(path, create=True)
else:
assert rt == VALID_STORE, 'The underlying store is corrupt'

print('Triples in graph before add: ', len(graph))

ontologies = rdflib.Graph()
ontologies.parse('C:\Users\Maral\Desktop\dblp.rdf',format='xml')
for onto in ontologies:
graph.add(onto)
print ('Triples in graph after add: ', len(graph))

print (graph.serialize(format='xml'))

# close when done, otherwise sleepycat will leak lock entries.
graph.close()

关于python - rdflib - 查询持久性存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40104729/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com