我想從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)
----------------------------------
bif:contains和str之間沒有空格。這不就是問題嗎? – Artemis
我修改了它並再次運行它,並給出了相同的例外 –