3

我有一个 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

如何才能做到这一点?

4

1 回答 1

3

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

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

@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(...) as ?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"                                      |
---------------------------------------------------------------------------------------------------------------------------

如果您使用这样的查询并遍历结果并按原样打印它们,您将获得所需的输出。如果您确实想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
于 2013-08-28T13:28:56.263 回答