简体   繁体   English

从ResultSet中的资源获取URI

[英]Get URI from resource in ResultSet

I am trying to get the URI from the a resource in Java, but it is always null . 我试图从Java中的资源获取URI,但它始终为null Right now, I am trying this: 现在,我正在尝试这个:

for ( ; rs.hasNext() ; ) {
  QuerySolution qs  = rs.next();
  System.out.println( qs.getLiteral("label"));
  System.out.println( qs.getResource("label"));
  …

The literal is returned just fine, I even tried the resource "?film", but still resource is null. 文字返回很好,我甚至尝试了资源“?电影”,但资源仍然是空的。 Here is my SPARQL query: 这是我的SPARQL查询:

PREFIX mdb: <http://data.linkedmdb.org/resource/movie/film>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
select ?label ?film where {
  ?film mdb:id ?uri .
  ?film rdfs:label ?label . 
  filter regex(?label,  + queryVar +
}

queryVar is just the user input, for example "Batman". queryVar只是用户输入,例如“Batman”。

Edit:I had an typo in my query: this was my query: 编辑:我的查询中有一个拼写错误:这是我的查询:

String sparqlQuery = "PREFIX mdb: http://data.linkedmdb.org/resource/movie/film " + "PREFIX rdfs: http://www.w3.org/2000/01/rdf-schema# " + "select ?label ?film" + "where " + "{ ?film mdb:id ?uri ." String sparqlQuery =“PREFIX mdb: http ://data.linkedmdb.org/resource/movie/film”+“PREFIX rdfs: http//www.w3.org/2000/01/rdf-schema# ”+“select ?label?film“+”where“+”{?film mdb:id?uri。“ + "?film rdfs:label ?label . " + " }" + ""; +“?film rdfs:label?label。”+“}”+“”;

See closely at ?film and where, they become filmwhere, thats the problem. 仔细看看?电影和它们成为电影的地方,这就是问题所在。

Thanks to Joshua I figured it out. 感谢约书亚我明白了。

The documentation for QuerySolution#getResource says QuerySolution#getResource文档

Resource getResource(String varName)

Return the value of the named variable in this binding, casting to a Resource. 返回此绑定中指定变量的值,并转换为Resource。 A return of null indicates that the variable is not present in this solution. 返回null表示该解决方案中不存在该变量。 An exception indicates it was present but not a resource. 异常表示它存在但不是资源。

If you're getting null , then some value isn't present in the query solution. 如果您获得null ,则查询解决方案中不存在某些值。 However, without seeing your actual query, it's impossible to tell whether you're getting empty results for some reason (eg, the queryVar isn't what you think it is), or not. 但是,如果没有看到您的实际查询,则无法判断您是否因某种原因(例如, queryVar不是您认为的那样)得到空结果。 If queryVar is just a String without surrounding quotes, you'd end up with a query like 如果queryVar只是一个没有引号的String,你最终会得到一个类似的查询

filter regex(?label,Batman)

instead of 代替

filter regex(?label,"Batman")

At any rate, I modified your query to be one that we can run with Jena's command line tools: 无论如何,我将您的查询修改为我们可以使用Jena的命令行工具运行的查询:

PREFIX mdb: <http://data.linkedmdb.org/resource/movie/film>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
select ?label ?film
where {
  service <http://data.linkedmdb.org/sparql> {
    ?film mdb:id ?uri .
    ?film rdfs:label ?label . 
    filter regex(?label, "Batman")
  }
}

When I run this, I get results like: 当我运行这个时,我得到的结果如下:

$ arq --query query.sparql --data data.n3 
-----------------------------------------------------------------------------------------
| label                                | film                                           |
=========================================================================================
| "Batman"                             | <http://data.linkedmdb.org/resource/film/2>    |
| "Batman"                             | <http://data.linkedmdb.org/resource/film/3>    |
| "Batman & Robin"                     | <http://data.linkedmdb.org/resource/film/4>    |
| "Batman: Mask of the Phantasm"       | <http://data.linkedmdb.org/resource/film/737>  |
| "Batman: Mystery of the Batwoman"    | <http://data.linkedmdb.org/resource/film/974>  |
| "Batman Beyond: Return of the Joker" | <http://data.linkedmdb.org/resource/film/1802> |
| "Batman & Mr. Freeze: SubZero"       | <http://data.linkedmdb.org/resource/film/2124> |
-----------------------------------------------------------------------------------------

in which label and film are always bound. 其中labelfilm总是受到约束。 All of the labels are literals, so you should be able to do 所有标签都是文字,所以你应该能够做到

qs.getLiteral("label")

and get a literal. 得到一个文字。 It sounds more like you want the URI of the film variable, which you would do with 这听起来更像是你想要的film变量的URI,你会做

qs.getResource("film").getURI()

You could, of course, use toString() afterward if you want the URI string. 当然,如果你想要URI字符串,你可以使用toString()

As an aside, rather than doing 抛开而不是做

"filter(?label, " + queryVar + "…"

you might want to consider using a ParameterizedSparqlString to safely substitute the queryVar in. Otherwise, what happens with queryVar is something like 你可能想考虑使用ParameterizedSparqlString来安全地替换queryVar。否则, queryVar会发生什么

"\"Batman\") UNION { ?film hasPassword ?label }"

and it gets substituted in? 它被取代了吗? You suddenly leak a bunch of information. 你突然泄漏了一堆信息。 Even if nothing sensitive gets out, you could still end up causing a big load on the SPARQL endpoint, effectively launching a denial-of-service attack. 即使没有任何敏感信息泄露,您仍然可能最终导致SPARQL端点出现大量负载,从而有效地启动拒绝服务攻击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM