Repository navigation
Expand file tree
/
Copy pathkafka_admin.py
More file actions
56 lines (45 loc) · 1.53 KB
/
Copy pathkafka_admin.py
File metadata and controls
56 lines (45 loc) · 1.53 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
'''
Kafka Admin
'''
import time
from confluent_kafka.admin import AdminClient, NewTopic
from logger import log
class KafkaAdminService:
'''
admin
'''
def __init__(self, bootstrap_server: str) -> None:
self.__admin = AdminClient({
'bootstrap.servers': bootstrap_server
})
def create_topic(self, topic: str, partitions: int = 4, recreate: bool = True) -> None:
'''
create topic
'''
if topic in self.__admin.list_topics().topics:
log.info("Topic '%s' already exists.", topic)
if recreate:
log.info("Recreating topic '%s'...", topic)
self.__admin.delete_topics([topic])
count = 5
while count > 0:
if topic not in self.__admin.list_topics().topics:
break
log.info("Waiting for topic '%s' to be deleted...", topic)
count -= 1
time.sleep(1)
else:
return
new_topic = NewTopic(topic, partitions, config={
'compression.type': 'lz4',
'retention.bytes': '1000000000' # 1GB per partition
})
self.__admin.create_topics([new_topic])
count = 5
while count > 0:
if topic in self.__admin.list_topics().topics:
break
log.info("Waiting for topic '%s' to be created...", topic)
count -= 1
time.sleep(1)
log.info("Topic '%s' created.", topic)