0

我正在使用“aws-sdk”、“~> 2.6.44”。我有一张activities表,用于存储用户执行的所有活动。

params = {
        table_name: 'activities', # required
        key_schema: [ # required
            {
                attribute_name: 'actor', # required User.1
                key_type: 'HASH', # required, accepts HASH, RANGE
            },
            {
                attribute_name: 'created_at', # timestamp
                key_type: 'RANGE'
            }
        ],....

我想用用户在过去 1 天执行的所有活动查询此表。看起来AWS 文档站点有 SDK 版本 3 的文档。

tableName = 'activities'
params = {
    table_name: tableName,
    key_condition_expression: "#user = :actor and #time between :start_time and :end_time",
    expression_attribute_names: {
        "#user" => 'actor',
        "#time" => "created_at"
    },
    expression_attribute_values: {
        actor: 'User.1',
        ":start_time" => (Time.now - 1.days).to_i,
        ":end_time" => (Time.now + 1.days).to_i
    }
}

DynamodbClient.client.get_item(params)
# Throws:  ArgumentError: no such member :key_condition_expression

我尝试使用过滤器表达式:

tableName = 'activities'
params = {
    table_name: tableName,
    key: {
        actor: 'User.1'
    },
    filter_expression: "created_at BETWEEN (:id1, :id2)",
    expression_attribute_values: { ":id1" => (Time.now - 1.days).to_i,":id2" => (Time.now + 1.days).to_i},
    projection_expression: "actor"
}

DynamodbClient.client.get_item(params)

# Throws ArgumentError: no such member :filter_expression

使用范围键的范围选项查询 DynamoDB 表的正确方法应该是什么?

4

1 回答 1

0

query如果我不想检索特定记录,我应该使用它。

以下查询有效:

tableName = 'activities'
params = {
    table_name: tableName,
    key_condition_expression: "#user = :actor and #time between :start_time and :end_time",
    expression_attribute_names: {
        "#user" => 'actor',
        "#time" => "created_at"
    },
    expression_attribute_values: {
        ":actor" => 'User.1',
        ":start_time" => (Time.now - 1.days).to_i,
        ":end_time" => (Time.now + 1.days).to_i
    }
}

DynamodbClient.client.query(params)

 => #<struct Aws::DynamoDB::Types::QueryOutput items=[{"actor"=>"User.1", "action"=>"Like", "created_at"=>#<BigDecimal:7fa6418b86e0,'0.150683976E10',18(27)>, "source"=>"FeedSource.661", "body"=>{"id"=>#<BigDecimal:7fa6418b82f8,'0.72E2',9(18)>}, "target"=>"FeedEntry.8419"}], count=1, scanned_count=1, last_evaluated_key=nil, consumed_capacity=nil> 

:)

于 2017-10-01T08:28:30.330 回答