关于循环中DBPedia SPARQL端点的SPARQL查询

时间:2014-01-24 09:31:33

标签: sparql jena semantic-web

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链接。当结果打印出第一个actor时,执行的其余部分会被卡住。 '读者'只不过是一个演员的名字,它是dbpedia资源。有人可以告诉我代码中可能出错的地方吗?

2 个答案:

答案 0 :(得分:4)

您没有显示您的数据,但假设该文件只包含了actor的名称,正如您所说,那么您实际询问的查询是这样的:

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:名称(英文)是Matt Damon的资源的类型?”

答案 1 :(得分:1)

正如Ian Dickinson在his answer中指出的那样,你没有向我们展示数据,也没有向我们展示reader的确切价值,所以很难确切地说出出了什么问题。听起来你需要根据

的方式调整查询
?actor rdfs:label ?label

其中?label是您感兴趣的actor的名称。如果您像在

中那样使用字符串连接
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提供{{3}这将负责正确的逃避。另请注意,RDF区分普通文字,例如"Richard Dreyfuss"和语言标记字符串,例如"Richard Dreyfuss"@en。这意味着您可能希望在查询中包含语言标记。最后,请注意DBpedia中可能存在多个具有给定标签的内容。 Heres的代码使用参数化的SPARQL字符串,查找标签为"Richard Dreyfuss"@en的资源及其所有RDF类型:

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