【问题标题】:Writing SPARQL queries with Jena to query for IRIs like: http://pt.dbpedia.org/使用 Jena 编写 SPARQL 查询来查询 IRI,例如:http://pt.dbpedia.org/
【发布时间】:2013-12-05 18:01:03
【问题描述】:

我正在使用 Jena 编写 SPARQL 查询以从作为方法参数接收的 URI 中获取 rdfs:label 属性。该方法只接收如下 URI:http://pt.dbpedia.org/.. 它应该返回 rdfs:label,但它不会返回任何东西。我检查了一下,它没有输入应该迭代结果的while block。我什至用 URI 做了一个测试:<http://pt.dbpedia.org/resource/Brasil>,但是没有用。

可能是什么问题?

 public String getLabel(String uri, String label) {
              Model model = ModelFactory.createDefaultModel().read( uri );
              RDFNode node;

 String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
              "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
                  "PREFIX owl: <http://www.w3.org/2002/07/owl#>" +
              "SELECT distinct ?label WHERE { " +
             "?resource owl:sameAs <" + uri + "> ;" +
                  "rdfs:label ?label ." +
                  "filter( langMatches(lang(?label),'pt'))  }";

             Query query = QueryFactory.create(queryString);

                QueryExecution qe = QueryExecutionFactory.create(query, model);
                ResultSet r =  qe.execSelect();

                while( r.hasNext() ) {
                       QuerySolution querySolution = r.next();
                       node = querySolution.get("label");
                       label = node.toString();
                    }
                return label;
      }

SPARQL 查询是这样的:

SELECT distinct ?label WHERE { 
  ?brasil owl:sameAs <http://pt.dbpedia.org/resource/Brasil> ;
          rdfs:label ?label .
  filter( langMatches(lang(?label),"pt") )
}

谢谢!

【问题讨论】:

  • 我不知道是不是你遇到的问题,(看起来不像),而是使用字符串连接来进行查询,你真的应该考虑一个参数化的 sparql 字符串。这个答案(stackoverflow.com/a/16739846/1281433)有一个例子。

标签: rdf sparql jena semantic-web dbpedia


【解决方案1】:

我知道这是您之前的问题Should queries with URIs like http://pt.dbpedia.org/resource/.. be different from the ones with URIs like http://dbpedia.org/resource/..? 的延续。如果您收到查询:

SELECT distinct ?label WHERE { 
  ?brasil owl:sameAs <http://pt.dbpedia.org/resource/Brasil> ;
          rdfs:label ?label .
  filter( langMatches(lang(?label),"pt") )
}

那么你的uri 一定是http://pt.dbpedia.org/resource/Brasil,所以你会一直(试图)用

检索数据
Model model = ModelFactory.createDefaultModel().read( uri );

然后您尝试对已下载的本地数据运行 SPARQL 查询。正如我在上一个(链接的)问题中提到的,我提供的查询旨在跨 SPARQL 端点运行;它们不是基于下载数据并在本地查询。

尝试像这样在本地下载数据是行不通的,如下程序及其输出所示:

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class BrasilExample {
    public static void main(String[] args) {
        final Model model = ModelFactory.createDefaultModel().read( "http://pt.dbpedia.org/resource/Brasil" );
        model.write( System.out );
    }
}
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" > 
</rdf:RDF>

如果您想下载一点数据并对其进行查询,请注意

并且后一页在底部有下载数据的链接,例如,

如果您要下载该文件,那么您的查询可能会起作用(当然uri 将不再相同)。

不过,您在我之前的回答中使用的查询是为主要的 DBpedia 端点设计的,而不是为葡萄牙语端点设计的。您可以通过转到http://dbpedia.org/resource/Brazil 并按照上述相同的重定向和下载链接从主 DBpedia 下载巴西的数据,但更好的选择是实际对主 DBpedia 端点运行查询,@ 987654326@,如下代码及其结果所示。

import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;

public class BrasilExample {
    public static void main(String[] args) {

        final String QUERY = 
                "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
                "PREFIX owl: <http://www.w3.org/2002/07/owl#>\n" +
                "\n" +
                "SELECT distinct ?label WHERE {\n" +
                "  ?brasil owl:sameAs <http://pt.dbpedia.org/resource/Brasil> ;\n" +
                "          rdfs:label ?label .\n" +
                "  filter( langMatches(lang(?label),\"pt\") )\n" +
                "}";

        final String ENDPOINT = "http://dbpedia.org/sparql";
        final ResultSet rs = QueryExecutionFactory.sparqlService( ENDPOINT, QUERY ).execSelect();
        ResultSetFormatter.out( rs );
    }
}
---------------
| label       |
===============
| "Brasil"@pt |
| "Brazil"@pt |
---------------

【讨论】:

  • 谢谢!我总是会得到一个以上的结果吗?因为如果我在浏览器上打开 pt.dbpedia.org/resource/Brasil>,我可以看到“rdfs:label”的值只有“Brasil”。为什么我在这里也得到结果“巴西”?
  • @Luciane 您将始终获得与 RDF 存储匹配的结果一样多的结果。显然,PT DBpedia 有一个关于 PT DBpedia 的巴西的结果。主要的 DBpedia 有两个结果。不同的数据库包含不同的数据。我想这就是世界的方式。
  • 好的。但是,如果我只想从 PT Dbpedia's Brasil 获得结果怎么办?
  • @Luciane 使用上一个问题 (SELECT ?label WHERE { &lt;http://pt.dbpedia.org/resource/Brasil&gt; rdfs:label ?label. }) 中的 PT DBpedia 查询查询 PT DBpedia (pt.dbpedia.org/sparql)。这就是为什么在那个答案中我指出了一些查询以及在哪些端点上运行它们以及它们产生的结果。您只需要在正确的端点上使用正确的查询。 一般来说:如果你想要PT DBpedia,使用pt.dbpedia.org/sparql;如果您想要 Main DBpedia,请使用 dbpedia.org/sparql,由于它们有不同的数据,您可能需要不同的查询。
猜你喜欢
  • 1970-01-01
  • 2012-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多