在 DynamoDB 中查询(使用 dynamodb-geo npm)- 错误消息:ValidationException:不支持查询关键条件

问题描述 投票:0回答:1

因此,我在将 dynamodb-geo 实施到我的程序中时遇到了麻烦,该程序可以找到指定位置 1 公里范围内的餐车。我正在关注这个视频作为这样做的教程。我将在下面附上我的代码和错误消息,以及任何有用的信息:

'use strict'

const AWS = require('aws-sdk')
AWS.config.update({
accessKeyId: 'my access id', // dw i have an actual id
secretAccessKey: 'my secret key', // dw i have an actual key
region: 'my region' //this matches with the region the table is in
});
const ddb = new AWS.DynamoDB()
const ddbGeo = require('dynamodb-geo')

const config = new ddbGeo.GeoDataManagerConfiguration(ddb, 'powerhouse_table')
config.hashKeyLength = 5

const myGeoTableManager = new ddbGeo.GeoDataManager(config)

myGeoTableManager.queryRadius({
RadiusInMeter: 1000,
CenterPoint: {
latitude: 30,
longitude: -90
}
})
.then((locations) => {
console.log('Locations found: ', locations.length)
console.log(locations)
})

我的错误消息:

  resp.error = util.error(new Error(), error);
                          ^

ValidationException: Query key condition not supported

最初,它给了我一个错误,因为没有名为 geohash-index 的索引,所以我为 geohash-index 制作了一个 GSI。分区键是“geohash”,数据类型为 Number(我尝试了 Number 和 String 作为我的数据类型,但收到了相同的错误消息)。我没有设置可选的 sortKey。

此外,这是我的表格的片段:

vendor-id 是表的分区键

有人知道如何修复这个错误吗?谢谢。

顺便说一句,我尝试更改全局二级索引上的数据类型,但不幸的是,字符串和数字都给了我同样的错误(我问 ChatGPT 为什么数字应该是分区键的 GSI 数据类型,它说 AWS以数字方式存储 geohashes)。

node.js amazon-web-services amazon-dynamodb querying geohashing
1个回答
0
投票

问题

您收到的错误是您的表(即“powerhouse_table”)当前结构的结果。

默认情况下,dynamo-geo 希望表的分区键被称为/命名为

hashKey
。此外,该键的值是由 dynamo-geo 自动生成和管理的。查看更多详情这里

在您当前的表结构中,分区键的名称是

vendor_id
,并且该值不是 dynamo-geo 期望的值。

当您执行半径查询时,dynamo-geo 将使用

hashKey
作为生成并发送到 Dynamo 的查询的分区键,当然,这会失败,表上没有这样的键。

解决方案

您应该调整表结构以适应 dynamo-geo 的要求。

我认为创建与 dynamo-geo 配合良好的表的最简单方法是使用 GeoTableUtil.getCreateTableRequest 实用程序函数。它将生成一个 createTable 请求,其中包含使用 dynamo-geo 所需的属性和索引定义。您可以针对您的 dynamo 数据库实例执行创建表请求。

您应该看一下here的示例,它也应该可以澄清很多事情。

我知道您目前正在按vendor_id对“powerhouse_table”进行分区,因为您希望能够按vendorId进行查询,为了在表重组后实现这一点,您可以创建一个以vendor_id为分区键的

全局二级索引
.

© www.soinside.com 2019 - 2024. All rights reserved.