Java 使用jena查询rdf文件时出错
我已成功加载rdf文件。甚至我也用相同的rdf文件在protege中尝试了这个查询。它运行正常。但当我在eclipse中尝试它时,它给了我一个例外Java 使用jena查询rdf文件时出错,java,sparql,rdf,jena,ontology,Java,Sparql,Rdf,Jena,Ontology,我已成功加载rdf文件。甚至我也用相同的rdf文件在protege中尝试了这个查询。它运行正常。但当我在eclipse中尝试它时,它给了我一个例外 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.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();
}}}
在删除“-”之后,它会给我空结果
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}";
String queryString=“前缀rdf:”+
“前缀foaf:”+
“选择?y?x”+
“哪里”+
“{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 .}";
您无法选择某些变量和
*
,因此请删除*
。解析器错误消息与显示的代码不一致。@AndyS我没有得到您想要说的内容?@AKSW ohh很抱歉,这是一个错误,我从查询中删除了*,然后我也得到了异常。我修改了上面的代码和异常。现在,在结束}
之前,查询末尾有一个-
。请删除它。它很可能是由与显示的代码不同的代码运行的-查询中的变量名和代码不匹配。@AndyS Sir我从代码中删除了-。所以它给我的结果是空的。如果我在protege中运行相同的查询和本体文件,那么它可以工作。我该怎么办?
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