英文:
SPARQL: How to retrieve a 1:N relationship?
问题
在学习了SPARQL的基础知识之后,我仍然在尝试理解1:N关系的方式。我该如何检索一个对象及其所有关系作为单个记录?
例如,我有一个与两个主题相关联的项目。我尝试使用以下查询来检索它:
SELECT ?projName (GROUP_CONCAT(?topic; SEPARATOR="|") as ?topics) (GROUP_CONCAT(?topicName; SEPARATOR="|") as ?topicNames) {
?proj hasName ?projName .
?proj hasTopic ?topic .
?topic hasName ?topicName .
FILTER (?proj = <$uri>) .
}
但我得到的结果是:
result: [
[
projName: "My Project"
topics: "TOPIC1_URI|TOPIC2_URI"
topicNames: "First Topic|Second Topic"
]
]
但我想要得到的是:
result: [
projName: "My Project"
topics: [
[
topic: "TOPIC1_URI"
topicName: "First Topic"
],
[
topic: "TOPIC2_URI"
topicName: "Second Topic"
]
]
]
我该如何实现这一点?我不知道我漏掉了什么,但我不知道如何在SPARQL中实现这一点。
非常感谢!
英文:
After learning the basics of SPARQL I'm still trying to make sense of 1:N relationships. How can I retrieve an object with all its relationships as a single record?
For example, I have a Project linked to two Topics. And I try to retrieve it with:
SELECT ?projName ?topic ?topicName {
?proj hasName ?projName .
?proj hasTopic ?topic .
?topic hasName ?topicName .
FILTER (?proj = <$uri>) .
}
But what I get is:
result: [
[
projName: "My Project"
topic: "TOPIC1_URI"
topicName: "First Topic"
],
[
projName: "My Project"
topic: "TOPIC2_URI"
topicName: "Second Topic"
]
]
But I would want to get it as:
result: [
projName: "My Project"
topics: [
[
topic: "TOPIC1_URI"
topicName: "First Topic"
],
[
topic: "TOPIC2_URI"
topicName: "Second Topic"
]
]
]
How could I achieve this? I don't know what I'm missing but I don't see how to do this with SPARQL.
Thanks a lot in advance
答案1
得分: 2
如您可以在这里阅读到的,SELECT
SPARQL查询的结果是一组绑定,即将值分配给查询的自由变量。您可以将这些绑定视为矩阵或表,就像SQL查询一样,其属性是变量的名称。但如果您只有一个矩阵来表示这些变量绑定,您不能随意安排这些变量绑定。
您可以使用GROUP_CONCAT
函数将所有关于主题的数据收集到一个单独的变量中。例如:
SELECT
?projName
(GROUP_CONCAT(?topicData; separator=", ") AS ?topics)
WHERE {
?proj hasName ?projName .
?proj hasTopic ?topic .
?topic hasName ?topicName .
FILTER (?proj = <$uri>) .
BIND(CONCAT(?topic, ": ", ?topicName) AS ?topicData)
}
GROUP BY ?projName
但请记住,分配给?topics
的值将是字符串,而不是 JSON数组:
result: [
projName: "My Project"
topics: "TOPIC1_URI: First Topic, TOPIC2_URI: Second Topic"
]
显然,您可以选择其他分隔符而不是": "
和", "
。
英文:
As you can read here, the result of a SELECT
SPARQL query is a set of bindings, i.e., assignments of values to the free variables of the query. You can think of such bindings as a matrix or a table, like for SQL queries, whose attributes are the variables' names. But you can't arrange such variables bindings as you wish if you have just a matrix for representing them.
What you can do is to collect all data about topics in a single variable using the GROUP_CONCAT
function. For example:
SELECT
?projName
(GROUP_CONCAT(?topicData; separator=", ") AS ?topics)
WHERE {
?proj hasName ?projName .
?proj hasTopic ?topic .
?topic hasName ?topicName .
FILTER (?proj = <$uri>) .
BIND(CONCAT(?topic, ": ", ?topicName) AS ?topicData)
}
GROUP BY ?projName
But remember that the values assigned to ?topics
will be strings, not JSON arrays:
result: [
projName: "My Project"
topics: "TOPIC1_URI: First Topic, TOPIC2_URI: Second Topic"
]
Clearly you can choose other separators than ": "
and ", "
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论