2012-09-30 28 views
3

我正在使用boto訪問dynamodb表。直到我嘗試執行掃描操作時,一切進展順利。無法通過boto使用dynamodb掃描

我試過一對夫婦,我發現互聯網經過反覆搜索語法,但沒有運氣:

def scanAssets(self, asset): 
    results = self.table.scan({('asset', 'EQ', asset)}) 
     -or- 
    results = self.table.scan(scan_filter={'asset':boto.dynamodb.condition.EQ(asset)}) 

我掃描被稱爲「資產」的屬性,以及資產是一個字符串。

奇怪的是table.scan呼叫最終總是通過此功能會:

def dynamize_scan_filter(self, scan_filter): 
    """ 
    Convert a layer2 scan_filter parameter into the 
    structure required by Layer1. 
    """ 
    d = None 
    if scan_filter: 
     d = {} 
     for attr_name in scan_filter: 
      condition = scan_filter[attr_name] 
      d[attr_name] = condition.to_dict() 
    return d 

我不是一個Python的專家,但我看不出如何做到這一點的工作。即scan_filter必須通過此代碼的結構是什麼樣的?

同樣,也許我只是把它說錯了。有什麼建議麼?

+2

嗨吉姆·鮑德溫,歡迎堆棧溢出。在這個網站[它可以回答你自己的問題](http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/)。所以,不要編輯你的問題帖子,你可以把答案放在回答帖子中並接受它(通過勾選答案左側的複選標記)? – Sicco

回答

4

好吧,看起來像我有一個導入問題。簡單地使用:

import boto 

並指定boto.dynamodb.condition不會削減它。我不得不補充:

import dynamodb.condition 

獲取條件類型得到提取。我現在的工作代碼是:

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

不是我完全理解爲什麼,但它現在爲我工作。 :-)

0

或者你也可以做到這一點

exclusive_start_key = None 
while True: 
    result_set = self.table.scan(
     asset__eq=asset, # The scan filter is explicitly given here 
     max_page_size=100, # Number of entries per page 
     limit=100, 
     # You can divide the table by n segments so that processing can be done parallelly and quickly. 
     total_segments=number_of_segments, 
     segment=segment, # Specify which segment you want to process 
     exclusive_start_key=exclusive_start_key # To start for last key seen 
    ) 
    dynamodb_items = map(lambda item: item, result_set) 
    # Do something with your item, add it to a list for later processing when you come out of the while loop 
    exclusive_start_key = result_set._last_key_seen 
    if not exclusive_start_key: 
     break 

這適用於任何領域。

分割:假設你有上面的腳本在test.py

,你可以在不同的屏幕上運行的平行狀

python test.py --segment=0 --total_segments=4 
python test.py --segment=1 --total_segments=4 
python test.py --segment=2 --total_segments=4 
python test.py --segment=3 --total_segments=4 

+0

該代碼適用於boto.dynamodb2,即第2層 – theBuzzyCoder