针对linkedmdb的SPARQL查询仅返回空值

时间:2013-08-27 10:53:27

标签: rdf sparql jena linkedmdb

我正在尝试使用Java代码来试验Jena。在这个应用程序中,我只得到20个空值。

public class Movie_database {
    public static void main(String args[]) {
        Model model = ModelFactory.createDefaultModel();
        String sparqlQueryString = "PREFIX mdb: <http://data.linkedmdb.org/resource/movie/>"
                + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
                + "select ?director "
                + "where " 
                + "{ ?director     mdb:director_name ?value ."
                + "?director rdfs:label ?label .}  "
                + "LIMIT 20";
        Query query = QueryFactory.create(sparqlQueryString);
        QueryExecution qexec = QueryExecutionFactory.sparqlService("http://linkedmdb.org/sparql", query);
        ResultSet results = qexec.execSelect();
        while (results.hasNext()) {
            QuerySolution soln = results.nextSolution();
            Literal value = soln.getLiteral("value");
            System.out.println(value);
        }
    }
}

毕竟,它打印出20个空值,因此它找到了一些东西,但为什么?value的值为null?

1 个答案:

答案 0 :(得分:3)

您构建的查询类似于以下内容,但以下内容可以使用Jena的命令行工具以独立方式运行,因为我在查询中使用了service

PREFIX mdb: <http://data.linkedmdb.org/resource/movie/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

select ?director where {
  service <http://linkedmdb.org/sparql> {
    ?director mdb:director_name ?value ;
              rdfs:label ?label .
  }
}
limit 20

当我使用命令行工具运行时,这些是结果(data.n3是一个空文件,因为arq需要--data参数,但是service确保数据来自远程端点):

$ arq --data data.n3 --query query.sparql
----------------------------------------------------
| director                                         |
====================================================
| <http://data.linkedmdb.org/resource/director/1>  |
| <http://data.linkedmdb.org/resource/director/2>  |
| <http://data.linkedmdb.org/resource/director/3>  |
| <http://data.linkedmdb.org/resource/director/4>  |
| <http://data.linkedmdb.org/resource/director/5>  |
| <http://data.linkedmdb.org/resource/director/6>  |
| <http://data.linkedmdb.org/resource/director/7>  |
| <http://data.linkedmdb.org/resource/director/8>  |
| <http://data.linkedmdb.org/resource/director/9>  |
| <http://data.linkedmdb.org/resource/director/10> |
| <http://data.linkedmdb.org/resource/director/11> |
| <http://data.linkedmdb.org/resource/director/12> |
| <http://data.linkedmdb.org/resource/director/13> |
| <http://data.linkedmdb.org/resource/director/14> |
| <http://data.linkedmdb.org/resource/director/15> |
| <http://data.linkedmdb.org/resource/director/16> |
| <http://data.linkedmdb.org/resource/director/17> |
| <http://data.linkedmdb.org/resource/director/18> |
| <http://data.linkedmdb.org/resource/director/20> |
| <http://data.linkedmdb.org/resource/director/21> |
----------------------------------------------------

请注意,您预测了?director,但未预测?value?label。因此,当您执行以下操作时,没有绑定?value变量:

    Literal value = soln.getLiteral("value");
    System.out.println(value);

您获得null结果。您可以使用QuerySolution.contains捕获一些这类错误。您需要做的就是将查询更新为

select ?director ?value where { # you probably want ?label in here, too

甚至

select * where { # this would get all three variables

就足够了。一旦选择了所有三个变量,就会得到如下结果:

-------------------------------------------------------------------------------------------------------------
| director                                         | value                | label                           |
=============================================================================================================
| <http://data.linkedmdb.org/resource/director/1>  | "Chris Morris"       | "Chris Morris (Director)"       |
| <http://data.linkedmdb.org/resource/director/2>  | "Cecil B. DeMille"   | "Cecil B. DeMille (Director)"   |
| <http://data.linkedmdb.org/resource/director/3>  | "Michael Mann"       | "Michael Mann (Director)"       |
| <http://data.linkedmdb.org/resource/director/4>  | "Mustafa Altıoklar" | "Mustafa Altıoklar (Director)" |
| <http://data.linkedmdb.org/resource/director/5>  | "Robert J. Flaherty" | "Robert J. Flaherty (Director)" |
| <http://data.linkedmdb.org/resource/director/6>  | "Rex Ingram"         | "Rex Ingram (Director)"         |
| <http://data.linkedmdb.org/resource/director/7>  | "William Dickson"    | "William Dickson (Director)"    |
| <http://data.linkedmdb.org/resource/director/8>  | "Andrzej Å»uÅawski" | "Andrzej Å»uÅawski (Director)" |
| <http://data.linkedmdb.org/resource/director/9>  | "Max Reinhardt"      | "Max Reinhardt (Director)"      |
| <http://data.linkedmdb.org/resource/director/10> | "Willis O'Brien"     | "Willis O'Brien (Director)"     |
| <http://data.linkedmdb.org/resource/director/11> | "Frank Miller"       | "Frank Miller (Director)"       |
| <http://data.linkedmdb.org/resource/director/12> | "David Byrne"        | "David Byrne (Director)"        |
| <http://data.linkedmdb.org/resource/director/13> | "John G. Avildsen"   | "John G. Avildsen (Director)"   |
| <http://data.linkedmdb.org/resource/director/14> | "Leslie Howard"      | "Leslie Howard (Director)"      |
| <http://data.linkedmdb.org/resource/director/15> | "John Hughes"        | "John Hughes (Director)"        |
| <http://data.linkedmdb.org/resource/director/16> | "Karl Koch"          | "Karl Koch (Director)"          |
| <http://data.linkedmdb.org/resource/director/17> | "George Miller"      | "George Miller (Director)"      |
| <http://data.linkedmdb.org/resource/director/18> | "Michael Powell"     | "Michael Powell (Director)"     |
| <http://data.linkedmdb.org/resource/director/20> | "Joseph De Grasse"   | "Joseph De Grasse (Director)"   |
| <http://data.linkedmdb.org/resource/director/21> | "Yamanaka Sadao"     | "Yamanaka Sadao (Director)"     |
-------------------------------------------------------------------------------------------------------------