2015-04-12 94 views
1

我想從javacode中使用sparql查詢DBPedia 對於某些查詢,它適用於另一個不起作用的anf。 我不認爲我的查詢中有錯誤,因爲我已經在DBPedia aparql端點中對其進行了測試。 這裏是我的Java代碼:使用sparql,java,Jena查詢DBPedia本體API

package ja1; 

import com.hp.hpl.jena.query.*; 
import com.hp.hpl.jena.rdf.model.*; 
import com.hp.hpl.jena.util.*; 

    public class Q_DBP_Online { 

     public static void main(String[]args) 
      { 
       sparqlTest(); 
      } 

    public static void sparqlTest() 
     {    
     /*String queryString = "SELECT ?o WHERE {"+ 
           "?s ?p ?o ."+ 
           "} LIMIT 10";*/ 
     String str="Obama"; 
     String queryString = "PREFIX pr:<http://xmlns.com/foaf/0.1/>" + 
           "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+ 
           "SELECT DISTINCT ?s ?label WHERE {" +           
           "?s rdfs:label ?label . "+ 
           "?s a pr:Person."+ 
           "FILTER (lang(?label) = 'en'). "+ 
           "?label bif:contains"+str+" ."+ 
           "}"; 

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

    } 
} 

因此,這是註釋的第一個查詢運行非常好,第二個不運行和我在NetBeans此消息:

SLF4J: Class path contains multiple SLF4J bindings. 
SLF4J: Found binding in [jar:file:/home/rodwan/Desktop/Th_Pr/apache-jena-2.12.1/lib/slf4j-log4j12-1.7.6.jar!/org/slf4j/impl/StaticLoggerBinder.class] 
SLF4J: Found binding in [jar:file:/home/rodwan/Desktop/Th_Pr/pellet-2.3.1/lib/jena/slf4j-log4j12-1.6.4.jar!/org/slf4j/impl/StaticLoggerBinder.class] 
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. 
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory] 

Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "(" "("" at line 1, column 169. 
Was expecting one of: 
    "values" ... 
    "graph" ... 
    "optional" ... 
    "minus" ... 
    "bind" ... 
    "service" ... 
    "filter" ... 
    "{" ... 
    "}" ... 
    ";" ... 
    "," ... 
    "." ... 

    at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.perform(ParserSPARQL11.java:102) 
    at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.parse$(ParserSPARQL11.java:53) 
    at com.hp.hpl.jena.sparql.lang.SPARQLParser.parse(SPARQLParser.java:37) 
    at com.hp.hpl.jena.query.QueryFactory.parse(QueryFactory.java:148) 
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:80) 
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:53) 
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:41) 
    at ja1.Q_DBP_Online.sparqlTest(Q_DBP_Online.java:38) 
    at ja1.Q_DBP_Online.main(Q_DBP_Online.java:18) 
Java Result: 1 
BUILD SUCCESSFUL (total time: 1 second) 

---------------------------------- 
+1

bif:contains和str之間沒有空格。這不就是問題嗎? – Artemis

+0

我修改了它並再次運行它,並給出了相同的例外 –

回答

4

我已經試過你例如,但似乎有一些缺失的空間。以下是爲我工作在DBpedia中:

PREFIX pr:<http://xmlns.com/foaf/0.1/> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT DISTINCT ?s ?label WHERE {?s rdfs:label ?label . ?s a pr:Person . FILTER (lang(?label) = 'en') . ?label bif:contains "Obama" .} 

翻譯到Java應該是這樣的:

String queryString = "PREFIX pr:<http://xmlns.com/foaf/0.1/>\n" + 
           "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"+ 
           "SELECT DISTINCT ?s ?label WHERE {" +        "?s rdfs:label ?label . "+ 
           "?s a pr:Person . "+ 
           "FILTER (lang(?label) = 'en') . "+ 
           "?label bif:contains \""+str+"\" ."+ 
           "}"; 

希望這有助於。

+0

這解決了問題:)非常感謝。 但是我得到一個新的錯誤: 線程「main」中的異常com.hp.hpl.jena.query.QueryParseException:第3行,第112列:無法解析的前綴名稱:bif:contains –

+0

它正在考慮將bif作爲命名空間,我不知道爲什麼! –

+0

對於你的第二個問題http://stackoverflow.com/questions/15876916/bifcontain-error-occurs-when-try-to-query-dbpedia-through-my-java-program可能會給出答案。 –

0

問題是查詢解析器會嚴格檢查您是否在查詢的開始部分使用了前綴聲明,並且顯然不是bif:contains - 實際上它是Virtuoso的內置屬性。

您已經提供了一個使用角撐的解決方案,即<bif:contains>

另一種方式是通過一些使用類QueryEngineHttp

QueryEngineHttp qe = new QueryEngineHttp(queryString); 

,只是發送查詢字符串到端點,而不是之前解析成一個查詢的對象。