Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamodb: Querying tables with secondary index

I am using the gem aws-sdk-ruby to query a table which looks something like this:

hk (Hashkey)  |  guid(Rangekey)  |  Timestamp (Secondary Range index)  |  other attributes
aaaa          |  50              |  2013-02-04T12:33:00Z               |
aaaa          |  244             |  2013-04-22T04:54:00Z               |
aaaa          |  342             |  2013-05-18T06:52:00Z               |
bbbb          |  243             |  2013-06-21T13:17:00Z               |

What I am trying to do is get all 'aaaa' rows that were created after a certain date. Ex:

AWS.config(access_key_id: 'xxx', secret_access_key: 'xxx', :dynamo_db => { :api_version => '2012-08-10' })
client = AWS::DynamoDB::Client.new
client.query(
{
  table_name: 'table',
  select: 'ALL_ATTRIBUTES',
  key_conditions: {
    'hk' => {
      comparison_operator: 'EQ',
      attribute_value_list: [
        {'s' => 'aaaa'}
      ]
    },
    'timestamp' => {
      comparison_operator: 'GE',
      attribute_value_list: [
        {'s' => Time.now.utc.iso8601}
      ]
    }
  }
})

When i run the code above i get this:

Query condition missed key schema element guid (AWS::DynamoDB::Errors::ValidationException)

Running the query with hashKey and RangeKey works but when i replace the rangeKey with a secondary range index it fails telling me that the rangeKey is required.

If i then add the range key (Which makes no sense) i get the following error instead:

Conditions can be of length 1 or 2 only (AWS::DynamoDB::Errors::ValidationException)

Anyone know what might be happening?

like image 477
ksamc Avatar asked Aug 02 '13 15:08

ksamc


People also ask

Why would you use a secondary index on a DynamoDB table?

A global secondary index lets you query over the entire table, across all partitions. A local secondary index lets you query over a single partition, as specified by the partition key value in the query. Queries on global secondary indexes support eventual consistency only.

How many secondary indexes are allowed per DynamoDB table?

Each table in DynamoDB can have up to 20 global secondary indexes (default quota) and 5 local secondary indexes. For more information about the differences between global secondary indexes and local secondary indexes, see Improving data access with secondary indexes.

Can we query DynamoDB table without primary key?

Hash key in DynamoDB The primary reason for that complexity is that you cannot query DynamoDB without the hash key. So, it's not allowed to query the entire database. That means you cannot do what you would call a full table scan in other databases.


1 Answers

You are not querying the secondary index, you are querying the primary index(hash and range key). To use a secondary index with DynamoDB you must use V2 of the API and specify the index in the query operation

client = AWS::DynamoDB::Client.new(api_version: '2012-08-10') 

client.query( {   :table_name: 'table',   :index_name: "timestamp-index", :select: 'ALL_PROJECTED_ATTRIBUTES',   :key_conditions: {
    'hk' => {
      :comparison_operator: 'EQ',
     :attribute_value_list: [
        {'s' => 'aaaa'}
      ]
    },
    'timestamp' => {
     :comparison_operator: 'GE',
      :attribute_value_list: [
        {'s' => Time.now.utc.iso8601}
      ]
    }   } })
like image 81
prestomation Avatar answered Oct 26 '22 13:10

prestomation