66import time
77from kafka import KafkaProducer
88from kafka .errors import KafkaError
9- from json_generator import generate_random_json
9+
10+ import random
11+ import string
12+ from datetime import datetime
13+
14+ def generate_random_json (min_size_kb = 1 ):
15+ """Generate a random JSON object of at least min_size_kb size"""
16+ base_data = {
17+ "timestamp" : datetime .now ().isoformat (),
18+ "event_id" : f"evt_{ random .randint (100000 , 999999 )} " ,
19+ "user_id" : f"user_{ random .randint (1000 , 9999 )} " ,
20+ "session_id" : f"session_{ random .randint (10000 , 99999 )} " ,
21+ "event_type" : random .choice (["click" , "view" , "purchase" , "login" , "logout" ]),
22+ "device_type" : random .choice (["mobile" , "desktop" , "tablet" ]),
23+ "os" : random .choice (["Windows" , "macOS" , "Linux" , "iOS" , "Android" ]),
24+ "browser" : random .choice (["Chrome" , "Firefox" , "Safari" , "Edge" ]),
25+ "country" : random .choice (["US" , "UK" , "DE" , "FR" , "JP" , "BR" , "IN" ]),
26+ "metrics" : {
27+ "load_time" : round (random .uniform (0.1 , 5.0 ), 3 ),
28+ "response_time" : round (random .uniform (0.01 , 1.0 ), 3 ),
29+ "cpu_usage" : round (random .uniform (0 , 100 ), 2 ),
30+ "memory_usage" : round (random .uniform (0 , 100 ), 2 )
31+ }
32+ }
33+
34+ # Calculate current size
35+ current_json = json .dumps (base_data )
36+ current_size = len (current_json .encode ('utf-8' ))
37+ target_size = min_size_kb * 1024
38+
39+ # Add padding data if needed to reach target size
40+ if current_size < target_size :
41+ padding_size = target_size - current_size
42+ # Generate random string data for padding
43+ padding_data = {
44+ "additional_data" : {
45+ f"field_{ i } " : '' .join (random .choices (string .ascii_letters + string .digits , k = 50 ))
46+ for i in range (padding_size // 50 )
47+ }
48+ }
49+ base_data .update (padding_data )
50+
51+ return base_data
1052
1153def create_producer (client_id ):
1254 """Create and configure Kafka producer"""
@@ -19,7 +61,7 @@ def create_producer(client_id):
1961 value_serializer = lambda v : json .dumps (v ).encode ('utf-8' ),
2062 )
2163
22- def send_messages_to_topics (producer , topics , producer_name , num_messages = 50 ):
64+ def send_messages_to_topics (producer , topics , producer_name , num_messages = 5 ):
2365 """Send random JSON messages to specified Kafka topics"""
2466
2567 successful = 0
@@ -57,30 +99,28 @@ def main():
5799 try :
58100 # Create two separate producers
59101 producer1 = create_producer ('kafka-python-producer-1' )
60- producer2 = create_producer ('kafka-python-producer-2' )
102+ # producer2 = create_producer('kafka-python-producer-2')
61103
62104 # First producer sends to test-topic and test-topic-1
63105 topics1 = ['test-topic' , 'test-topic-1' ]
64106 send_messages_to_topics (producer1 , topics1 , 'kafka-python-producer-1' )
65-
107+ # Sleep for 10 minutes at the end
108+ print ("Sleeping for 10 minutes..." )
109+ time .sleep (600 )
110+ print ("Sleep completed" )
66111 # Second producer sends to test-topic-2 and test-topic-3
67112 topics2 = ['test-topic-2' , 'test-topic-3' ]
68- send_messages_to_topics (producer2 , topics2 , 'kafka-python-producer-2' )
113+ # send_messages_to_topics(producer2, topics2, 'kafka-python-producer-2')
69114
70115 except Exception as e :
71116 print (f"Error: { e } " )
72117 finally :
73118 if producer1 :
74119 producer1 .close ()
75120 print ("Producer 1 closed" )
76- if producer2 :
77- producer2 .close ()
78- print ("Producer 2 closed" )
79-
80- # Sleep for 10 minutes at the end
81- print ("Sleeping for 10 minutes..." )
82- time .sleep (600 )
83- print ("Sleep completed" )
121+ # if producer2:
122+ # producer2.close()
123+ # print("Producer 2 closed")
84124
85125if __name__ == "__main__" :
86126 main ()
0 commit comments