使用Ruby SDK进行DynamoDB查询

问题描述

我真的很难使查询正常工作。我正在尝试使所有主键(“关键字”)等于“堆肥汤匙”的物品。

这是我的物品之一

{
  "keyword": "compost spoons","created_at": "2020-08-12T11:31:21+00:00"
}

这是我查询中的参数:

params = {
    table_name: "servings",select: "ALL_ATTRIBUTES",return_consumed_capacity: "INDEXES",key_condition_expression: "keyword = :keyword_val",expression_attribute_values: {
        "keyword_val" => "compost spoon"
    }
  }

通过的错误

"ExpressionAttributeValues contains invalid key: Syntax error; key: \"keyword_val\""

什么是参数才能使查询正常工作?

架构:

{
  "AttributeDeFinitions": [
    {
      "AttributeName": "keyword","AttributeType": "S"
    },{
      "AttributeName": "created_at","AttributeType": "S"
    }
  ],"TableName": "servings","KeySchema": [
    {
      "AttributeName": "keyword","KeyType": "HASH"
    },"KeyType": "RANGE"
    }
  ],"TableStatus": "ACTIVE","CreationDateTime": "2020-08-14T10:26:55.528Z","ProvisionedThroughput": {
    "LastIncreaseDateTime": "1970-01-01T00:00:00.000Z","LastDecreaseDateTime": "1970-01-01T00:00:00.000Z","NumberOfDecreasesToday": 0,"ReadCapacityUnits": 5,"WriteCapacityUnits": 5
  },"TableSizeBytes": 168,"ItemCount": 3,"TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/servings"
}

解决方法

好像您在keyword_valexpression_attribute_values前面缺少一个冒号。我相信应该是:

params = {
    table_name: "servings",select: "ALL_ATTRIBUTES",return_consumed_capacity: "INDEXES",key_condition_expression: "keyword = :keyword_val",expression_attribute_values: {
        ":keyword_val" => "compost spoon".  // <---- CHANGE IS HERE!!
    }
  }