Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying AWS DynamoDB to return lat and lon result within radius

I am fairly new to dynamodb and i have it setup with lambda and api gateway.

My dynamodb table looks like this. enter image description here

Now i am simply querying the table with the following lambda function.

var AWS = require('aws-sdk');
var dynamoDB = new AWS.DynamoDB();

exports.handler = function(event, context) {

    /**
     * Debugging events
     * @type {[type]}
     */
    console.log("Request received:\n", JSON.stringify(event));
    console.log("Context received:\n", JSON.stringify(context));

    /**
     * Important this needs to be your Dynamo DB table name
     * @type {String}
     */
    var tableName = "Tracker";
    var datetime = new Date().getTime().toString();

    var queryData = {
        "TableName": tableName,
        "ConsistentRead": true,
        "KeyConditionExpression": "TrackIt = :val",
        "ExpressionAttributeValues": {":val": {"S": event.tid}}
    };
    dynamoDB.query(queryData, function(err, data) {
        if (err) {
            context.fail('ERROR: Dynamo failed: ' + err);
        }else{
            context.done(null,data.Items);
        }
    });

};

Now what i want to do is query the table based on latitude and longitude values in a certain radius say 3 miles, now i know how to do this with mysql but i would really like to know where to start when trying to do this with dynamodb.

I cant really find any helpful tutorials online has anyone else done this or is doing this that could point me in the right direction i would really appreciate it.

like image 656
user1503606 Avatar asked Jul 10 '16 20:07

user1503606


People also ask

Does DynamoDB support range queries?

Querying is a very powerful operation in DynamoDB. It allows you to select multiple Items that have the same partition ("HASH") key but different sort ("RANGE") keys.

What does DynamoDB Query return?

In a Query operation, DynamoDB retrieves the items in sorted order, and then processes the items using KeyConditionExpression and any FilterExpression that might be present. Only then are the Query results sent back to the client. A Query operation always returns a result set.

How do I Query DynamoDB locally?

To access DynamoDB running locally, use the --endpoint-url parameter. The following is an example of using the AWS CLI to list the tables in DynamoDB on your computer. The AWS CLI can't use the downloadable version of DynamoDB as a default endpoint. Therefore, you must specify --endpoint-url with each AWS CLI command.


1 Answers

You can use an expression such as:

var queryData = {
        "TableName": tableName,
        "ConsistentRead": true,
        "KeyConditionExpression": "#latitude between :min and :max",
        "ExpressionAttributeNames": {
            "#latitude": "latitude",
        },
        "ExpressionAttributeValues": {
            ":min": [MIN_LATITUDE],
            ":max": [MAX_LATITUDE] 
        }
    };

For more information: http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.NodeJs.04.html

like image 169
filipebarretto Avatar answered Oct 02 '22 21:10

filipebarretto