简体   繁体   English

Sparql从BNCF端点查询JSON错误

[英]Sparql query JSON error from BNCF endpoint

I'm trying to retrieve results from the BNCF at this endpoint . 我正在尝试从此端点的BNCF检索结果。

My query (with "ab" as example) is: 我的查询(以“ ab”为例)是:

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?source ?label ?content
                WHERE {
                    ?source a skos:Concept;
                        skos:prefLabel ?label; 
                        skos:scopeNote ?content.
                FILTER regex(str(?label), "ab", "i")
            }

The query is correct in fact if you try to run it works. 实际上,如果您尝试运行它,则查询是正确的。 But when I try to get the results from my python this is the error: 但是当我尝试从我的python中获取结果时,这是错误:

SyntaxError: JSON Parse error: Unexpected EOF

This is my python code: 这是我的python代码:

__3store = "http://digitale.bncf.firenze.sbn.it/openrdf-workbench/repositories/NS_03_2014/query"
sparql = SPARQLUpdateStore(queryEndpoint=__3store)
sparql.setReturnFormat(JSON)
results = sparql.query(query_rdf).convert()
print json.dumps(result, separators=(',',':'))

I tried the code above according to this answer , before my code was like this: 在我的代码像这样之前,我根据此答案尝试了上面的代码:

__3store = "http://digitale.bncf.firenze.sbn.it/openrdf-workbench/repositories/NS_03_2014/query"
sparql = SPARQLWrapper(__3store,returnFormat="json")
sparql.setQuery(query_rdf)
result = sparql.query().convert() 
print json.dumps(result, separators=(',',':'))

but both throw the same error. 但都抛出相同的错误。

Does anyone know how to fix it? 有谁知道如何修理它? Thanks 谢谢

EDIT: 编辑:

This is python code, hope it is enough to understand 这是python代码,希望足以理解

import sys
sys.path.append ('cgi/lib')
import rdflib
from rdflib.plugins.stores.sparqlstore import SPARQLUpdateStore, SPARQLStore
import json
from SPARQLWrapper import SPARQLWrapper, JSON

#MAIN
print "Content-type: application/json"
print
prefix_SKOS =       "prefix skos:      <http://www.w3.org/2004/02/skos/core#>"
crlf = "\n"
query_rdf = ""
query_rdf += prefix_SKOS + crlf
query_rdf += '''
            SELECT DISTINCT ?source ?title ?content
                WHERE {
                    ?source a skos:Concept;
                        skos:prefLabel ?title; 
                        skos:scopeNote ?content.
                FILTER regex(str(?title), "ab", "i")
            }

        '''
__3store = "http://digitale.bncf.firenze.sbn.it/openrdf-workbench/repositories/NS_03_2014/query"
sparql = SPARQLWrapper(__3store,returnFormat="json")
sparql.setQuery(query_rdf)
result = sparql.query().convert() 

print result

Running this in Python shell returns: 在Python shell中运行它会返回:

Content-type: application/json


Warning (from warnings module):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SPARQLWrapper-1.6.4-py2.7.egg/SPARQLWrapper/Wrapper.py", line 689
RuntimeWarning: Format requested was JSON, but XML (application/sparql-results+xml;charset=UTF-8) has been returned by the endpoint
<xml.dom.minidom.Document instance at 0x105add710>

So I think the result is always an XML also if I specificied Json as a return format. 因此,如果我将Json指定为返回格式,那么我认为结果始终也是XML。

There are a couple of problems playing together here: 这里一起玩有几个问题:

First, you should only use SPARQLUpdateStore from rdflib if you want to access a SPARQL store via rdflib's Graph interface (eg, you can add triples, you can iterate over them, etc.). 首先,如果要通过rdflib的Graph接口访问SPARQL存储,则仅应使用SPARQLUpdateStore的SPARQLUpdateStore(例如,可以添加三元组,可以对其进行迭代等)。 If you want to write a SPARQL query yourself you should use SPARQLWrapper . 如果要自己编写SPARQL查询,则应使用SPARQLWrapper

Second, if you ask SPARQLWrapper to return JSON, what it does is actually ask the server for a couple of mime types that are most common and standardized for what we just call "json", as shown here and here : 第二,如果您要求SPARQLWrapper返回JSON,则实际上是向服务器询问几种最常见且标准化的MIME类型,如我们所谓的“ json”,如此此处所示:

_SPARQL_JSON = ["application/sparql-results+json", "text/javascript", "application/json"]

