使用耶拿(Jena)编写SPARQL查询来查询IRI,例如:http://pt.dbpedia.org/ [英] Writing SPARQL queries with Jena to query for IRIs like: http://pt.dbpedia.org/

查看:79
本文介绍了使用耶拿(Jena)编写SPARQL查询来查询IRI,例如:http://pt.dbpedia.org/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Jena to write a SPARQL query to get the rdfs:label property from a URI received as a method parameter. That method only receives URIs like: http://pt.dbpedia.org/.. It should return me the rdfs:label, but it doesn't return me anything. I checked and it doesn't enter the while block supposed to iterate the results. I even made a test with the URI: <http://pt.dbpedia.org/resource/Brasil> , but it didn't work.

可能是什么问题?

 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") )
}

谢谢!

推荐答案

我了解到,这是您先前的问题的延续,应该带有URI的查询(例如http://pt.dbpedia.org/resource/..)与带有URI的查询(例如http://dbpedia.org/resource/..?)不同.如果您要查询:

I understand that this is a continuation of your earlier question, Should queries with URIs like http://pt.dbpedia.org/resource/.. be different from the ones with URIs like http://dbpedia.org/resource/..?. If you're getting the query:

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,因此您将一直(尝试)使用

then your uri must have been http://pt.dbpedia.org/resource/Brasil, so you would have been (trying) to retrieve data with

Model model = ModelFactory.createDefaultModel().read( uri );

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

and then you're trying to run a SPARQL query against the local data that you've downloaded. As I mentioned in the previous (linked) question, the queries that I provided were meant to be run across the SPARQL endpoints; they weren't based on downloading the data and querying locally.

尝试以这种方式在本地下载数据无效,因为以下程序及其输出显示:

Trying to download the data locally like this doesn't work, as the following program and its output show:

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>

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

If you want to download a little bit of data and query against it, then note that

  • http://pt.dbpedia.org/resource/Brasil redirects to
  • http://pt.dbpedia.org/page/Brasil

,后者的页面底部有链接以下载数据,例如

and that latter page has links at the bottom to download the data, e.g.,

如果要下载该文件,则查询可能会起作用(但uri当然不再相同).

If you were to download that file, then your query might work (but of course the uri would no longer be the same).

不过,您从我先前的答案中使用的查询是针对主要DBpedia端点而不是葡萄牙语端点而设计的.您可能可以通过访问 http://dbpedia.org/resource从主DBpedia下载Brasil的数据./Brazil 并按照上述相同的重定向和下载链接进行操作,但更好的选择是针对主要DBpedia端点 http://dbpedia.org/sparql ,如以下代码及其结果所示.

The query you're using from my earlier answer, though, was designed for the main DBpedia endpoint, not the Portuguese endpoint. You might be able to download the data for Brasil from the main DBpedia by going to http://dbpedia.org/resource/Brazil and following the same redirect and download link as described above, but a better choice would be to actually run the query against the main DBpedia endpoint, http://dbpedia.org/sparql, as shown in the following code and its results.

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

这篇关于使用耶拿(Jena)编写SPARQL查询来查询IRI,例如:http://pt.dbpedia.org/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