2016-03-13 32 views
0

我已成功加載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) 
+0

你不能選擇一些變量和'*',因此請刪除'*'。 – AKSW

+0

解析器錯誤消息與所示的代碼不一致。 – AndyS

+0

@AndyS我沒有得到你想說的話? –

回答

0

你好先生非常感謝你,問題解決了。我根據你的兩個建議做了更改。它的工作。其實我正在使用錯誤的變量。其實我完全沒有意識到這一點。所以我再次通過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 
0

取出後 ' - ' 它給了我空的結果。

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}"; 

做一些改變它給什麼。空白的結果。

+0

它很可能是由不同於所顯示的代碼運行 - 查詢中的變量名稱和代碼不匹配。 – AndyS

+0

@AndyS先生,我從我的代碼中刪除了。所以它給我空效果。如果我運行相同的查詢和本體文件的保護,那麼它的工作原理。我該怎麼辦? –

+0

非常感謝您先生.... !!!! –