It seems as if your sever does understand application/sparql-results+json , but not a combined "give me any of these mime-types header" as rdflib compiles it for maximum interoperability (so your server essentially doesn't fully support HTTP Accept Headers ): 似乎您的服务器确实了解了application/sparql-results+json ,但是没有组合使用“给我这些mime-types标头中的任何一个”,因为rdflib对其进行了编译以实现最大的互操作性(因此您的服务器实质上不完全支持HTTP接受标头 ):

curl -i -G -H 'Accept: application/sparql-results+json' --data-urlencode 'query=PREFIX skos: 
<http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?source ?label ?content
WHERE {
 ?source a skos:Concept;
 skos:prefLabel ?label;
 skos:scopeNote ?content.
 FILTER regex(str(?label), "ab", "i")
}' http://digitale.bncf.firenze.sbn.it/openrdf-workbench/repositories/NS_03_2014/query

will return: 将返回:

HTTP/1.1 200 OK
Date: Mon, 18 May 2015 13:13:45 GMT
Server: Apache/2.2.17 (Unix) PHP/5.3.6 mod_jk/1.2.31
...
Content-Type: application/sparql-results+json;charset=UTF-8

{
  "head" : {
    "vars" : [ ],
    "vars" : [ "source", "label", "content" ],
    "link" : [ "info" ]
  },
  "results" : {
    "bindings" : [ {
      "content" : {
        "type" : "literal",
        "value" : "Il lasciare ingiustificatamente qualcuno o qualcosa di cui si è responsabili"
      },
      "source" : {
        "type" : "uri",
        "value" : "http://purl.org/bncf/tid/12445"
      },
      "label" : {
        "xml:lang" : "it",
        "type" : "literal",
        "value" : "Abbandono"
      }
    },
...

so everything is ok, but if we ask for the combined, more interoperable mime types: 所以一切都很好,但是如果我们要求组合的,更具互操作性的mime类型:

curl -i -G -H 'Accept: application/sparql-results+json,text/javascript,application/json' --data-urlencode 'query=PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?source ?label ?content
WHERE {
 ?source a skos:Concept;
 skos:prefLabel ?label;
 skos:scopeNote ?content.
 FILTER regex(str(?label), "ab", "i")
}' http://digitale.bncf.firenze.sbn.it/openrdf-workbench/repositories/NS_03_2014/query

we get an xml result: 我们得到一个xml结果:

HTTP/1.1 200 OK
Server: Apache/2.2.17 (Unix) PHP/5.3.6 mod_jk/1.2.31
...
Content-Type: application/sparql-results+xml;charset=UTF-8

<?xml version='1.0' encoding='UTF-8'?>
...

So long story short: it's a bug in the server you're using. 长话短说:这是您使用的服务器中的错误。 The following is a nasty workaround (it seems SPARQLWrapper doesn't just allow us to manually set the headers, but unconditionally overrides them in _createRequest ), but it works: 以下是一个令人讨厌的解决方法(似乎SPARQLWrapper不仅允许我们手动设置标头,而且在_createRequest无条件地覆盖了标头),但它可以工作:

In [1]: import SPARQLWrapper as sw

In [2]: sparql = sw.SPARQLWrapper("http://digitale.bncf.firenze.sbn.it/openrdf-workbench/repositories/NS_03_2014/query")

In [3]: sparql.setReturnFormat(sw.JSON)

In [4]: sparql.setQuery('''                                                                                                     PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?source ?label ?content
                WHERE {
                    ?source a skos:Concept;
                        skos:prefLabel ?label;
                        skos:scopeNote ?content.
                FILTER regex(str(?label), "ab", "i")
            }
''')

In [5]: request = sparql._createRequest()

In [6]: request.add_header('Accept', 'application/sparql-results+json')

In [7]: from urllib2 import urlopen

In [8]: response = urlopen(request)

In [9]: res = sw.Wrapper.QueryResult((response, sparql.returnFormat))

In [10]: result = res.convert()

In [11]: result
Out[11]:
{u'head': {u'link': [u'info'], u'vars': [u'source', u'label', u'content']},
 u'results': {u'bindings': [{u'content': {u'type': u'literal',
     u'value': u'Il lasciare ingiustificatamente qualcuno o qualcosa di cui si \xe8 responsabili'},
    u'label': {u'type': u'literal',
     u'value': u'Abbandono',
     u'xml:lang': u'it'},
    u'source': {u'type': u'uri', u'value': u'http://purl.org/bncf/tid/12445'}},
   ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM