简体   繁体   中英

Correct Boto aws DynamoDb .scan syntax

I've been working with Dynamo & Python for a little while now however I've run into what appears to be mismatch in syntax.

I read:

# All results.
>>> everything = users.scan()

# Look for last names beginning with "D".
>>> results = users.scan(last_name__beginswith='D')
>>> for res in results:
...     print res['first_name']
'Alice'
'John'
'Jane'

# Use an ``IN`` filter & limit.
>>> results = users.scan(
...     age__in=[25, 26, 27, 28, 29],
...     limit=1
... )
>>> for res in results:
...     print res['first_name']
'Alice'

From: http://boto.readthedocs.org/en/latest/ref/dynamodb2.html Which seems to agree with the scan function in boto table: https://github.com/boto/boto/blob/433f211b5eb93560916a4bd4a1dbf905e6c13a58/boto/dynamodb2/table.py

The issue lies in that when I try:

def getByAdvertiser(adv):
    matchingTable=swfTable.scan(advertiser__eq=adv)
    return getTableElements(matchingTable)
def getTableElements(table):
    res=[]
    for t in table:
        res.append(t)
    return res

Which based on the above syntax should work given that swfTable is a valid table, .scan() returns elements, that "advertiser" is a dynamo table column, and there exists at least one element in "advertiser" that is equal to adv ("itunes.apple.com"). I get the following error however:

Traceback (most recent call last): File "/Users/tai/Documents/workspace/testSelenium/testS/ init .py", line 101, in forInFile() File "/Users/tai/Documents/workspace/testSelenium/testS/ init .py", line 95, in forInFile dynamoAccess.getByAdvertiser("itunes.apple.com") File "/Users/tai/Documents/workspace/testSelenium/testS/dynamoAccess.py", line 34, in getByAdvertiser matchingTable=swfTable.scan(advertiser__eq=adv) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/boto/dynamodb/table.py", line 518, in scan return self.layer2.scan(self, *args, **kw) TypeError: scan() got an unexpected keyword argument 'advertiser__eq'

I don't see how I'm not following the documentation's syntax.

However when I look at other Boto dynamodb questions they use syntax such as:

results = self.table.scan(scan_filter={'asset': dynamodb.condition.EQ(asset)})

from:

Trouble getting a dynamodb scan to work with boto

Or:

all_query = table.scan(attributes_to_get=['something'])

From: boto python dynamodb scan attributes_to_get

Which looks nothing like what I'm using or anything that I've seen documented.

EDIT:

I believe the issue may be that I have been using dynamodb1 instead of 2.

aws_dynamo_table="decompiled_swf_text"
conn= S3Connection(aws_access_key_id,aws_secret_access_key);
dynamoConn = boto.connect_dynamodb(aws_access_key_id, aws_secret_access_key)
dTable = dynamoConn.get_table(aws_dynamo_table)

Would this be solved by using dynamodb2? If so how can I set that up? I'm trying:

dynamoConn = dynamodb2.layer1.DynamoDBConnection(region=RegionInfo(name=aws_dynamo_region,endpoint='dynamodb.us-east-1.amazonaws.com'),aws_access_key_id,aws_secret_access_key)

However I don't see how I can then query and scan tables... I don't see an available function.

The two examples that you found are both using traditional/low-level API . for example in the old API, the scan function will take a scanFilter as a parameter.

And syntax like

>>> results = users.scan(
...     age__in=[25, 26, 27, 28, 29],
...     limit=1
... )

is defined in new/high level API .

You should definitely switch to DynamoDB2. from your error output

in scan return self.layer2.scan(self, *args, **kw) TypeError:

it seems you were using DynamoDB instead of DynamoDB2 because layer2 API is defined in DynamoDB api and it goes away in DynamoDB2.

So switch boto DynamoDB2

1) get your table

2) use table.scan(age__in=[25, 26, 27, 28, 29], limit=1) see here

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