简体   繁体   中英

AWS ruby sdk v2 - dynamodb query

I've got a hash (string) and range (number) table in DynamoDB. I'm trying to run a query using the ruby SDK v2.0.30 but keep getting the following error:

aws-sdk-core-2.0.30/lib/seahorse/client/plugins/raise_response_errors.rb:15:in `call': One or more parameter values were invalid: Condition parameter type does not match schema type (Aws::DynamoDB::Errors::ValidationException)

Here is my code:

gem 'aws-sdk', '~> 2'
require 'aws-sdk'

dynamodb = Aws::DynamoDB::Client.new(region: 'eu-west-1', credentials: creds)
resp = dynamodb.query(
      table_name: "TEST_TABLE",
      key_conditions: {
        'ID' => {
          comparison_operator: 'EQ',
          attribute_value_list: [{ 's' => 'test123' }]
        }
      })

I'm new to ruby and have tried looking online and on AWS docs but can't find anything. Any help would be appreciated.

Thanks

Your error is in how you formatted the value of the hash key in your query expression. The v2 AWS SDK for Ruby ( aws-sdk gem) accepts all attribute values as vanilla Ruby values.

A value can be:

  • String
  • Numeric (Integer, Float, BigDecimal, etc)
  • Boolean
  • IO (blob type)
  • Set (of Numeric/String)
  • Array (of values )
  • Hash (String => value )

You do not need to provide the type hint as was required with the v1 AWS SDK for Ruby.

ddb = Aws::DynamoDB::Client.new
ddb.query({
  table_name: 'TEST_TABLE',
  key_conditions: {
    'ID' => {
      comparison_operattor: 'EQ',
      attribute_value_list: ['test-123']      
    }
  }
})

Also, not directly related to you question, but you may find the following blog series helpful when working with DynamoDB from the aws-sdk gem:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM