Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

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)

Does anyone know what might be happening?

1 Answer

0 votes
by (44.4k points)

You are querying the primary index rather than the secondary index. For using the secondary index, you must use V2 of the API for specifying the index.

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}

      ]

    }   } })

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...