用dynamodb从python获取get_item

时间:2018-11-15 02:49:48

标签: aws-lambda amazon-dynamodb

这个问题此刻正在轻描淡写地杀了我。 我正在尝试学习python,lambda和Dynamodb。

Python看起来很棒,我能够使用Xampp之类的普通MySQL服务器连接到MySQL,目标是学习与Dynamodb一起工作,但是以某种方式,我无法从Dynamodb中获取_items。这真的让我大吃一惊,已经过去了两天。

已经看过大量的youtube电影并阅读了aws文档。

任何提示我做错了什么。 直到现在我的代码;

import json
import boto3
from boto3.dynamodb.conditions import Key, Attr

#always start with the lambda_handler
def lambda_handler(event, context):

    # make the connection to dynamodb
    dynamodb = boto3.resource('dynamodb')

    # select the table
    table = dynamodb.Table("html_contents")

    # get item from database
    items = table.get_item(Key={"id": '1'})

我看到的每个地方都应该这样做。 但是我一直收到以下错误

{errorMessage=An error occurred (ValidationException) when calling the GetItem operation: The provided key element does not match the schema, errorType=ClientError, stackTrace=[["\/var\/task\/lambda_function.py",16,"lambda_handler","\"id\": '1'"],["\/var\/runtime\/boto3\/resources\/factory.py",520,"do_action","response = action(self, *args, **kwargs)"],["\/var\/runtime\/boto3\/resources\/action.py",83,"__call__","response = getattr(parent.meta.client, operation_name)(**params)"],["\/var\/runtime\/botocore\/client.py",314,"_api_call","return self._make_api_call(operation_name, kwargs)"],["\/var\/runtime\/botocore\/client.py",612,"_make_api_call","raise error_class(parsed_response, operation_name)"]]}

我的数据库结构。

enter image description here

我的DynamoDb设置 表名html_contents 主分区键ID(数字) 主排序键- 时间点恢复DISABLEDEnable 加密已禁用 生存时间属性DISABLEDManage TTL 表格状态为有效

我在这里做错了什么?我开始认为我在aws配置上做错了。

先谢谢您。

卫斯理

2 个答案:

答案 0 :(得分:2)

那是@ippi。

这是我正在使用的引号。

table.get_item(Key={"id": '1'})

需要

table.get_item(Key={"id": 1})

因为我使用的是数字而不是字符串。 希望这对下一个有相同问题的人有所帮助。

答案 1 :(得分:1)

您面临这个问题是因为您创建了一个带有数据类型为整数的分区键的表。 现在,您正在执行读取项目操作,将分区指定为字符串,该字符串需要是导致此问题的整数。

我是 Lucid-Dynamodb 的作者,它是 AWS DynamoDB 的极简包装器。它涵盖了所有 Dynamodb 操作。

参考: https://github.com/dineshsonachalam/Lucid-Dynamodb#4-read-an-item