dynamodb row count via python, boto query

半城伤御伤魂 提交于 2019-12-06 16:01:23

问题


Folks, Am trying to get the following bit of code working to return the row count in a table:

import boto
import boto.dynamodb2
from boto.dynamodb2.table import Table
from boto.dynamodb2.fields import HashKey, RangeKey

drivers = Table('current_fhv_drivers')
rowcountquery = drivers.query(
   number = 'blah',
   expiration = 'foo',
   count=True,
  )
for x in rowcountquery:
 print x['Count']

Error I see is:

boto.dynamodb2.exceptions.UnknownFilterTypeError: Operator 'count' from 'count' is not recognized.

Whats the correct syntaxt to get row count :)

Thanks!


回答1:


That exception is basically telling you that the operator 'count' is not recognized by boto.

If you read the second paragraph on http://boto.readthedocs.org/en/latest/dynamodb2_tut.html#querying you'll see that:

Filter parameters are passed as kwargs & use a __ to separate the fieldname from the operator being used to filter the value.

So I would change your code to:

import boto
import boto.dynamodb2
from boto.dynamodb2.table import Table
from boto.dynamodb2.fields import HashKey, RangeKey

drivers = Table('current_fhv_drivers')
rowcountquery = drivers.query(
   number__eq = 'blah',
   expiration__eq = 'foo',
   count__eq = True,
  )
for x in rowcountquery:
 print x['Count']



回答2:


from boto.dynamodb2.table import Table
table = Table('current_fhv_drivers')
print(table.query_count(number__eq = 'blah', expiration__eq = 'foo'))


来源:https://stackoverflow.com/questions/18968012/dynamodb-row-count-via-python-boto-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!