- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 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 Schema 1.1 2.4 rdfs:Datatype rdfs:Datatype is the class of datatypes. All instances of rdfs:D
我遇到了books ontology ,在 owl 的底部文档,我们有: A reservation has been made by a person for a boo
在 RDF/XML 文件中具有属性的订单是否重要?换句话说,以下两个相同吗? 1 2 2 1 最佳答案 是的,RDF 在数学上被定义为一组三元组,因此这些三元组在您使用
我刚开始学习 RDF,我的问题是: 空白节点的目的是什么? 例子: ex:John foaf:knows _:p1 _:p1 foaf:birthDate 04-21 这
让我们看看这个例子:http://dbpedia.org/page/Berlin “owl:sameAs”和“is owl:sameAs of”有什么区别 最佳答案 owl:sameAs 是一个对称属
许多数据集都有变化的历史。将历史数据作为关联数据提供可能是一个挑战。我正在考虑的一般情况是,数据集包含有关具有可以随时间变化的属性的事物的数据。一个例子可能是温莎城堡的历史:它过去有很多配置,但它仍然
我正在使用 MarkLogic 8.0-6.3 使用 sem:rdf-load 从 RDF 文件生成三元组时很少有三元组没有被创建。 我已经粘贴了 RDF 文件内容、生成的三元组和我用来加载文件的查询
在这里完成 RDF 新手,我正在努力理解如何创建合理且可重用的谓词。我问了我们公司的几位专家,但他们的谓词(被迫)通过复制对象是唯一的(例如 woman,has_a_bag,purse 与 woman
谁能给我一个 RDF 中具体化的简单例子?我想看看我是否理解正确。 例如,我提出以下案例 Tolkien -> wrote -> Lord of the rings /|\
我想对如下所示的合作伙伴关系进行建模,我以标记属性图的格式表示。 我想使用RDF语言来表达上面的图,特别是我想了解是否可以表达“loves”边的标签(这是一篇文章/信件的URI)。 我是RDF新手,我
我在构建 Project Gutenberg 目录的 SPARQL 查询时遇到困难(可在页面底部的 Gutenberg Feeds 获取)。我知道我对 SparQL/RDF 等如何工作缺乏了解。实际上
我正在用 RDF 创建一个 Material 集合。我遇到了两种处理度量单位的方法: 通过将描述性名称链接到 RDF 属性: prop:density prop:hasUnits "kg/m
RDF:Property 是否可以包含其他属性以及 rdf:range 和 rdf:domain。例如,您是否可以拥有以下内容: This is a member of
我对 RDF 和 RDF Schema (RDFS) 有点困惑。我读过一些文档,其中提到 rdfs 是为 RDF 定义词汇表。 根据这个定义,我可以说 RDFS 是像 OWL 或 Dublin Cor
rdf:resource、rdf:about 和 rdf:ID 之间在概念上有什么区别。我做了一些调查,但我还不清楚它们之间的区别。例如,第一次声明资源时是否使用rdf:ID,rdf:resource
这是rdf代码: 42 我需要得到号码 "42" .我试过这个: PREFIX soton: PREFIX skos: ?location skos:notation rdf:
关于语义网的初学者问题。 我有一个颜色知识库,其中包括相似的颜色、颜色修饰符(暗、亮、~ish 等)、颜色关系(更深、更亮)、颜色同义词等。我想弄清楚 RDF/OWL 是否是一个操作(主要是查询)这个
有什么办法可以加快 rdf 文件加载到 Sesame 中的速度吗?我有 N-triple 格式的文件,大小从几 MB 到几 GB。我已经尝试了 Sesame Cook Book 中的前三种方法,但无济
例如,这句话: "I give John a book." 使其成为一组三元组: I give John. John hasIndirect book. book count 1. 或者,它可以是:
在 RDF 图的 Turtle 序列化中,我有很多这样的三元组(很多个体,都有一个共同的类型值): :A a :b . :B a :b . :C a :b . :D a :b . # … :Z a :
我是一名优秀的程序员,十分优秀!