AWS Lambda 查询二级索引 [英] AWS Lambda querying secondary index

查看:17
本文介绍了AWS Lambda 查询二级索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是 AWS lambda 中对 dynamoDB JSON 对象的 node.js 查询.UserID 是没有排序键的主键.GeoHash 是辅助键,索引名称为GeoHash-index".调用成功且没有错误,但不会返回任何内容.下面的测试数据可能是错误的,因为它没有提供与索引名称的任何连接,但我是 AWS/noSQL 的新手,有点迷茫.

The following is node.js query in AWS lambda on a dynamoDB JSON object. UserID is the primary key with no sort key. GeoHash is a secondary key, with index name "GeoHash-index". The call succeeds with no error, but does not result in anything being returned. It's possible that the test data below is wrong as it does not offer any connection to the index name, but I'm new to AWS/noSQL and a little lost.

var AWS = require('aws-sdk');
var docClient = new AWS.DynamoDB({apiVersion: '2012-08-10'});

exports.handler = function(event,context,callback) {

    console.log(JSON.stringify(event, null, '  '));

    var tableName = "table1";

    // getItem
    docClient.getItem({
        TableName: tableName,
        IndexName: "GeoHash-index",
        KeyConditionExpression: "GeoHash = :geohash",
        ExpressionAttributeValues: {":geohash": "dpz886gb0tb0"}
    }), 

    function(err,data){
        if(err){
             callback(err);
        } else {
             callback(null,data);
        }
    }
};

lambda 测试数据在哪里

Where the lambda test data is

{
  "GeoHash": "dpz886gb0tb0",
  "Radius": 2,
  "Timestamp": 1472601493180,
  "UserID": "User1"
}

GeoHash 字符串应该相互匹配.想法?

The GeoHash string should match each other. Thoughts?

编辑这种方法也没有成功

var AWS = require('aws-sdk');
var docClient = new AWS.DynamoDB({apiVersion: '2012-08-10'});

exports.handler = function index(event, context, callback) {
    var params = {
        TableName: "LocationAware1",
        IndexName: "GeoHash-index",
        KeyConditionExpression: "GeoHash = :geohash",
        ExpressionAttributeValues: {
            ":geohash": {'S': 'dpz886gb0tb0'}
        },
    };

    docClient.query(params, function(err, data) {
        if (err)
            console.log(JSON.stringify(err));
        else
            console.log(JSON.stringify(data));
    });
}

推荐答案

var AWS = require('aws-sdk');

exports.handler = function(event,context,callback) {

    var params = {
      TableName: 'Table1',
      IndexName: 'GeoHash-index',
      KeyConditionExpression: 'GeoHash = :geohash',
      ExpressionAttributeValues: {
        ':geohash': 'dpz886g8p9e2',
      }
    };

    var docClient = new AWS.DynamoDB.DocumentClient();

    docClient.query(params, function(err, data) {
       if (err) callback(err);
       else callback(null, data);
    });
}

重写了,很干净.

这篇关于AWS Lambda 查询二级索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