正如Ian Dickinson在his answer中指出的那样,您没有向我们显示数据,也没有向我们显示reader
的确切值,因此很难准确判断出现了什么问题。这听起来像你需要查询沿
?actor rdfs:label ?label
其中?label
是你感兴趣的演员的名字线调整的东西。如果你使用字符串连接就像你在
做
String queryNew = "select ?y ?z where {" + reader + " <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?z . }";
你打开自己的SPARQL注入攻击。例如,如果reader
包含更多SPARQL查询文本,会发生什么情况? (另外,因为rdf:type
是如此常用,SPARQL实际上允许您使用a
缩写它,如?actor a dbpedia-owl:Person
。)为了避免此类问题,Jena提供了一个ParameterizedSparqlString,它将负责正确转义。还要注意RDF区分普通文字,例如"Richard Dreyfuss"
和带语言标记的字符串,例如"Richard Dreyfuss"@en
。这意味着您可能希望在查询中包含语言标记。最后,请注意,DBpedia中的某个标签可能不止一个。使用发现资源与标签"Richard Dreyfuss"@en
,他们所有的RDF类型的沿参数SPARQL串继承人的代码:
import com.hp.hpl.jena.query.ParameterizedSparqlString;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
public class DBpediaQueryExample {
public static void main(String[] args) {
/*
* A typed literal: the name Richard Dreyfuss in English. In SPARQL
* this is written as "Richard Dreyfuss"@en.
*/
final Literal richardDreyfuss = ResourceFactory.createLangLiteral("Richard Dreyfuss", "en");
/*
* A parameterized SPARQL string for the query. Using this, along with
* the various setZZZ(...) methods can prevent some SPARQL injection
* attacks by doing proper escaping.
*/
final ParameterizedSparqlString queryString = new ParameterizedSparqlString(
"prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
"\n" +
"select ?resource ?type where {\n" +
" ?resource a ?type ;\n" +
" rdfs:label ?label .\n" +
"}");
/*
* Fill in "Richard Dreyfuss"@en for the ?label.
*/
queryString.setLiteral("label", richardDreyfuss);
/*
* Get a query execution that will run against the DBpedia SPARQL
* endpoint, and will use the parameterized query.
*/
final QueryExecution exec = QueryExecutionFactory.sparqlService(
"http://dbpedia.org/sparql",
queryString.asQuery());
/*
* Execute the query to produce a result set, and format it nicely.
*/
final ResultSet results = exec.execSelect();
ResultSetFormatter.out(results);
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------
| resource | type |
=================================================================================================================================================
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://www.w3.org/2002/07/owl#Thing> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/ActorsFromLosAngeles,California> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/AmericanFilmActors> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/AmericanTelevisionActors> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/ontology/Agent> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/ontology/Person> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://umbel.org/umbel/rc/Actor> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://schema.org/Person> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://xmlns.com/foaf/0.1/Person> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/AmericanComedians> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Comedian109940146> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/JewishActors> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/PeopleFromBrooklyn> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Adult109605289> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Communicator109610660> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/LivingPeople> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Object100002684> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/PeopleWithBipolarDisorder> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Person100007846> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Whole100003553> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/YagoLegalActor> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Entertainer109616922> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/AlternateHistoryWriters> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Actor109765278> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/AmericanConscientiousObjectors> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/AmericanHistoricalNovelists> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/ConscientiousObjector109957013> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Dissenter110018021> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Novelist110363573> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Performer110415638> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Writer110794014> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/CausalAgent100007347> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/LivingThing100004258> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Organism100004475> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/JewishComedians> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/JewishPacifists> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Pacifist110390199> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/PhysicalEntity100001930> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/YagoLegalActorGeo> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/PeopleFromQueens> |
| <http://sw.opencyc.org/2008/06/10/concept/en/RichardDreyfuss> | <http://sw.opencyc.org/2008/06/10/concept/en/ActorInMovies> |
| <http://sw.opencyc.org/2008/06/10/concept/en/RichardDreyfuss> | <http://sw.opencyc.org/2008/06/10/concept/en/MaleHuman> |
| <http://sw.opencyc.org/2008/06/10/concept/Mx4rwOREVpwpEbGdrcN5Y29ycA> | <http://sw.opencyc.org/2008/06/10/concept/Mx4rvVjWoZwpEbGdrcN5Y29ycA> |
| <http://sw.opencyc.org/2008/06/10/concept/Mx4rwOREVpwpEbGdrcN5Y29ycA> | <http://sw.opencyc.org/2008/06/10/concept/Mx4rwMRyTJwpEbGdrcN5Y29ycA> |
-------------------------------------------------------------------------------------------------------------------------------------------------
嗨伊恩和约书亚感谢你的答案。我提到的演员名字实际上是维基百科的资源名称。该计划花了很长时间才运行,但在30分钟内取得了部分结果。 –
@harrypotter您可以显示更多的数据或输入列表吗?除非它特别大,否则对于一个相当简单的查询来说,这似乎很长时间。另外,如果你得到DBpedia IRI列表,你可以考虑使用'values'块来运行一个大的查询,而不是每个actor的一个查询,你会遇到速度限制问题。 –