简体   繁体   English

使用boto扫描dynamodb表

[英]using boto to scan dynamodb table

Folks, I have an 'admins' table, with 'UserName' as its HashKey. 伙计们,我有一个'admins'表,其中'UserName'作为其HashKey。 The table looks like this: 该表如下所示:

admins = Table('admins')
admins.put_item(data={
  'UserName':'jon',
  'password':'pass1',
  })
admins.put_item(data={
  'UserName':'tom',
  'password':'pass2',
  })

So to pull the users out, I am trying to do the following, but failing: 因此,为了吸引用户,我尝试执行以下操作,但失败了:

admins = Table('admins')
all_admins = admins.scan()
for x in all_admins:
  print x['UserName']

Why am I getting an empty set? 为什么我会得到一个空集?

Thanks! 谢谢!

What you are doing looks correct. 您正在做什么看起来正确。

Have you confirmed the data actually got written?(take a look at the AWS console) Are you trying to read directly after writing? 您是否已确认实际写入的数据?(查看AWS控制台)您是否在尝试在写入后直接读取数据? The default read is eventually consistent, and thus you may not find items directly after writing them 默认读取最终是一致的,因此您在写入项目后可能无法直接找到它们

Use Item= instead of data= to insert the items, you can also use batch 使用Item=代替data=插入项目,也可以使用批处理

admins = Table('admins')
admins.put_item(Item={
               'UserName':'jon',
               'password':'pass1'})
admins.put_item(Item={
               'UserName':'tom',
               'password':'pass2'})

And to pull the users you should use 为了吸引用户,您应该使用

admins = Table('admins')
all_admins = admins.scan(
             ConsistentRead=True)
items = all_admins['Items']
for x in items:
  print x['UserName']

This should do the job 这应该做的工作

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

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