SPARQL:如何检索1:N关系?

huangapple go评论59阅读模式
英文:

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 = &lt;$uri&gt;) .
        }

But what I get is:

result: [
  [
    projName: &quot;My Project&quot;
    topic: &quot;TOPIC1_URI&quot;
    topicName: &quot;First Topic&quot;
  ],
  [
    projName: &quot;My Project&quot;
    topic: &quot;TOPIC2_URI&quot;
    topicName: &quot;Second Topic&quot;
  ]
]

But I would want to get it as:

result: [
  projName: &quot;My Project&quot;
  topics: [
    [
      topic: &quot;TOPIC1_URI&quot;
      topicName: &quot;First Topic&quot;
    ],
    [
      topic: &quot;TOPIC2_URI&quot;
      topicName: &quot;Second Topic&quot;
    ]
  ]
]

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=&quot;, &quot;) AS ?topics)
WHERE {
  ?proj hasName ?projName .
  ?proj hasTopic ?topic .
  ?topic hasName ?topicName .
  FILTER (?proj = &lt;$uri&gt;) .
  BIND(CONCAT(?topic, &quot;: &quot;, ?topicName) AS ?topicData)
}
GROUP BY ?projName

But remember that the values assigned to ?topics will be strings, not JSON arrays:

result: [
  projName: &quot;My Project&quot;
  topics: &quot;TOPIC1_URI: First Topic, TOPIC2_URI: Second Topic&quot;
]

Clearly you can choose other separators than &quot;: &quot; and &quot;, &quot;.

huangapple
  • 本文由 发表于 2023年2月6日 20:53:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定