rdflib - 查询持久性存储

DMD*_*DMD 2 python rdflib linked-data

我对链接数据和 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')
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢。

Ins*_* IT 5

这是一个工作示例,

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()
Run Code Online (Sandbox Code Playgroud)