RDF 在一行中列出主题及其对象

标签 rdf sparql jena sesame

我有一个 RDF 文件,我需要从中提取一些信息并将其写入文件。我了解它的基本工作原理,但我坚持这样做:

String queryString = "select ?person ?children where { ?person ?hasChildren ?children}";
TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
TupleQueryResult result = tupleQuery.evaluate();     
while (result.hasNext()) {
    BindingSet bindingSet = result.next();
    Value p1 = bindingSet.getValue("person");
    Value p2 = bindingSet.getValue("child");
    println(p1 + " has children " + p2 +"");
}
result.close();

我得到的输出是这样的:

http://example.org/people/person1 has children http://example.org/people/child1
http://example.org/people/person1 has children http://example.org/people/child2

我看不出如何以这种格式列出所有人及其对象:

person1 has children child1 and child2

如何做到这一点?

最佳答案

您可能会发现这个描述 SPARQL 的 group_concat 的答案很有用:

在 SPARQL 中,当您有查询解决方案的结果集时,您可以对一个或多个变量进行分组,合并具有这些共同变量的解决方案。例如,考虑数据

@prefix : <http://example.org/people/>.

:person1 :hasChild :child1, :child2, :child3 .
:person2 :hasChild :child4, :child5 .
:person3 :hasChild :child6 .

如果你在上面运行下面的查询

prefix : <http://example.org/people/>

select ?person ?child where { 
  ?person :hasChild ?child .
}

你得到这样的结果:

$ arq --data data.n3 --query query.sparql
----------------------
| person   | child   |
======================
| :person3 | :child6 |
| :person2 | :child5 |
| :person2 | :child4 |
| :person1 | :child3 |
| :person1 | :child2 |
| :person1 | :child1 |
----------------------

迭代问题中的结果将产生您当前获得的输出类型。我们想要做的是实际获得如下结果:

$ arq --data data.n3 --query query.sparql
----------------------------------------
| person   | child                     |
========================================
| :person3 | :child6                   |
| :person2 | :child4, :child5          |
| :person1 | :child1, :child2, :child3 |
----------------------------------------

而这正是 group_by 让我们做的。像这样的查询:

prefix : <http://example.org/people/>

select ?person (group_concat(?child;separator=' and ') as ?children) where { 
  ?person :hasChild ?child .
}
group by ?person

产生(注意结果中的变量是 ?children,而不是 ?child,因为我们使用 group_concat(...) 作为 ? children 创建新变量 ?children):

$ arq --data data.n3 --query query.sparql
---------------------------------------------------------------------------------------------------------------------------
| person   | children                                                                                                     |
===========================================================================================================================
| :person3 | "http://example.org/people/child6"                                                                           |
| :person1 | "http://example.org/people/child3 and http://example.org/people/child2 and http://example.org/people/child1" |
| :person2 | "http://example.org/people/child5 and http://example.org/people/child4"                                      |
---------------------------------------------------------------------------------------------------------------------------

如果您使用这样的查询并遍历结果,按您的方式打印它们,您将获得您想要的输出。如果您确实想从人员和 child 中去除领先的 http://example.org/people/,您将需要更多的字符串处理。例如,使用 STRAFTER要删除 http://example.org/people/ 前缀,您可以使用如下查询:

prefix : <http://example.org/people/>

select
 (strafter(str(?personX),"http://example.org/people/") as ?person)
 (group_concat(strafter(str(?child),"http://example.org/people/");separator=' and ') as ?children)
where { 
  ?personX :hasChild ?child .
}
group by ?personX

得到如下结果:

$ arq --data data.n3 --query query.sparql
----------------------------------------------
| person    | children                       |
==============================================
| "person3" | "child6"                       |
| "person2" | "child5 and child4"            |
| "person1" | "child3 and child2 and child1" |
----------------------------------------------

当你进行打印时,它会给你这样的结果

person3 has children child6
person2 has children child5 and child4
person1 has children child3 and child2 and child1

关于RDF 在一行中列出主题及其对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18485461/

相关文章:

rdf - 当对象是 bNode 时,rdf 具体化如何工作?

python - SPARQLWrapper HTTP 错误 401 : Unauthorized

java - 将网站的所有数据存储在 Apache Jena 上作为 RDF?

rdf - 如何将属性表示为 RDF 中的属性

rdf - 为属性分配多个值

SPARQL 查询以获取定义了命名空间前缀的所有类标签

Sparql - 将限制条件应用于谓词

rdf - 无法使用 SPARQL 对层次结构进行前缀

java - 从 RDF 文件生成 .DOT 文件

sparql - 如何获取所有不具有给定属性的实体?