附件是我在我的Android应用程序中使用的罐子。
以下是执行查询的代码:
com.hp.hpl.jena.query.Query query = com.hp.hpl.jena.query.QueryFactory.create(
queryString, Syntax.syntaxARQ);
com.hp.hpl.jena.query.QueryExecution qexec = com.hp.hpl.jena.query.QueryExecutionFactory.sparqlService(
endpoint, query);
我的查询使用嵌套的选择,这些选择似乎没有问题,但归结为:
(SUM(xsd:integer(?numOfMentions)) as ?sum)
我得到一个例外说:
`
W/System.err(22539): com.hp.hpl.jena.query.QueryParseException: Encountered " "as" "AS "" at line 18, column 35.
W/System.err(22539): Was expecting:
W/System.err(22539): ")" ...
W/System.err(22539):
W/System.err(22539): at com.hp.hpl.jena.sparql.lang.ParserARQ.perform(ParserARQ.java:86)
W/System.err(22539): at com.hp.hpl.jena.sparql.lang.ParserARQ.parse(ParserARQ.java:39)
W/System.err(22539): at com.hp.hpl.jena.query.QueryFactory.parse(QueryFactory.java:129)
W/System.err(22539): at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:72)
W/System.err(22539): at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:43)
W/System.err(22539): at com.hp.hpl.jena.query.QueryExecutionFactory.makeQuery(QueryExecutionFactory.java:516)
W/System.err(22539): at com.hp.hpl.jena.query.QueryExecutionFactory.create(QueryExecutionFactory.java:71)
W/System.err(22539): at technion.com.testapplication.async.FetchMekorotByScoreTask.doInBackground(FetchMekorotByScoreTask.java:42)
W/System.err(22539): at technion.com.testapplication.async.FetchMekorotByScoreTask.doInBackground(FetchMekorotByScoreTask.java:22)
W/System.err(22539): at android.os.AsyncTask$2.call(AsyncTask.java:292)
W/System.err(22539): at enter code herejava.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err(22539): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err(22539): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err(22539): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err(22539): at java.lang.Thread.run(Thread.java:818)
`
请帮忙
更新1:
附加部分查询,我收到失败。
+ "SELECT distinct ?source ?label ?text\n"
+ "?sums ?spanss ?labels\n"
+ "?sum ?description\n"
+ "WHERE {\n"
+ " SELECT ?source ?label ?text ?description\n"
+ " (SUM(xsd:integer(?numOfMentions)) as ?sum)\n"
+ " (group_concat(?numOfMentions;separator=\",\") as ?sums)\n"
+ " (group_concat(?spans;separator=\",\") as ?spanss)\n"
+ " (group_concat(?target_label ;separator=\",\") as ?labels)\n"
错误正在提及:
(SUM(xsd:integer(?numOfMentions)) as ?sum
更新
SELECT DISTINCT(?source) SAMPLE(?label) as ?label SAMPLE(?text) as ?text
SAMPLE(?sums) as ?sums SAMPLE(?spanss) as ?spanss SAMPLE (?labels) as ?labels
SAMPLE(?sum) as ?sum SAMPLE(?description) as ?description
WHERE {
SELECT ?source ?label ?text ?description
(SUM(xsd:integer(?numOfMentions)) as ?sum)
(group_concat(?numOfMentions;separator=",") as ?sums)
(group_concat(?spans;separator=",") as ?spanss)
(group_concat(?target_label ;separator=",") as ?labels)
WHERE {
SELECT ?mentions ?source ?numOfMentions ?target_label ?label ?text ?description
(group_concat(DISTINCT(?span);separator=",") as ?spans)
WHERE
{
{
values ?_var1 {prefix2:text-book-1-6-11 prefix2:text-book-1-6-16}
?_var1 a prefix:_var1.
?mentions rdf:type prefix:Mention.
?mentions prefix:target ?_var1.
?mentions prefix:source ?source.
?mentions prefix:numOfMentions ?numOfMentions.
?mentions prefix:span ?span.
?source rdfs:label ?label.
?source prefix:text ?text.
?source prefix:book ?source_book.
?source_book prefix:description ?description.
?_var1 rdfs:label ?target_label }
UNION {
values ?container {prefix2:text-book-1-6-11 prefix2:text-book-1-6-16}
values ?types {prefix:Section prefix:_var2}
?container a ?types.
?_var1 prefix:within ?container.
?_var1 a prefix:_var1.
?mentions rdf:type prefix:Mention.
?mentions prefix:target ?_var1.
?mentions prefix:source ?source.
?mentions prefix:numOfMentions ?numOfMentions.
?mentions prefix:span ?span.
?source rdfs:label ?label.
?source prefix:text ?text.
?source prefix:book ?source_book.
?source_book prefix:description ?description.
?_var1 rdfs:label ?target_label
}
UNION {
values ?books {prefix2:text-book-1-6-11 prefix2:text-book-1-6-16}
values ?types { prefix:_var4 }
?books a ?types.
?_var1 prefix:book ?books.
?_var1 a prefix:_var1.
?mentions rdf:type prefix:Mention.
?mentions prefix:target ?_var1.
?mentions prefix:source ?source.
?mentions prefix:numOfMentions ?numOfMentions.
?mentions prefix:span ?span.
?source rdfs:label ?label.
?source prefix:text ?text.
?source prefix:book ?source_book.
?source_book prefix:description ?description.
?_var1 rdfs:label ?target_label
}
}
group by ?target_label ?text ?mentions ?description
?source ?numOfMentions ?label
}
group by ?source ?label ?text ?description
}
order by DESC(?sum) offset 0 limit 20
答案 0 :(得分:0)
查询有几个问题:
DISTINCT
变量周围的?source
外部。这是非法的,也不一定是由它组成的内部查询。 GROUP BY
等汇总功能?source
SAMPLE
个变量
... AS ...
和(
中包含那些)
。例如,SAMPLE(?label) as ?label
必须为(SAMPLE(?label) as ?label)
(SAMPLE(?label) as ?label)
确实可能是(SAMPLE(?label) as ?label_)
,你应该重命名内部变量的名称,以获得" good"最终结果中的var名称。您的查询的可解析版本:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX prefix: <http://...>
PREFIX prefix2: <http://...>
SELECT ?source (SAMPLE(?label_) AS ?label) (SAMPLE(?text_) AS ?text) (SAMPLE(?sums_) AS ?sums) (SAMPLE(?spanss_) AS ?spanss) (SAMPLE(?labels_) AS ?labels) (SAMPLE(?sum_) AS ?sum) (SAMPLE(?description_) AS ?description)
WHERE
{ SELECT ?source (?label AS ?label_) (?text AS ?text_) (?description AS ?description_) (SUM(xsd:integer(?numOfMentions)) AS ?sum_) (GROUP_CONCAT(?numOfMentions ; separator=',') AS ?sums_) (GROUP_CONCAT(?spans ; separator=',') AS ?spanss_) (GROUP_CONCAT(?target_label ; separator=',') AS ?labels_)
WHERE
{ SELECT ?mentions ?source ?numOfMentions ?target_label ?label ?text ?description (GROUP_CONCAT(DISTINCT ?span ; separator=',') AS ?spans)
WHERE
{ { VALUES ?_var1 { prefix2:text-book-1-6-11 prefix2:text-book-1-6-16 }
?_var1 rdf:type prefix2:_var1 .
?mentions rdf:type prefix2:Mention ;
prefix2:target ?_var1 ;
prefix2:source ?source ;
prefix2:numOfMentions ?numOfMentions ;
prefix2:span ?span .
?source rdfs:label ?label ;
prefix2:text ?text ;
prefix2:book ?source_book .
?source_book prefix2:description ?description .
?_var1 rdfs:label ?target_label
}
UNION
{ VALUES ?container { prefix2:text-book-1-6-11 prefix2:text-book-1-6-16 }
VALUES ?types { prefix2:Section prefix2:_var2 }
?container rdf:type ?types .
?_var1 prefix2:within ?container ;
rdf:type prefix2:_var1 .
?mentions rdf:type prefix2:Mention ;
prefix2:target ?_var1 ;
prefix2:source ?source ;
prefix2:numOfMentions ?numOfMentions ;
prefix2:span ?span .
?source rdfs:label ?label ;
prefix2:text ?text ;
prefix2:book ?source_book .
?source_book prefix2:description ?description .
?_var1 rdfs:label ?target_label
}
UNION
{ VALUES ?books { prefix2:text-book-1-6-11 prefix2:text-book-1-6-16 }
VALUES ?types { prefix2:_var4 }
?books rdf:type ?types .
?_var1 prefix2:book ?books ;
rdf:type prefix2:_var1 .
?mentions rdf:type prefix2:Mention ;
prefix2:target ?_var1 ;
prefix2:source ?source ;
prefix2:numOfMentions ?numOfMentions ;
prefix2:span ?span .
?source rdfs:label ?label ;
prefix2:text ?text ;
prefix2:book ?source_book .
?source_book prefix2:description ?description .
?_var1 rdfs:label ?target_label
}
}
GROUP BY ?target_label ?text ?mentions ?description ?source ?numOfMentions ?label
}
GROUP BY ?source ?label ?text ?description
}
GROUP BY ?source
ORDER BY DESC(?sum)
OFFSET 0
LIMIT 20
答案 1 :(得分:0)
感谢所有的帮助, 但它也没有帮助。
最后,我设法使用androjena和以下内容查询我的远程服务端点:
QueryEngineHTTP queryEngineHTTP = new QueryEngineHTTP(Queries.ENDPOINT, queryString);
queryEngineHTTP.execSelect()
ResultSet resultSet = queryEngineHTTP.execSelect();
while (resultSet.hasNext()) {
QuerySolution rb = resultSet.nextSolution();
// parse rb here.
}
通过这种方式,我设法摆脱了解析器并获得了发布结果并自己解析它们。
我希望这可以帮助将来想要使用androjena的人。