-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer.py
More file actions
64 lines (39 loc) · 1.58 KB
/
consumer.py
File metadata and controls
64 lines (39 loc) · 1.58 KB
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
#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 14 08:25:07 2020
@author: prade
"""
from kafka import KafkaConsumer
from pymongo import MongoClient
from json import loads
import json
import pymongo
print("Consumer starting")
consumer = KafkaConsumer(
'astronauts_data',
bootstrap_servers=['localhost:9092'],
auto_offset_reset='latest',
enable_auto_commit=True,
value_deserializer=lambda x: json.loads(x.decode('ascii')))
print("Consumer is up!")
try:
print("Connecting to MongoDB")
""" Atlas """
"""for some reason choose VERSION: 3.4.0 for credentials or later on atlas"""
#client = pymongo.MongoClient("mongodb://admin:admin@mongodb-atlas-shard-00-00-afdbx.mongodb.net:27017,mongodb-atlas-shard-00-01-afdbx.mongodb.net:27017,mongodb-atlas-shard-00-02-afdbx.mongodb.net:27017/test?ssl=true&replicaSet=MongoDB-Atlas-shard-0&authSource=admin&retryWrites=true&w=majority")
client = MongoClient('mongodb://localhost:27017/')
print("Connection Estabilished")
except pymongo.errors.ConnectionFailure:
print("SERVER ISN'T AVAILABLE")
db_cluster = client['debugdata']
collection = db_cluster['debug5']
consumer.subscribe(['astronauts_data'])
for message in consumer:
print("Message value:",message.value)
try:
js = json.loads(message.value)
collection.insert_many(js)
print("added to database")
except json.decoder.JSONDecodeError:
print('Unable to decode: %s', js)