使用 sparql、java、Jena API 查询 DBPedia ontology
Querying the DBPedia ontology using sparql, java, Jena API
我正在尝试使用 java代码中的 sparql 查询 DBPedia
对于某些查询,它可以完美地工作,而对于另一个它不起作用。
我认为我的查询没有错误,因为我已经在 DBPedia aparql 端点中对其进行了测试。
这是我的 java 代码:
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)
----------------------------------
我试过你的例子,但似乎缺少一些空格。以下内容在 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+"\" ."+
"}";
希望对您有所帮助。
问题在于查询解析器会严格检查您是否在查询的开头使用了所有已声明的前缀,而 bif:contains
显然不是这种情况 - 这实际上是一个内置的 属性 的 Virtuoso。
您已经提供了一种使用尖括号的解决方案,即 <bif:contains>
.
另一种方法是通过
之类的方式使用 class QueryEngineHttp
QueryEngineHttp qe = new QueryEngineHttp(queryString);
并且只需将查询字符串发送到端点,而不是之前将其解析为查询对象。
我正在尝试使用 java代码中的 sparql 查询 DBPedia 对于某些查询,它可以完美地工作,而对于另一个它不起作用。 我认为我的查询没有错误,因为我已经在 DBPedia aparql 端点中对其进行了测试。 这是我的 java 代码:
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)
----------------------------------
我试过你的例子,但似乎缺少一些空格。以下内容在 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+"\" ."+
"}";
希望对您有所帮助。
问题在于查询解析器会严格检查您是否在查询的开头使用了所有已声明的前缀,而 bif:contains
显然不是这种情况 - 这实际上是一个内置的 属性 的 Virtuoso。
您已经提供了一种使用尖括号的解决方案,即 <bif:contains>
.
另一种方法是通过
之类的方式使用 class QueryEngineHttpQueryEngineHttp qe = new QueryEngineHttp(queryString);
并且只需将查询字符串发送到端点,而不是之前将其解析为查询对象。