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

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

问题描述

以下是dynamoDB JSON对象上AWS lambda中的node.js查询。
用户ID是没有排序键的主键。
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);
        }
    }
};

λ测试数据在哪里

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

GeoHash字符串应彼此匹配。有想法吗?

The GeoHash string should match each other. Thoughts?

编辑
这种方法也没有成功

EDIT No success with this method either

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天全站免登陆