python - 长 SELECT 查询的 SPARQLWrapper QueryBadFormed 错误

标签 python rdflib graphdb sparqlwrapper

可用于 SELECT 查询的查询大小是否有限制?我有一个很长的 SELECT 查询(在下面发布)不断抛出 QueryBadFormed 错误。我已经在 sparql.org 上验证了查询,并且已经在三重存储上运行了查询。我正在使用 GraphDB 8.6 SE。查询运行良好。

代码:

from SPARQLWrapper import SPARQLWrapper, SPARQLWrapper2, JSON,  CSV, TSV

# set endpoint and query
endpoint = r"http://localhost:7200/repositories/EDR"
query = get_dental_procedures_query()  # return query below

# get results from endpoint
sparql = SPARQLWrapper(endpoint)
setReturnFormat(JSON)  # I've also tried CSV and TSV
sparql.setQuery(query)
results = sparql.query().convert()

返回错误:

SPARQLWrapper.SPARQLExceptions.QueryBadFormed: QueryBadFormed: a bad request has been sent to the endpoint, probably the sparql query is bad formed.

这里是查询:

BASE    <http://purl.regenstrief.org/NDPBRN/dental-practice/>
PREFIX  mesial_surface: <http://purl.obolibrary.org/obo/FMA_no_fmaid_Mesial_surface_enamel_of_tooth>
PREFIX  exception: <http://purl.obolibrary.org/obo/OHD_0000404>
PREFIX  ada_num: <http://purl.obolibrary.org/obo/OHD_0000065>
PREFIX  occurrence_date: <http://purl.obolibrary.org/obo/OHD_0000015>
PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
PREFIX  resin_filling_proc: <http://purl.obolibrary.org/obo/OHD_0000042>
PREFIX  birth_date: <http://purl.obolibrary.org/obo/OHD_0000050>
PREFIX  restored_buccal: <http://purl.obolibrary.org/obo/OHD_0000222>
PREFIX  caries_finding: <http://purl.obolibrary.org/obo/OHD_0000024>
PREFIX  dental_finding: <http://purl.obolibrary.org/obo/OHD_0000010>
PREFIX  molar: <http://purl.obolibrary.org/obo/FMA_55638>
PREFIX  male_gender_role: <http://purl.obolibrary.org/obo/OMRSE_00000007>
PREFIX  endodontically_restored_tooth: <http://purl.obolibrary.org/obo/0000236>
PREFIX  root_canal_treatment: <http://purl.obolibrary.org/obo/OHD_0000230>
PREFIX  has_part: <http://purl.obolibrary.org/obo/BFO_0000051>
PREFIX  gender_role: <http://purl.obolibrary.org/obo/OMRSE_00000007>
PREFIX  part_of: <http://purl.obolibrary.org/obo/BFO_0000050>
PREFIX  inheres_in: <http://purl.obolibrary.org/obo/BFO_0000052>
PREFIX  missing_tooth_finding: <http://purl.obolibrary.org/obo/OHD_0000026>
PREFIX  pbrn_id: <http://purl.obolibrary.org/obo/OHD_0000273>
PREFIX  distal_surface: <http://purl.obolibrary.org/obo/FMA_no_fmaid_Distal_surface_enamel_of_tooth>
PREFIX  has_output: <http://purl.obolibrary.org/obo/OBI_0000299>
PREFIX  occlusal_surface: <http://purl.obolibrary.org/obo/FMA_no_fmaid_Occlusal_surface_enamel_of_tooth>
PREFIX  incisor: <http://purl.obolibrary.org/obo/FMA_12823>
PREFIX  graph: <http://purl.regenstrief.org/NDPBRN/dental-practice#>
PREFIX  patient_role: <http://purl.obolibrary.org/obo/OHD_0000190>
PREFIX  anterior_tooth: <http://purl.obolibrary.org/obo/OHD_0000307>
PREFIX  resin: <http://purl.obolibrary.org/obo/OHD_0000036>
PREFIX  restored_lingual: <http://purl.obolibrary.org/obo/OHD_0000226>
PREFIX  dental_proc: <http://purl.obolibrary.org/obo/OHD_0000002>
PREFIX  restored_surface: <http://purl.obolibrary.org/obo/OHD_0000208>
PREFIX  extracoronally_restored_tooth: <http://purl.obolibrary.org/obo/0000238>
PREFIX  lingual_surface: <http://purl.obolibrary.org/obo/FMA_no_fmaid_Lingual_surface_enamel_of_tooth>
PREFIX  dentition: <http://purl.obolibrary.org/obo/FMA_75152>
PREFIX  sesame: <http://www.openrdf.org/schema/sesame#>
PREFIX  lesion: <http://purl.obolibrary.org/obo/OHD_0000021>
PREFIX  labial_surface: <http://purl.obolibrary.org/obo/FMA_no_fmaid_Labial_surface_enamel_of_tooth>
PREFIX  has_input: <http://purl.obolibrary.org/obo/OBI_0000293>
PREFIX  posterior_tooth: <http://purl.obolibrary.org/obo/OHD_0000308>
PREFIX  extraction_proc: <http://purl.obolibrary.org/obo/OHD_0000057>
PREFIX  xsd:  <http://www.w3.org/2001/XMLSchema#>
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  restored_occlusal: <http://purl.obolibrary.org/obo/OHD_0000228>
PREFIX  is_about: <http://purl.obolibrary.org/obo/IAO_0000136>
PREFIX  restored_labial: <http://purl.obolibrary.org/obo/OHD_0000225>
PREFIX  coronally_restored_tooth: <http://purl.obolibrary.org/obo/0000237>
PREFIX  patient: <http://purl.obolibrary.org/obo/OHD_0000012>
PREFIX  prop: <http://purl.regenstrief.org/NDPBRN/property/>
PREFIX  restoration_proc: <http://purl.obolibrary.org/obo/OHD_0000004>
PREFIX  last_visit_date: <http://purl.obolibrary.org/obo/OHD_0000219>
PREFIX  tooth: <http://purl.obolibrary.org/obo/FMA_12516>
PREFIX  intracoronally_restored_tooth: <http://purl.obolibrary.org/obo/0000239>
PREFIX  bearer_of: <http://purl.obolibrary.org/obo/BFO_0000053>
PREFIX  first_visit_date: <http://purl.obolibrary.org/obo/OHD_0000218>
PREFIX  surgically_modified_tooth: <http://purl.obolibrary.org/obo/0000231>
PREFIX  canine: <http://purl.obolibrary.org/obo/FMA_55636>
PREFIX  facial_surface: <http://purl.obolibrary.org/obo/FMA_no_fmaid_Facial_surface_enamel_of_tooth>
PREFIX  restored_distal: <http://purl.obolibrary.org/obo/OHD_0000223>
PREFIX  premolar: <http://purl.obolibrary.org/obo/FMA_55637>
PREFIX  restored_tooth: <http://purl.obolibrary.org/obo/OHD_0000189>
PREFIX  restored_facial: <http://purl.obolibrary.org/obo/OHD_0000235>
PREFIX  material: <http://purl.obolibrary.org/obo/OHD_0000000>
PREFIX  missing_tooth_num: <http://purl.obolibrary.org/obo/OHD_0000234>
PREFIX  buccal_surface: <http://purl.obolibrary.org/obo/FMA_no_fmaid_Buccal_surface_enamel_of_tooth>
PREFIX  realizes: <http://purl.obolibrary.org/obo/BFO_0000055>
PREFIX  female_gender_role: <http://purl.obolibrary.org/obo/OMRSE_00000008>
PREFIX  restored_mesial: <http://purl.obolibrary.org/obo/OHD_0000227>
PREFIX  restored_incisal: <http://purl.obolibrary.org/obo/OHD_0000224>
PREFIX  visit: <http://purl.obolibrary.org/obo/OHD_0000009>
PREFIX  obo:  <http://purl.obolibrary.org/obo/>
PREFIX  incisal_surface: <http://purl.obolibrary.org/obo/FMA_no_fmaid_Incisal_surface_enamel_of_tooth>

