2015-06-09 88 views
2

我想使用AWS SDK for Ruby V2通過哈希和範圍鍵查詢DynamoDB表。以下代碼可以工作。通過哈希和範圍鍵查詢DynamoDB表

dynamodb = Aws::DynamoDB::Client.new(region: 'somewhere') 
dynamodb.query(
    table_name: TABLE_NAME, 
    key_conditions: { 
    HASH_KEY_NAME => { 
     attribute_value_list: ['hoge'], 
     comparison_operator: 'EQ' 
    }, 
    RANGE_KEY_NAME => { 
     attribute_value_list: ['foo'], 
     comparison_operator: 'EQ' 
    } 
    } 
) 

但是,我想設置多個項目以範圍鍵條件。

像這樣:

dynamodb = Aws::DynamoDB::Client.new(region: 'somewhere') 
dynamodb.query(
    table_name: TABLE_NAME, 
    key_conditions: { 
    HASH_KEY_NAME => { 
     attribute_value_list: ['hoge'], 
     comparison_operator: 'EQ' 
    }, 
    RANGE_KEY_NAME => { 
     attribute_value_list: ['foo', 'bar'], 
     comparison_operator: 'EQ' 
    } 
    } 
) 

此代碼返回lib/ruby/gems/2.2.0/gems/aws-sdk-core-2.0.48/lib/seahorse/client/plugins/raise_response_errors.rb:15:in `call': One or more parameter values were invalid: Invalid number of argument(s) for the EQ ComparisonOperator (Aws::DynamoDB::Errors::ValidationException)

我試過使用IN運算符。

dynamodb = Aws::DynamoDB::Client.new(region: 'somewhere') 
dynamodb.query(
    table_name: TABLE_NAME, 
    key_conditions: { 
    HASH_KEY_NAME => { 
     attribute_value_list: ['hoge'], 
     comparison_operator: 'EQ' 
    }, 
    RANGE_KEY_NAME => { 
     attribute_value_list: ['foo', 'bar'], 
     comparison_operator: 'IN' 
    } 
    } 
) 

它返回lib/ruby/gems/2.2.0/gems/aws-sdk-core-2.0.48/lib/seahorse/client/plugins/raise_response_errors.rb:15:in `call': Attempted conditional constraint is not an indexable operation (Aws::DynamoDB::Errors::ValidationException)

如何通過一個散列鍵和多個範圍鍵查詢DynamoDB表?

+0

你不能。 'KeyConditions'不支持'IN'運算符。看到我對[這個問題]的答案(http://stackoverflow.com/q/30589635/627727)。 – mkobit

+0

非常感謝。這意味着'query'方法不能設置多個範圍鍵作爲查詢條件嗎? – necojackarc

回答

0

Query操作只允許在Range Key以下運算符:

EQ | LE | LT | GE | GT | BEGINS_WITH | BETWEEN 

對於查詢操作,條件是用於指定 KeyConditions查詢表或索引時使用。對於 KeyConditions,僅支持以下比較運算符:

EQ | LE | LT | GE | GT | BEGINS_WITH | BETWEEN

來源: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryAndScan.html

您仍然可以通過使用符合要求的FilterExpression

:filter_expression => "RANGE_KEY_NAME in (:id1, :id2)",{ ":id1" => "hoge",":id2" => "foo"} 

但是消耗的供應吞吐量將基於該查詢返回的結果,而不是過濾結果組。

另一種選擇是通過BatchGetItem發送多個GetItem請求(每個請求可能有Range Key值)。其結果將只包含匹配的記錄:

resp = dynamodb.batch_get_item(
    # required 
    request_items: { 
    "TableName" => { 
     # required 
     keys: [ 
     { 
      "AttributeName" => "value", #<Hash,Array,String,Numeric,Boolean,nil,IO,Set>, 
     }, 
     ], 
     attributes_to_get: ["AttributeName", '...'], 
     consistent_read: true, 
     projection_expression: "ProjectionExpression", 
     expression_attribute_names: { "ExpressionAttributeNameVariable" => "AttributeName" }, 
    }, 
    }, 
    return_consumed_capacity: "INDEXES|TOTAL|NONE", 
) 

來源:http://docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html

+0

'batch_get_item'是解決方案!謝謝! – necojackarc

相關問題