-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb-tests.py
52 lines (42 loc) · 1.51 KB
/
db-tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import boto3
from boto3.dynamodb.conditions import Attr, Key
import logging
dynamo = boto3.resource('dynamodb')
tb = dynamo.Table('test2')
def scan_key_with_filter(db, key, filter_expression, items=1000):
log = logging.getLogger(__name__)
resultlist = []
retritem = 0
scanneditem = 0
consumedcapacity = 0
lastkey = None
while retritem < items:
if lastkey != None:
response = db.scan(
ProjectionExpression=key,
FilterExpression=filter_expression,
ReturnConsumedCapacity='TOTAL',
ExclusiveStartKey=lastkey
)
if lastkey == None:
response = db.scan(
ProjectionExpression=key,
FilterExpression=filter_expression,
ReturnConsumedCapacity='TOTAL'
)
resultlist.extend(response['Items'])
retritem += response['Count']
scanneditem += response['ScannedCount']
consumedcapacity += response['ConsumedCapacity']['CapacityUnits']
log.info('%s out of %s DB items received', retritem, scanneditem)
try:
lastkey = response['LastEvaluatedKey']
except:
log.info('After %s scanned itmens. No more last keys', scanneditem)
break
return resultlist
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
filterexp = Key('commenters_count').gt(30)
finale = scan_key_with_filter(tb, 'shortcode', filterexp, items=400)
print(finale)