-
Notifications
You must be signed in to change notification settings - Fork 9
/
kinesis-dynamodb.py
executable file
·56 lines (51 loc) · 2.13 KB
/
kinesis-dynamodb.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
53
54
55
56
#!/usr/bin/env python
import boto3
import time
import json
import decimal
## script to read data from Kinesis, extract hashtags and store into dynamoDB
#Connent to the kinesis stream
inuser = boto3.session.Session(profile_name='default')
kinesis = inuser.client("kinesis")
shard_id = 'shardId-000000000000' #only one shard
shard_it = kinesis.get_shard_iterator(StreamName="test-twitter-stream", ShardId=shard_id, ShardIteratorType="LATEST")["ShardIterator"]
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('hashtags')
while 1==1:
out = kinesis.get_records(ShardIterator=shard_it, Limit=100)
for record in out['Records']:
if 'entities' in json.loads(record['Data']):
htags = json.loads(record['Data'])['entities']['hashtags']
if htags:
for ht in htags:
htag = ht['text']
checkItemExists = table.get_item(
Key={
'hashtag':htag
}
)
if 'Item' in checkItemExists:
response = table.update_item(
Key={
'hashtag': htag
},
UpdateExpression="set htCount = htCount + :val",
ConditionExpression="attribute_exists(hashtag)",
ExpressionAttributeValues={
':val': decimal.Decimal(1)
},
ReturnValues="UPDATED_NEW"
)
else:
response = table.update_item(
Key={
'hashtag': htag
},
UpdateExpression="set htCount = :val",
ExpressionAttributeValues={
':val': decimal.Decimal(1)
},
ReturnValues="UPDATED_NEW"
)
shard_it = out["NextShardIterator"]
time.sleep(1.0)