使用耶拿查询 rdf 文件时出错



我已经成功加载了rdf文件。即使我也使用相同的 rdf 文件尝试了这个查询到门徒中。它运行正常。但是当我在日食中尝试它时,它给了我例外。

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.util.FileManager;
public class Test {
public static void main(String[] args) {
    // TODO Auto-generated method stub
sparqltest();}
static void sparqltest()
{
FileManager.get().addLocatorClassLoader(Test.class.getClassLoader());
Model model= FileManager.get().loadModel("C:/Users/avg/workspacejena32/Jena/data.rdf");
String queryString="PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
    "PREFIX foaf:<http://xmlns.com/foaf/0.1/> " +
    "SELECT ?y ?x" +
    "WHERE " +
    "{?y foaf:z ?x}";

Query query= QueryFactory.create(queryString);
QueryExecution qexec=QueryExecutionFactory.create(query, model);
try {
ResultSet results = qexec.execSelect();while ( results.hasNext()){
    QuerySolution soln = results.nextSolution();
    Literal name = soln.getLiteral("x");
    System.out.println(name);
}
} 
finally {
qexec.close();
}}}

我收到以下异常...

   Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "-" "- "" at line 1, column 130.
 Was expecting one of:
"graph" ...
"optional" ...
"minus" ...
"bind" ...
"service" ...
"filter" ...
"{" ...
"}" ...
";" ...
"," ...
"." ...
at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.perform(ParserSPARQL11.java:87)
at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.parse(ParserSPARQL11.java:40)
at com.hp.hpl.jena.query.QueryFactory.parse(QueryFactory.java:132)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:69)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:40)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:28)
at Test.sparqltest(Test.java:31)
at Test.main(Test.java:16)

删除"-"后,它给了我空结果。

null
null
null
null
null
null
null
null
null

如果我在查询中进行一些更改,例如

    String queryString="PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
    "PREFIX foaf:<http://xmlns.com/foaf/0.1/> " +
    "SELECT ?y ?x" +
    "WHERE " +
    "{?y foaf:Studies ?x}";

它什么也没给。空白结果。

您好先生非常感谢您,问题解决了。我根据您的两个建议进行了更改。它奏效了。实际上我使用了错误的变量。其实我完全不知道。所以我再次浏览了 sparql 教程并得到了它。

String queryString="PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
               "PREFIX foaf:<http://xmlns.com/foaf/0.1/> " +
               "SELECT * WHERE {" +
               "?person foaf:name ?x .}";

我得到的结果如下...

Charlie
Mary
John
George V

最新更新