SELECT DISTINCT  ?practice ?patient_id ?gender ?dob ?first_visit ?last_visit ?tooth_id ?tooth_num ?first_PCR ?first_RCT ?event_name ?ada_code ?event_date ?extract_date ?missing_date (if(bound(?surface_m), 1, 0) AS ?m) (if(bound(?surface_o), 1, 0) AS ?o) (if(bound(?surface_d), 1, 0) AS ?d) (if(bound(?surface_b), 1, 0) AS ?b) (if(bound(?surface_l), 1, 0) AS ?l) (if(bound(?surface_f), 1, 0) AS ?f) (if(bound(?surface_incisal), 1, 0) AS ?i)
WHERE
  { ?patient_i  a                   patient: ;
          birth_date:           ?dob ;
          pbrn_id:              ?pbrn_id
    OPTIONAL
      { ?gender_t  rdfs:subClassOf   male_gender_role: ;
                rdfs:label         ?gender_name .
        ?gender_i  sesame:directType  ?gender_t ;
                inheres_in:        ?patient_i
      }
  ?patient_i  first_visit_date:  ?first_visit ;
          last_visit_date:   ?last_visit .
  ?tooth_t  rdfs:subClassOf    tooth: ;
          ada_num:           ?ada_num .
  ?tooth_i  sesame:directType  ?tooth_t
  OPTIONAL
    { ?tooth_i  prop:first_PCR_date  ?first_PCR }
  OPTIONAL
    { ?tooth_i  prop:first_RCT_date  ?first_RCT }
  OPTIONAL
    { ?tooth_i  prop:extraction_date  ?extract_date }
  OPTIONAL
      { ?tooth_i  prop:missing_tooth_finding_date  ?missing_date }
  ?event_t  rdfs:subClassOf    dental_proc: ;
            rdfs:label         ?event_name .
  ?event_i  sesame:directType  ?event_t ;
            has_input:         ?patient_i ;
            has_output:        ?tooth_i ;
            occurrence_date:   ?event_date ;
            prop:ada_code      ?ada_code
  OPTIONAL
    { ?event_i  has_output:        ?surface_m .
      ?surface_m  sesame:directType  restored_mesial: ;
              part_of:           ?tooth_i
    }
  OPTIONAL
    { ?event_i  has_output:        ?surface_o .
      ?surface_o  sesame:directType  restored_occlusal: ;
              part_of:           ?tooth_i
    }
  OPTIONAL
    { ?event_i  has_output:        ?surface_d .
    ?surface_d  sesame:directType  restored_distal: ;
              part_of:           ?tooth_i
    }
  OPTIONAL
    { ?event_i  has_output:        ?surface_b .
      ?surface_b  sesame:directType  restored_buccal: ;
              part_of:           ?tooth_i
    }
OPTIONAL
  { ?event_i  has_output:        ?surface_l .
    ?surface_l  sesame:directType  restored_lingual: ;
              part_of:           ?tooth_i
    }
  OPTIONAL
    { ?event_i  has_output:        ?surface_f .
      ?surface_f  sesame:directType  restored_facial: ;
              part_of:           ?tooth_i
    }
  OPTIONAL
    { ?event_i  has_output:        ?surface_incisal .
      ?surface_incisal
              sesame:directType  restored_incisal: ;
              part_of:           ?tooth_i
    }
  BIND(strafter(str(?tooth_i), "tooth/") AS ?tooth_id)
  BIND(strafter(str(?patient_i), "patient/") AS ?patient_id)
  BIND(strbefore(str(?gender_name), " ") AS ?gender)
  BIND(strafter(str(?ada_num), "Tooth ") AS ?tooth_num)
  BIND(strafter(str(?pbrn_id), "NDPBRN practice ") AS ?practice)
}
limit 5

最佳答案

对长查询使用 POST HTTP 方法:

sparql.setMethod('POST')

更多信息:https://www.w3.org/TR/sparql11-protocol/#query-operation


在您的情况下,限制似乎与 urllib2 相关。上述方法似乎也不适用于 SPARQLWrapper2()

关于python - 长 SELECT 查询的 SPARQLWrapper QueryBadFormed 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52795711/

相关文章:

python - 如何修复 "Error during WebSocket handshake: Unexpected response code: 400"错误

python - 如果您从不重新定义任何内置函数,为什么还要导入内置函数?

python - 远程端点 RDFLib/Redland 上的 SPARQL 查询

http - 使用 RDF4J 返回错误查询 FactForge Sparql 端点

python - NLTK CorpusReader 一次标记一个文件

python - 如何在 RDFlib 中解析 N3

python - rdflib - 查询持久性存储

java - 如何在gremlin中添加数据类型为值集的顶点条目

图形数据库内存不足错误 : Java heap space

python - 按 "column"的字母顺序对python中的列表列表进行排序