我尝试查询这样的数据库:
from rdflib import Graph, Literal, URIRef
from rdflib.namespace import RDF, SKOS
from rdflib.plugins.stores import sparqlstore
# define endpoint according to https://www.stardog.com/docs/
endpoint = 'http://path/to/query' # http://<server>:<port>/{db}/query
# create store
store = sparqlstore.SPARQLUpdateStore()
# I only want to query
store.open(endpoint)
store.setCredentials('me', 'my_pw')
# What does this actually do? That runs through
default_graph = URIRef('some:stuff')
ng = Graph(store, identifier=default_graph)
# # If identifier is not defined, it crashes
# ng = Graph(store)
rq = """
SELECT ?foo ?bar
WHERE {
?something a <http://path/to/data/.ttl#SomeValues>.
?something <http://path/to/data/.ttl#foo> ?foo.
?something <http://path/to/data/.ttl#bar> ?bar.
}
"""
query_res = ng.query(rq)
for s, l in query_res:
print(s, l)
不幸的是,我目前没有得到任何结果:
<head><variable name="foo"></variable><variable name="bar"></variable></head><results></results></sparql>
我的问题是,identifier
中的Graph
正在做什么,即这是否重要,如果是,那么应该如何定义。当我没有定义它时,代码崩溃:
响应:b'{“message”:“在URI中找不到分隔符: N53e412e0f3a74d6eab7ed6da163463bf“}”
如果我输入任何带有冒号或斜杠的内容,它会一直运行(但查询仍然没有返回任何内容)。
任何人都可以简单地解释一下,应该放在哪里以及这是否可能是查询失败的原因(查询命令本身是正确的;当我从其他工具调用它时,它工作正常)?
答案 0 :(得分:1)
Graph
构造函数的identifier
参数允许识别RDFLib图。如果值为None
,则blank node将用作标识符。
但是,如果store
值为SPARQLUpdateStore
,则identifier
值也会用作SPARQL协议的default-graph-uri
,因此不能为空节点
因此,问题是:远程Triplestore中默认“未命名”图形的名称是什么?
<强>命名强>
Stardog包含几个常用命名集的别名 图表。提供这些非标准扩展是为了方便起见 可用于预期命名图形IRI的任何地方。这包括 SPARQL查询&amp;更新,属性图操作和配置 值。以下是特殊命名图IRI的列表。
Named Graph IRI Refers to -------------------------------- --------------------------------------------- tag:stardog:api:context:default the default (no) context graph tag:stardog:api:context:all all contexts, including the default graph tag:stardog:api:context:named all named graphs, excluding the default graph
我找不到私有Stardog端点的任何公开(似乎ABS的端点已关闭)。 DBpedia上的示例:
from rdflib import Graph, URIRef
from rdflib.plugins.stores import sparqlstore
store = sparqlstore.SPARQLUpdateStore()
store.open('http://dbpedia.org/sparql')
default_graph = URIRef('http://people.aifb.kit.edu/ath/#DBpedia_PageRank')
ng = Graph(store, identifier=default_graph)
rq = """
SELECT ?foo ?foobar {
?foo ?foobar ?bar
} LIMIT 100
"""
query_res = ng.query(rq)
for s, l in query_res:
print(s, l)
结果类似于should be。即使在您的代码中,未命名图表的名称也是唯一的问题,所获得的结果是正确的SPARQL XML results。
P.S。可能您可以尝试使用sparqlwrapper代替rdflib。