简体   繁体   中英

using boto to scan dynamodb table

Folks, I have an 'admins' table, with 'UserName' as its 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? 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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