Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda querying secondary index

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);
        }
    }
};

Where the lambda test data is

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

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));
    });
}
like image 552
Iorek Avatar asked Aug 28 '26 02:08

Iorek


1 Answers

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);
    });
}

Rewrote and it's clean.

like image 111
Iorek Avatar answered Aug 30 '26 16:08

Iorek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!