简体   繁体   English

AWS ruby​​ sdk v2 - dynamodb查询

[英]AWS ruby sdk v2 - dynamodb query

I've got a hash (string) and range (number) table in DynamoDB. 我在DynamoDB中有一个哈希(字符串)和范围(数字)表。 I'm trying to run a query using the ruby SDK v2.0.30 but keep getting the following error: 我正在尝试使用ruby SDK v2.0.30运行查询,但不断收到以下错误:

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. 我是ruby的新手并尝试在线查看AWS文档,但找不到任何东西。 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. 适用于Ruby的v2 AWS SDK( aws-sdk gem)接受所有属性值作为vanilla Ruby值。

A value can be: 可以是:

  • String
  • Numeric (Integer, Float, BigDecimal, etc) 数字(整数,浮点数,BigDecimal等)
  • Boolean 布尔
  • IO (blob type) IO(blob类型)
  • Set (of Numeric/String) 设置(数字/字符串)
  • Array (of values ) 数组(
  • Hash (String => value ) 哈希(String => value

You do not need to provide the type hint as was required with the v1 AWS SDK for Ruby. 您无需提供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: 此外,与您的问题没有直接关系,但您可以在aws-sdk gem中使用DynamoDB时发现以下博客系列有用:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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