Skip to main content
 首页 » 编程设计

python之rdflib之查询持久性存储

2025年05月04日23lhb25

我对链接数据和 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()