I am fairly new to dynamodb and i have it setup with lambda and api gateway.
My dynamodb table looks like this.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With