Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS DynamoDB Scan filterExpression - simple number comparison

I am trying to do a simple dynamoDB scan with a filter expression (documentation here)

This is my expression string:

"attribute_exists("my_db_key") AND ("my_db_key" = 1)"

This simply states:

"If a value for my_db_key exists AND my_db_key EQUALS 1, return it in the results"

However it does not work and I get a this error:

Invalid FilterExpression: Syntax error; token: "1", near: "= 1)

I am aware that I can use an attribute value placeholder for values and then use that in the expression but I do not want to do this. And according to Amazon's documentation it is NOT required.

So how do I do this simple expression? Does anyone have an example or link to documentation? Amazon's documentation is unfortunately of no help.

NOTE: I am implementing this with AWSDynamoDBScanInput on iOS but my issue here is to do with global expression syntax so it should not matter.

like image 717
Zigglzworth Avatar asked Mar 04 '15 15:03

Zigglzworth


People also ask

Which is faster scan or query in DynamoDB?

More complex queries on DynamoDB data are occasionally required. Instead of scanning for such queries, it is usually preferable to create a GSI (global secondary index). Out of interest, I ran an experiment to confirm that Scan operation is indeed slower than Query operation.

How fast is DynamoDB scan?

By default, the Scan operation processes data sequentially. Amazon DynamoDB returns data to the application in 1 MB increments, and an application performs additional Scan operations to retrieve the next 1 MB of data. The larger the table or index being scanned, the more time the Scan takes to complete.

How can I improve DynamoDB scan performance?

For faster response times, design your tables and indexes so that your applications can use Query instead of Scan . (For tables, you can also consider using the GetItem and BatchGetItem APIs.) Alternatively, design your application to use Scan operations in a way that minimizes the impact on your request rate.

What is the difference between DynamoDB scan and query?

DynamoDB scans and GSIs. DynamoDB supports two different types of read operations, which are query and scan. A query is a lookup based on either the primary key or an index key. A scan is, as the name indicates, a read call that scans the entire table in order to find a particular result.


2 Answers

Your params need to look something like this (for the Node AWS library):

params = {
  "FilterExpression": 'attribute_exists("my_db_key") AND ("my_db_key" = :value)',
  "ExpressionAttributeValues": {
    ":value": 1
  },
  // ...
};

docClient.scan(params, function(err, data){
  // Handle err or process data
})

For some languages, the parameters should look more like this:

{
  "FilterExpression": 'attribute_exists("my_db_key") AND ("my_db_key" = :value)',
  "ExpressionAttributeValues": {
    ":value": {"N":1}
  },
  // ...
};
like image 185
Nate Avatar answered Oct 20 '22 12:10

Nate


You have to use a placeholder and pass the value separately. Here's some documentation and a Post from AWS forums

like image 34
Max Avatar answered Oct 20 '22 14:10

Max