简体   繁体   English

使用 Python 从 DynamoDB 检索数据

[英]Retrieving data from a DynamoDB using Python

A newbie to DynamoDb and python in general here.这里一般是 DynamoDb 和 python 的新手。 I have a task to complete where I have to retrieve information from a DynamoDB using some information provided.我有一项任务要完成,我必须使用提供的一些信息从 DynamoDB 中检索信息。 I've set up the access keys and such and I've been provided a 'Hash Key' and a table name.我已经设置了访问密钥等,并为我提供了一个“哈希键”和一个表名。 I'm looking for a way to use the hash key in order to retrieve the information but I haven't been able to find something specific online.我正在寻找一种使用哈希键来检索信息的方法,但我无法在网上找到特定的东西。

#Table Name
table_name = 'Waypoints'

    #Dynamodb client
    dynamodb_client = boto3.client('dynamodb')
    
    #Hash key
    hash_key = {
        ''
    }
    #Retrieve items
    response = dynamodb_client.get_item(TableName = table_name, Key = hash_key)

Above is what I have writtenbut that doesn't work.以上是我写的,但这不起作用。 Get item only returns one_item from what I can gather but I'm not sure what to pass on to make it work in the first place. Get item 仅从我可以收集的内容中返回 one_item,但我不确定首先要传递什么才能使其正常工作。

Any sort of help would be greatly appreaciated.任何形式的帮助将不胜感激。

First of all, in get_item() request the key should not be just the key's value, but rather a map with the key's name and value.首先,在get_item()请求中,键不应该只是键的值,而应该是键的名称和值的映射。 For example, if your hash-key attribute is called "p", the Key you should pass would be {'p': hash_key} .例如,如果您的 hash-key 属性称为“p”,则您应该传递的Key将是{'p': hash_key}

Second, is the hash key the entire key in your table?其次,哈希键是表中的整个键吗? If you also have a sort key, in a get_item() you must also specify that part of the key - and the result is one item.如果您还有排序键,则在get_item()中您还必须指定键的该部分 - 结果是一项。 If you are looking for all the items with a particular hash key but different sort keys, then the function you need to use is query() , not get_item() .如果您要查找具有特定哈希键但排序键不同的所有项目,那么您需要使用的函数是query() ,而不是get_item()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM