我已成功加載rdf文件。即使我也試過這個查詢成爲同樣的rdf文件的保護。它正常運行。但是當我在eclipse中嘗試它時,它給我例外。使用jena查詢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)
你不能選擇一些變量和'*',因此請刪除'*'。 – AKSW
解析器錯誤消息與所示的代碼不一致。 – AndyS
@AndyS我沒有得到你想說的話? –