DynamoDB表使用全局二级索引查询项目

时间:2017-05-13 22:01:22

标签: node.js amazon-web-services amazon-dynamodb

我正在尝试查询具有各种位置的纬度和经度的发电机表。我希望在用户平移地图时获取某些坐标之间的值。

表的主键是city,排序键是id。我创建了一个全局二级索引,其中lat作为分区键,lon作为排序键(用于查询纬度和经度两点之间的位置)。

我正在尝试使用此查询:

let doc = require('dynamodb-doc');
let dynamo = new doc.DynamoDB();

...

        var params = {
            TableName : "locations-dev",
            IndexName: "lat-lon-index",
            KeyConditionExpression: "lon between :lon2 and :lon1 AND lat between :lat1 and :lat2",
            ExpressionAttributeValues: {
                ":lat1": JSON.stringify(event.bodyJSON.east),
                ":lat2": JSON.stringify(event.bodyJSON.west),
                ":lon1": JSON.stringify(event.bodyJSON.north),
                ":lon2": JSON.stringify(event.bodyJSON.south)
            }
        };

        dynamo.query(params, function (err, data) {
            if (err) {
                console.error('Error with ', err);
                context.fail(err);
            } else {
                context.succeed(data);
            }
        });

但是我收到了这个错误:

{
   "errorMessage": "Query key condition not supported",
   "errorType": "ValidationException",
   "stackTrace": [
    ...
  ]
 }

以下是Dynamo中的示例项目:

{
  "id": "18",
  "lat": "39.923070",
  "lon": "-86.036178",
  "name": "Home Depot",
  "phone": "(317)915-8534",
  "website": "https://corporate.homedepot.com/newsroom/battery-recycling-one-million-pounds"
}

1 个答案:

答案 0 :(得分:3)

DynamoDB中的主键(即使在二级索引中)只能使用equals条件进行查询。此约束源自其内部表示,因为它存储为散列值以标识其项目分区。无法通过范围查询这些散列值。

Choosing the Right DynamoDB Partition Key

  

除扫描外,DynamoDB API操作需要相同的运算符   (EQ)表和GSI的分区键。结果,   分区键必须是您可以轻松查询的内容   使用简单查找的应用程序(例如,使用key = value,其中   返回一个或多个项目。