我有一个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
如何做到这一点?
答案 0 :(得分: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