-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathNycTaxi_Producer_Desktop_JSON.py
78 lines (61 loc) · 1.62 KB
/
NycTaxi_Producer_Desktop_JSON.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import json
import csv
import boto3
# Set up
'''
1. Run 'pip install boto3' if you have not already
2. Install aws CLI - https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html
3. Run 'aws configure'
'''
# Make JSON from the yellow cab trip data CSV files
taxiCabRides = []
with open('../data/yellow_tripdata_2020-01.csv', encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
for rows in csvReader:
taxiCabRides.append(rows)
# print(rows)
# print(taxiCabRides[0])
# Sample JSON record
'''
{
'VendorID': '1',
'tpep_pickup_datetime': '2020-01-01 00:28:15',
'tpep_dropoff_datetime': '2020-01-01 00:33:03',
'passenger_count': '1',
'trip_distance': '1.20',
'RatecodeID': '1',
'store_and_fwd_flag': 'N',
'PULocationID': '238',
'DOLocationID': '239',
'payment_type': '1',
'fare_amount': '6',
'extra': '3',
'mta_tax': '0.5',
'tip_amount': '1.47',
'tolls_amount': '0',
'improvement_surcharge': '0.3',
'total_amount': '11.27',
'congestion_surcharge': '2.5'
}
'''
# Create a kinesis client
client = boto3.client('kinesis')
counter = 0
for ride in taxiCabRides:
# print(ride)
# print(json.dumps(ride))
# print(ride['VendorID'])
# print(ride['tpep_pickup_datetime'])
# Send message to Kinesis DataStream
response = client.put_record(
StreamName = "yellow-cab-trip",
Data = json.dumps(ride),
PartitionKey = str(hash(ride['tpep_pickup_datetime']))
)
counter = counter + 1
print('Message sent #' + str(counter))
# print(response)
# If the message was not sucssfully sent print an error message
if response['ResponseMetadata']['HTTPStatusCode'] != 200:
print('Error!')
print(response)