-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
45 lines (37 loc) · 1.37 KB
/
main.py
File metadata and controls
45 lines (37 loc) · 1.37 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
from os import walk, path
from kafka import KafkaProducer
from kafka.admin import KafkaAdminClient, NewTopic
from constants import KAFKA_TOPIC, INIT_PATH, BATCH_LIMIT
import json
def get_producer():
return KafkaProducer(bootstrap_servers=['localhost:9092'])
def produce_new_file(kafka_producer, batch_paths, partition_id):
data = {
"batch": batch_paths,
}
kafka_producer.send(KAFKA_TOPIC, bytes(json.dumps(data), 'utf-8'), partition=partition_id)
kafka_producer.flush()
def enumerate_files(init_path=None):
if not init_path:
raise ValueError("No init_path provided - or not available at .env")
for (root, dirnames, filenames) in walk(init_path):
for filename in filenames:
yield path.join(root, filename)
def main():
kafka_producer = get_producer()
counter = 0
batch_counter = 0
batch_paths = []
for path in enumerate_files(INIT_PATH):
batch_paths.append(path)
if batch_counter == BATCH_LIMIT:
# TODO make more then 2 partition in future
partition_id = 1 if bool(counter%2) else 0
print(f"PART: {partition_id}, BATCH: {batch_counter}")
produce_new_file(kafka_producer, batch_paths, partition_id)
counter += 1
batch_counter = 0
batch_paths = []
batch_counter +=1
if __name__ == '__main__':
main()