2014-01-24 60 views
1
while((reader=br.readLine())!=null) 
{ 
    System.out.println(reader); 
    type=""; 
    String queryNew="select ?y ?z where {"+reader+" <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?z . }"; 
    Query query=QueryFactory.create(queryNew); 

    ARQ.getContext().setTrue(ARQ.useSAX); 
    QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query); 

    ResultSet results = qexec.execSelect(); 

    while(results.hasNext()) 
    { 
     QuerySolution soln=results.nextSolution(); 
     type=type+" "+soln.get("?z"); 
     System.out.println(soln.get("?y")+" "+soln.get("?z")); 
    } 

    bw.write(reader+" "+type+"\n"); 
} 

从文件中读取演员姓名,然后尝试获取每个演员的所有rdf:type链接。当结果为第一个参与者打印时,其余的执行会被卡住。 '读者'不过是一个演员的名字,就是dbpedia资源。任何人都可以告诉我代码中可能出现了什么问题?关于循环中DBPedia SPARQL终点的SPARQL查询

回答

4

你不显示您的数据,但假设该文件包含演员只是名字,就像你说的,那么你实际上问的查询是这样的:

select ?y ?z where { 
    "Matt Damon" rdf:type ?z 
} 

这是永远不会返回任何结果,因为RDF三元组的主题始终是RDF资源,而不是字符串文字。

您应该将查询更改为:

prefix dbpprop: <http://dbpedia.org/property/> 
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 

select ?actor ?type where { 
    ?actor dbpprop:name "Matt Damon"@en ; 
      rdf:type ?type 
} 
换句话说

“什么是RDF:类型,它们的名称(英文)是马特·达蒙的资源吗?”

+0

嗨伊恩和约书亚感谢你的答案。我提到的演员名字实际上是维基百科的资源名称。该计划花了很长时间才运行,但在30分钟内取得了部分结果。 –

+0

@harrypotter您可以显示更多的数据或输入列表吗?除非它特别大,否则对于一个相当简单的查询来说,这似乎很长时间。另外,如果你得到DBpedia IRI列表,你可以考虑使用'values'块来运行一个大的查询,而不是每个actor的一个查询,你会遇到速度限制问题。 –

1

正如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> | 
-------------------------------------------------------------------------------------------------------------------------------------------------