Dynamodb:使用二级索引查询表

ksa*_*amc 10 ruby amazon-web-services amazon-dynamodb

我使用gem aws-sdk-ruby查询一个看起来像这样的表:

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               |
Run Code Online (Sandbox Code Playgroud)

我想要做的是获取在特定日期之后创建的所有'aaaa'行.例如:

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}
      ]
    }
  }
})
Run Code Online (Sandbox Code Playgroud)

当我运行上面的代码时,我得到这个:

Query condition missed key schema element guid (AWS::DynamoDB::Errors::ValidationException)
Run Code Online (Sandbox Code Playgroud)

使用hashKey和RangeKey运行查询但是当我用范围索引替换rangeKey时,它无法告诉我rangeKey是必需的.

如果我然后添加范围键(这没有意义)我得到以下错误:

Conditions can be of length 1 or 2 only (AWS::DynamoDB::Errors::ValidationException)
Run Code Online (Sandbox Code Playgroud)

任何人都知道可能会发生什么?

pre*_*ion 13

您没有查询二级索引,而是查询主索引(哈希和范围键).要在DynamoDB中使用辅助索引,必须使用API​​的V2并在查询操作中指定索引

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}
      ]
    }   } })
Run Code Online (Sandbox Code Playgroud)

  • 不推荐使用`AWS :: DynamoDB :: ClientV2`.您应该使用:`AWS :: DynamoDB :: Client.new(api_version:'2012-08-10')` (5认同)