user1503606
user1503606

Reputation: 4300

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.

Upvotes: 7

Views: 2324

Answers (2)

Mike Brant
Mike Brant

Reputation: 71422

I don't know that Dynamo is going to be a good solution if you will need to frequently support geospatial lookup use cases. You might consider database solutions which more directly support geospatial functionality.

Upvotes: 0

filipebarretto
filipebarretto

Reputation: 1952

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

Upvotes: 3

Related Questions