Sparql将整个对象分组到列表中
我有一个餐馆的列表,我想把它们归为两个标识符组成的列表。响应将在JSON中显示:Sparql将整个对象分组到列表中,sparql,Sparql,我有一个餐馆的列表,我想把它们归为两个标识符组成的列表。响应将在JSON中显示: [ { "restaurantChainId": "1", "restaurantTypeId": "001", "restaurantList": [ { "restaurantId"
[
{
"restaurantChainId": "1",
"restaurantTypeId": "001",
"restaurantList": [
{
"restaurantId": "1-1",
"restaurantChainId": "1",
"restaurantTypeId": "001",
"restaurant name": "Pizza Place London"
"restaurant location": "London"
},
{
"restaurantId": "1-2",
"restaurantChainId": "1",
"restaurantTypeId": "001",
"restaurant name": "Pizza Place Birmingham"
"restaurant location": "Birmingham"
}
]
},
{
"restaurantChainId": "1",
"restaurantTypeId": "002",
"restaurantList": [
{
"restaurantId": "1-3",
"restaurantChainId": "1",
"restaurantTypeId": "002",
"restaurant name": "Pizza Place Manchester"
"restaurant location": "Manchester"
}
]
},
{
"restaurantChainId": "2",
"restaurantTypeId": "002",
"restaurantList": [
{
"restaurantId": "2-1",
"restaurantChainId": "2",
"restaurantTypeId": "002",
"restaurant name": "Taco Town London"
"restaurant location": "London"
},
{
"restaurantId": "2-2",
"restaurantChainId": "2",
"restaurantTypeId": "002",
"restaurant name": "Taco Town Newcastle"
"restaurant location": "Newcastle"
}
]
}
]
因此,数组中的对象具有一个列表,其中列表中的每个对象共享restaurantChainId
和restaurantTypeId
我已尝试了以下组\u concat查询:
SELECT ?restaurantChainId (GROUP_CONCAT(?restaurantTypeId; SEPARATOR=", ") AS ?restaurantTypeIdList)
WHERE {
?restaurant <http://example.com/terms#docType> "restaurant" ;
<http://example.com/terms#restaurantTypeId> ?restaurantTypeId;
<http://example.com/terms#institutionFid> ?restaurantChainId .
FILTER (strstarts(str(?restaurantTypeId), '00'))
} GROUP BY ?restaurantChainId
以下是当前的结果:
{
"head" : {
"vars" : [ "restaurantChainId", "restaurantTypeIdList", "restaurant" ]
},
"results" : {
"bindings" : [ {
"restaurantChainId" : {
"type" : "literal",
"value" : "XYZ"
},
"restaurantTypeIdList" : {
"type" : "literal",
"value" : "ABA3"
},
"restaurant" : {
"type" : "literal",
"value" : "http://example.co.uk/restaurant/XYZ-4, http://example.co.uk/restaurant/XYZ-5, http://example.co.uk/restaurant/XYZ-6"
}
}, {
"restaurantChainId" : {
"type" : "literal",
"value" : "ABC"
},
"restaurantTypeIdList" : {
"type" : "literal",
"value" : "ABA1"
},
"restaurant" : {
"type" : "literal",
"value" : "http://example.co.uk/restaurant/ABC-14, http://example.co.uk/restaurant/ABC-15, http://example.co.uk/restaurant/ABC-4"
}
}, {
"restaurantChainId" : {
"type" : "literal",
"value" : "XYZ"
},
"restaurantTypeIdList" : {
"type" : "literal",
"value" : "ABA1"
},
"restaurant" : {
"type" : "literal",
"value" : "http://example.co.uk/restaurant/XYZ-2, http://example.co.uk/restaurant/XYZ-1"
}
}, {
"restaurantChainId" : {
"type" : "literal",
"value" : "XYZ"
},
"restaurantTypeIdList" : {
"type" : "literal",
"value" : "ABA2"
},
"restaurant" : {
"type" : "literal",
"value" : "http://example.co.uk/restaurant/XYZ-3"
}
}, {
"restaurantChainId" : {
"type" : "literal",
"value" : "ABC"
},
"restaurantTypeIdList" : {
"type" : "literal",
"value" : "ABA2"
},
"restaurant" : {
"type" : "literal",
"value" : "http://example.co.uk/restaurant/ABC-9"
}
} ]
}
}
因此,实际上分组是正确的,我有每个餐厅的uri,但我不知道如何获取与它们相关的具体信息。这就是您现在得到的结果吗?或者你想要什么?我的意思是,您是否需要JSON-LD作为响应格式?如果我没有完全错的话,那就需要一个SPARQL contruct查询。对于JSON格式,那么你需要一个支持@AKSW的三重存储/SPARQL引擎。我已经更新了我的答案,以包括一个稍微改进的查询和该查询的输出。好的,这是SPARQL JSON,但缺少什么。我的意思是,它完全包含了您查询的内容。如果您还需要关于餐厅的数据,我强烈建议使用SPARQL
CONSTRUCT
query并使用JSON-LD作为响应类型。我也不认为使用单个SELECT查询是可能的。我的意思是,一方面您需要分组,但另一方面您需要组中每个实体的数据。有时使用(a)多个查询要简单得多,或者更经常地使用(b)在客户端进行数据处理要好得多。这就是您现在得到的结果吗?或者你想要什么?我的意思是,您是否需要JSON-LD作为响应格式?如果我没有完全错的话,那就需要一个SPARQL contruct查询。对于JSON格式,那么你需要一个支持@AKSW的三重存储/SPARQL引擎。我已经更新了我的答案,以包括一个稍微改进的查询和该查询的输出。好的,这是SPARQL JSON,但缺少什么。我的意思是,它完全包含了您查询的内容。如果您还需要关于餐厅的数据,我强烈建议使用SPARQLCONSTRUCT
query并使用JSON-LD作为响应类型。我也不认为使用单个SELECT查询是可能的。我的意思是,一方面您需要分组,但另一方面您需要组中每个实体的数据。有时使用(a)多个查询要简单得多,或者更经常地使用(b)在客户端进行数据处理要好得多。
{
"head" : {
"vars" : [ "restaurantChainId", "restaurantTypeIdList", "restaurant" ]
},
"results" : {
"bindings" : [ {
"restaurantChainId" : {
"type" : "literal",
"value" : "XYZ"
},
"restaurantTypeIdList" : {
"type" : "literal",
"value" : "ABA3"
},
"restaurant" : {
"type" : "literal",
"value" : "http://example.co.uk/restaurant/XYZ-4, http://example.co.uk/restaurant/XYZ-5, http://example.co.uk/restaurant/XYZ-6"
}
}, {
"restaurantChainId" : {
"type" : "literal",
"value" : "ABC"
},
"restaurantTypeIdList" : {
"type" : "literal",
"value" : "ABA1"
},
"restaurant" : {
"type" : "literal",
"value" : "http://example.co.uk/restaurant/ABC-14, http://example.co.uk/restaurant/ABC-15, http://example.co.uk/restaurant/ABC-4"
}
}, {
"restaurantChainId" : {
"type" : "literal",
"value" : "XYZ"
},
"restaurantTypeIdList" : {
"type" : "literal",
"value" : "ABA1"
},
"restaurant" : {
"type" : "literal",
"value" : "http://example.co.uk/restaurant/XYZ-2, http://example.co.uk/restaurant/XYZ-1"
}
}, {
"restaurantChainId" : {
"type" : "literal",
"value" : "XYZ"
},
"restaurantTypeIdList" : {
"type" : "literal",
"value" : "ABA2"
},
"restaurant" : {
"type" : "literal",
"value" : "http://example.co.uk/restaurant/XYZ-3"
}
}, {
"restaurantChainId" : {
"type" : "literal",
"value" : "ABC"
},
"restaurantTypeIdList" : {
"type" : "literal",
"value" : "ABA2"
},
"restaurant" : {
"type" : "literal",
"value" : "http://example.co.uk/restaurant/ABC-9"
}
} ]
}
}