使用Python从DynamoDB中检索数据
一般而言,DynamoDB和Python的新手。我有一项任务可以完成我必须使用一些信息从DynamoDB中检索信息的地方。我已经设置了访问密钥等等,并且得到了一个“哈希键”和一个表名。我正在寻找一种使用哈希密钥来检索信息的方法,但是我无法在线找到特定的东西。
#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)
上面是我写过的,但不起作用。获取物品仅从我可以收集的内容中返回One_item,但我不确定要传递什么以使其首先发挥作用。
任何形式的帮助都将得到极大的认可。
A newbie to DynamoDb and python in general here. I have a task to complete where I have to retrieve information from a DynamoDB using some information provided. 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.
Any sort of help would be greatly appreaciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,在
get_item()
请求中,密钥不仅是密钥的值,而应是带有密钥名称和值的地图。例如,如果您的hash-key属性称为“ p”,则键
您应该通过的是{'p':hash_key}
。其次,哈希键是桌子中的整个键吗?如果您也有一个排序键,则在
get_item()
中,还必须指定键的部分 - 结果是一个项目。如果您正在寻找具有特定哈希键但类别键的所有项目,则需要使用的功能是query()
,而不是get_item()
。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. For example, if your hash-key attribute is called "p", theKey
you should pass would be{'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. If you are looking for all the items with a particular hash key but different sort keys, then the function you need to use isquery()
, notget_item()
.