简体   繁体   中英

RDFlib count not working in SPARQL query

I am exploring python based library 'RDFlib' with native store (Sleepycat). A basic "select * ..." query works fine but COUNT does not seem to work.
Here is my code:

from rdflib import ConjunctiveGraph, Namespace, Literal 
path = './mytriplestore'
graph = ConjunctiveGraph('Sleepycat')
graph.open(path, create = False)
query = """SELECT count(?o)
   WHERE {
     <http://examplesubject> ?p ?o.
   }
   Limit 10"""

qres = graph.query(query)
for row in qres:
   print row

Error:

File "/usr/local/lib/python2.7/dist-packages/rdflib/graph.py", line 1085, in query
query_object, initBindings, initNs, **kwargs))
File "/usr/local/lib/python2.7/dist-packages/rdflib/plugins/sparql/processor.py", line 74, in query
parsetree = parseQuery(strOrQuery)
File "/usr/local/lib/python2.7/dist-packages/rdflib/plugins/sparql/parser.py", line 1058, in parseQuery
return Query.parseString(q, parseAll=True)
File "/usr/local/lib/python2.7/dist-packages/pyparsing-1.5.7-py2.7.egg/pyparsing.py", line 1006, in parseString
raise exc
pyparsing.ParseException: Expected "?" (at char 7), (line:1, col:8)

Could anyone help me solve this problem?

You have to use group by to be able to get result for count, try something like :

query = """SELECT count(?o) As ?count
   WHERE {
     <http://examplesubject> ?p ?o.
   }
   GROUP BY ?o
   Limit 10"""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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