我正在使用' aws-sdk','〜> 44年2月6日&#39 ;.我有一个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 documentation site包含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表应该是什么样的正确方法?
答案 0 :(得分: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>
:)