12
12
from metricproducer import MetricProvider
13
13
from userbets import UserBetsProvider
14
14
from rolling import RollingProvider
15
+ from metricadvancedproducer import MetricAdvancedProvider
15
16
16
17
17
18
MAX_NUMBER_PIZZAS_IN_ORDER = 10
18
19
MAX_ADDITIONAL_TOPPINGS_IN_PIZZA = 5
19
20
20
21
21
- # Creating a Faker instance and seeding to have the same results every time we execute the script
22
+ # Creating a Faker instance and seeding to have the
23
+ # same results every time we execute the script
22
24
fake = Faker ()
23
25
Faker .seed (4321 )
24
26
25
27
26
28
# function produce_msgs starts producing messages with Faker
27
- def produce_msgs (security_protocol = 'SSL' ,
28
- sasl_mechanism = 'SCRAM-SHA-256' ,
29
- cert_folder = '~/kafka-pizza/' ,
30
- username = '' ,
31
- password = '' ,
32
- hostname = 'hostname' ,
33
- port = '1234' ,
34
- topic_name = 'pizza-orders' ,
35
- nr_messages = - 1 ,
36
- max_waiting_time_in_sec = 5 ,
37
- subject = 'pizza' ):
38
- if security_protocol .upper () == 'PLAINTEXT' :
29
+ def produce_msgs (
30
+ security_protocol = "SSL" ,
31
+ sasl_mechanism = "SCRAM-SHA-256" ,
32
+ cert_folder = "~/kafka-pizza/" ,
33
+ username = "" ,
34
+ password = "" ,
35
+ hostname = "hostname" ,
36
+ port = "1234" ,
37
+ topic_name = "pizza-orders" ,
38
+ nr_messages = - 1 ,
39
+ max_waiting_time_in_sec = 5 ,
40
+ subject = "pizza" ,
41
+ ):
42
+ if security_protocol .upper () == "PLAINTEXT" :
39
43
producer = KafkaProducer (
40
- bootstrap_servers = hostname + ':' + port ,
41
- security_protocol = ' PLAINTEXT' ,
42
- value_serializer = lambda v : json .dumps (v ).encode (' ascii' ),
43
- key_serializer = lambda v : json .dumps (v ).encode (' ascii' )
44
+ bootstrap_servers = hostname + ":" + port ,
45
+ security_protocol = " PLAINTEXT" ,
46
+ value_serializer = lambda v : json .dumps (v ).encode (" ascii" ),
47
+ key_serializer = lambda v : json .dumps (v ).encode (" ascii" ),
44
48
)
45
- elif security_protocol .upper () == ' SSL' :
49
+ elif security_protocol .upper () == " SSL" :
46
50
producer = KafkaProducer (
47
- bootstrap_servers = hostname + ':' + port ,
48
- security_protocol = ' SSL' ,
49
- ssl_cafile = cert_folder + ' /ca.pem' ,
50
- ssl_certfile = cert_folder + ' /service.cert' ,
51
- ssl_keyfile = cert_folder + ' /service.key' ,
52
- value_serializer = lambda v : json .dumps (v ).encode (' ascii' ),
53
- key_serializer = lambda v : json .dumps (v ).encode (' ascii' )
51
+ bootstrap_servers = hostname + ":" + port ,
52
+ security_protocol = " SSL" ,
53
+ ssl_cafile = cert_folder + " /ca.pem" ,
54
+ ssl_certfile = cert_folder + " /service.cert" ,
55
+ ssl_keyfile = cert_folder + " /service.key" ,
56
+ value_serializer = lambda v : json .dumps (v ).encode (" ascii" ),
57
+ key_serializer = lambda v : json .dumps (v ).encode (" ascii" ),
54
58
)
55
- elif security_protocol .upper () == ' SASL_SSL' :
59
+ elif security_protocol .upper () == " SASL_SSL" :
56
60
producer = KafkaProducer (
57
- bootstrap_servers = hostname + ':' + port ,
58
- security_protocol = ' SASL_SSL' ,
61
+ bootstrap_servers = hostname + ":" + port ,
62
+ security_protocol = " SASL_SSL" ,
59
63
sasl_mechanism = sasl_mechanism ,
60
- ssl_cafile = cert_folder + ' /ca.pem' if cert_folder else None ,
64
+ ssl_cafile = cert_folder + " /ca.pem" if cert_folder else None ,
61
65
sasl_plain_username = username ,
62
66
sasl_plain_password = password ,
63
- value_serializer = lambda v : json .dumps (v ).encode (' ascii' ),
64
- key_serializer = lambda v : json .dumps (v ).encode (' ascii' )
67
+ value_serializer = lambda v : json .dumps (v ).encode (" ascii" ),
68
+ key_serializer = lambda v : json .dumps (v ).encode (" ascii" ),
65
69
)
66
70
else :
67
71
sys .exit ("This security protocol is not supported!" )
68
72
69
73
if nr_messages <= 0 :
70
- nr_messages = float (' inf' )
74
+ nr_messages = float (" inf" )
71
75
i = 0
72
-
73
- if subject == ' stock' :
76
+
77
+ if subject == " stock" :
74
78
fake .add_provider (StockProvider )
75
- elif subject == ' realstock' :
79
+ elif subject == " realstock" :
76
80
fake .add_provider (RealStockProvider )
77
- elif subject == ' metric' :
81
+ elif subject == " metric" :
78
82
fake .add_provider (MetricProvider )
79
- elif subject == 'userbehaviour' :
83
+ elif subject == "advancedmetric" :
84
+ fake .add_provider (MetricAdvancedProvider )
85
+ elif subject == "userbehaviour" :
80
86
fake .add_provider (UserBehaviorProvider )
81
- elif subject == ' bet' :
87
+ elif subject == " bet" :
82
88
fake .add_provider (UserBetsProvider )
83
- elif subject == ' rolling' :
89
+ elif subject == " rolling" :
84
90
fake .add_provider (RollingProvider )
85
91
else :
86
92
fake .add_provider (PizzaProvider )
87
93
while i < nr_messages :
88
- if subject in ['stock' , 'userbehaviour' , 'realstock' , 'metric' , 'bet' , 'rolling' ]:
94
+ if subject in [
95
+ "stock" ,
96
+ "userbehaviour" ,
97
+ "realstock" ,
98
+ "metric" ,
99
+ "bet" ,
100
+ "rolling" ,
101
+ "advancedmetric" ,
102
+ ]:
89
103
message , key = fake .produce_msg ()
90
104
else :
91
- message , key = fake .produce_msg (fake , i , MAX_NUMBER_PIZZAS_IN_ORDER , MAX_ADDITIONAL_TOPPINGS_IN_PIZZA )
105
+ message , key = fake .produce_msg (
106
+ fake ,
107
+ i ,
108
+ MAX_NUMBER_PIZZAS_IN_ORDER ,
109
+ MAX_ADDITIONAL_TOPPINGS_IN_PIZZA ,
110
+ )
92
111
93
- print (' Sending: {}' .format (message ))
112
+ print (" Sending: {}" .format (message ))
94
113
# sending the message to Kafka
95
- producer .send (topic_name ,
96
- key = key ,
97
- value = message )
114
+ producer .send (topic_name , key = key , value = message )
98
115
# Sleeping time
99
- sleep_time = random .randint (0 , int (max_waiting_time_in_sec * 10000 ))/ 10000
100
- print ('Sleeping for...' + str (sleep_time )+ 's' )
116
+ sleep_time = (
117
+ random .randint (0 , int (max_waiting_time_in_sec * 10000 )) / 10000
118
+ )
119
+ print ("Sleeping for..." + str (sleep_time ) + "s" )
101
120
time .sleep (sleep_time )
102
121
103
122
# Force flushing of all messages
@@ -106,23 +125,65 @@ def produce_msgs(security_protocol='SSL',
106
125
i = i + 1
107
126
producer .flush ()
108
127
128
+
109
129
# calling the main produce_msgs function: parameters are:
110
130
# * nr_messages: number of messages to produce
111
131
# * max_waiting_time_in_sec: maximum waiting time in sec between messages
112
132
133
+
113
134
def main ():
114
135
parser = argparse .ArgumentParser ()
115
- parser .add_argument ('--security-protocol' , help = 'Security protocol for Kafka (PLAINTEXT, SSL, SASL_SSL)' , required = True )
116
- parser .add_argument ('--sasl-mechanism' , help = 'SASL mechanism for Kafka (PLAIN, GSSAPI, OAUTHBEARER, SCRAM-SHA-256, SCRAM-SHA-512)' , required = False )
117
- parser .add_argument ('--cert-folder' , help = 'Path to folder containing required Kafka certificates. Required --security-protocol equal SSL or SASL_SSL' , required = False )
118
- parser .add_argument ('--username' , help = 'Username. Required if security-protocol is SASL_SSL' , required = False )
119
- parser .add_argument ('--password' , help = 'Password. Required if security-protocol is SASL_SSL' , required = False )
120
- parser .add_argument ('--host' , help = 'Kafka Host (obtained from Aiven console)' , required = True )
121
- parser .add_argument ('--port' , help = 'Kafka Port (obtained from Aiven console)' , required = True )
122
- parser .add_argument ('--topic-name' , help = 'Topic Name' , required = True )
123
- parser .add_argument ('--nr-messages' , help = 'Number of messages to produce (0 for unlimited)' , required = True )
124
- parser .add_argument ('--max-waiting-time' , help = 'Max waiting time between messages (0 for none)' , required = True )
125
- parser .add_argument ('--subject' , help = 'What type of content to produce (possible choices are [pizza, userbehaviour, stock, realstock, metric] pizza is the default' , required = False )
136
+ parser .add_argument (
137
+ "--security-protocol" ,
138
+ help = "Security protocol for Kafka (PLAINTEXT, SSL, SASL_SSL)" ,
139
+ required = True ,
140
+ )
141
+ parser .add_argument (
142
+ "--sasl-mechanism" ,
143
+ help = "SASL mechanism for Kafka (PLAIN, GSSAPI, OAUTHBEARER, SCRAM-SHA-256, SCRAM-SHA-512)" ,
144
+ required = False ,
145
+ )
146
+ parser .add_argument (
147
+ "--cert-folder" ,
148
+ help = "Path to folder containing required Kafka certificates. Required --security-protocol equal SSL or SASL_SSL" ,
149
+ required = False ,
150
+ )
151
+ parser .add_argument (
152
+ "--username" ,
153
+ help = "Username. Required if security-protocol is SASL_SSL" ,
154
+ required = False ,
155
+ )
156
+ parser .add_argument (
157
+ "--password" ,
158
+ help = "Password. Required if security-protocol is SASL_SSL" ,
159
+ required = False ,
160
+ )
161
+ parser .add_argument (
162
+ "--host" ,
163
+ help = "Kafka Host (obtained from Aiven console)" ,
164
+ required = True ,
165
+ )
166
+ parser .add_argument (
167
+ "--port" ,
168
+ help = "Kafka Port (obtained from Aiven console)" ,
169
+ required = True ,
170
+ )
171
+ parser .add_argument ("--topic-name" , help = "Topic Name" , required = True )
172
+ parser .add_argument (
173
+ "--nr-messages" ,
174
+ help = "Number of messages to produce (0 for unlimited)" ,
175
+ required = True ,
176
+ )
177
+ parser .add_argument (
178
+ "--max-waiting-time" ,
179
+ help = "Max waiting time between messages (0 for none)" ,
180
+ required = True ,
181
+ )
182
+ parser .add_argument (
183
+ "--subject" ,
184
+ help = "What type of content to produce (possible choices are [pizza, userbehaviour, stock, realstock, metric, advancedmetric] pizza is the default" ,
185
+ required = False ,
186
+ )
126
187
args = parser .parse_args ()
127
188
p_security_protocol = args .security_protocol
128
189
p_cert_folder = args .cert_folder
@@ -131,21 +192,23 @@ def main():
131
192
p_sasl_mechanism = args .sasl_mechanism
132
193
p_hostname = args .host
133
194
p_port = args .port
134
- p_topic_name = args .topic_name
195
+ p_topic_name = args .topic_name
135
196
p_subject = args .subject
136
- produce_msgs (security_protocol = p_security_protocol ,
137
- cert_folder = p_cert_folder ,
138
- username = p_username ,
139
- password = p_password ,
140
- hostname = p_hostname ,
141
- port = p_port ,
142
- topic_name = p_topic_name ,
143
- nr_messages = int (args .nr_messages ),
144
- max_waiting_time_in_sec = float (args .max_waiting_time ),
145
- subject = p_subject ,
146
- sasl_mechanism = p_sasl_mechanism ,
147
- )
197
+ produce_msgs (
198
+ security_protocol = p_security_protocol ,
199
+ cert_folder = p_cert_folder ,
200
+ username = p_username ,
201
+ password = p_password ,
202
+ hostname = p_hostname ,
203
+ port = p_port ,
204
+ topic_name = p_topic_name ,
205
+ nr_messages = int (args .nr_messages ),
206
+ max_waiting_time_in_sec = float (args .max_waiting_time ),
207
+ subject = p_subject ,
208
+ sasl_mechanism = p_sasl_mechanism ,
209
+ )
148
210
print (args .nr_messages )
149
211
150
- if __name__ == '__main__' :
212
+
213
+ if __name__ == "__main__" :
151
214
main ()
0 commit comments