-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCKafka_to_Elastic.py
More file actions
50 lines (42 loc) · 1.16 KB
/
CKafka_to_Elastic.py
File metadata and controls
50 lines (42 loc) · 1.16 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
from confluent_kafka import Consumer, KafkaException
from elasticsearch import Elasticsearch
import json
client = Elasticsearch(
"https:abc",
api_key="NA"
)
kafka_config = {
'bootstrap.servers': '####',
'group.id': 'fraud_card',
'security.protocol': 'SASL_SSL',
'sasl.mechanisms': 'PLAIN',
'sasl.username': '###',
'sasl.password': '####'
}
# Create Kafka consumer
consumer = Consumer(kafka_config)
consumer.subscribe(['abc'])
def index_document(doc):
try:
res = client.index(index="abc", body=doc)
print(res)
except Exception as e:
print("Failed to index document: ", e)
try:
while True:
msg = consumer.poll(timeout=1.0)
if msg is None:
continue
if msg.error():
if msg.error().code() == KafkaException._PARTITION_EOF:
continue
else:
print(msg.error())
break
print('Received message: {}'.format(msg.value().decode('utf-8')))
document = json.loads(msg.value().decode('utf-8'))
index_document(document)
except KeyboardInterrupt:
pass
finally:
consumer.close()