2. Kafka_Producer
Kafka Producer¶
Produces Current DateTime stamp with a random count continously unless stopped.
Copyright By PowCoder代写 加微信 powcoder
Produces to the topic Week8-Stream
# import statements
from time import sleep
from json import dumps
from kafka import KafkaProducer
import random
import datetime as dt
def publish_message(producer_instance, topic_name, key, value):
key_bytes = bytes(key, encoding=’utf-8′)
value_bytes = bytes(value, encoding=’utf-8′)
producer_instance.send(topic_name, key=key_bytes, value=value_bytes)
producer_instance.flush()
print(‘Message published successfully. Data: ‘ + str(data))
except Exception as ex:
print(‘Exception in publishing message.’)
print(str(ex))
def connect_kafka_producer():
_producer = None
_producer = KafkaProducer(bootstrap_servers=[‘localhost:9092’],
api_version=(0, 10))
except Exception as ex:
print(‘Exception while connecting Kafka.’)
print(str(ex))
return _producer
if __name__ == ‘__main__’:
topic = ‘Week9-Topic’
print(‘Publishing records..’)
producer = connect_kafka_producer()
while True:
data = str(dt.datetime.now().strftime(“%X”)) + ‘, ‘ + str(random.randrange(0,100))
publish_message(producer, topic, ‘parsed’, data)
Publishing records..
Message published successfully. Data: 14:51:05, 90
Message published successfully. Data: 14:51:06, 96
Message published successfully. Data: 14:51:07, 66
Message published successfully. Data: 14:51:08, 70
Message published successfully. Data: 14:51:09, 33
Message published successfully. Data: 14:51:10, 98
Message published successfully. Data: 14:51:11, 39
Message published successfully. Data: 14:51:12, 32
Message published successfully. Data: 14:51:13, 14
Message published successfully. Data: 14:51:14, 93
Message published successfully. Data: 14:51:15, 27
Message published successfully. Data: 14:51:16, 34
Message published successfully. Data: 14:51:17, 92
Message published successfully. Data: 14:51:18, 43
Message published successfully. Data: 14:51:19, 79
Message published successfully. Data: 14:51:20, 42
Message published successfully. Data: 14:51:21, 80
Message published successfully. Data: 14:51:22, 83
Message published successfully. Data: 14:51:23, 78
Message published successfully. Data: 14:51:24, 75
Message published successfully. Data: 14:51:25, 59
Message published successfully. Data: 14:51:26, 43
Message published successfully. Data: 14:51:27, 80
Message published successfully. Data: 14:51:28, 19
Message published successfully. Data: 14:51:29, 86
Message published successfully. Data: 14:51:30, 5
Message published successfully. Data: 14:51:31, 19
Message published successfully. Data: 14:51:32, 60
Message published successfully. Data: 14:51:33, 73
Message published successfully. Data: 14:51:34, 10
Message published successfully. Data: 14:51:35, 8
Message published successfully. Data: 14:51:36, 35
Message published successfully. Data: 14:51:37, 1
Message published successfully. Data: 14:51:38, 88
Message published successfully. Data: 14:51:39, 20
Message published successfully. Data: 14:51:40, 38
Message published successfully. Data: 14:51:41, 83
Message published successfully. Data: 14:51:42, 17
Message published successfully. Data: 14:51:43, 12
Message published successfully. Data: 14:51:44, 57
Message published successfully. Data: 14:51:45, 36
Message published successfully. Data: 14:51:47, 75
Message published successfully. Data: 14:51:48, 76
Message published successfully. Data: 14:51:49, 32
Message published successfully. Data: 14:51:50, 47
Message published successfully. Data: 14:51:51, 22
Message published successfully. Data: 14:51:52, 12
Message published successfully. Data: 14:51:53, 60
Message published successfully. Data: 14:51:54, 85
Message published successfully. Data: 14:51:55, 95
Message published successfully. Data: 14:51:56, 61
Message published successfully. Data: 14:51:57, 87
Message published successfully. Data: 14:51:58, 1
Message published successfully. Data: 14:52:00, 90
Message published successfully. Data: 14:52:01, 38
Message published successfully. Data: 14:52:02, 30
Message published successfully. Data: 14:52:03, 80
Message published successfully. Data: 14:52:04, 42
Message published successfully. Data: 14:52:05, 32
Message published successfully. Data: 14:52:06, 35
Message published successfully. Data: 14:52:07, 0
Message published successfully. Data: 14:52:08, 61
Message published successfully. Data: 14:52:09, 49
Message published successfully. Data: 14:52:10, 1
Message published successfully. Data: 14:52:11, 46
Message published successfully. Data: 14:52:13, 2
Message published successfully. Data: 14:52:14, 46
Message published successfully. Data: 14:52:15, 80
Message published successfully. Data: 14:52:16, 50
Message published successfully. Data: 14:52:18, 93
Message published successfully. Data: 14:52:19, 53
Message published successfully. Data: 14:52:20, 51
Message published successfully. Data: 14:52:21, 34
Message published successfully. Data: 14:52:22, 81
Message published successfully. Data: 14:52:23, 22
Message published successfully. Data: 14:52:24, 84
Message published successfully. Data: 14:52:25, 4
Message published successfully. Data: 14:52:26, 34
Message published successfully. Data: 14:52:27, 36
Message published successfully. Data: 14:52:28, 41
Message published successfully. Data: 14:52:29, 88
Message published successfully. Data: 14:52:30, 90
Message published successfully. Data: 14:52:31, 22
Message published successfully. Data: 14:52:32, 99
Message published successfully. Data: 14:52:33, 74
Message published successfully. Data: 14:52:34, 22
Message published successfully. Data: 14:52:35, 25
Message published successfully. Data: 14:52:36, 93
Message published successfully. Data: 14:52:37, 72
Message published successfully. Data: 14:52:38, 30
Message published successfully. Data: 14:52:39, 63
Message published successfully. Data: 14:52:40, 90
Message published successfully. Data: 14:52:41, 12
Message published successfully. Data: 14:52:42, 23
Message published successfully. Data: 14:52:43, 74
Message published successfully. Data: 14:52:44, 28
Message published successfully. Data: 14:52:45, 21
Message published successfully. Data: 14:52:46, 27
Message published successfully. Data: 14:52:47, 55
Message published successfully. Data: 14:52:48, 40
Message published successfully. Data: 14:52:49, 29
Message published successfully. Data: 14:52:50, 2
Message published successfully. Data: 14:52:51, 63
Message published successfully. Data: 14:52:52, 25
Message published successfully. Data: 14:52:53, 21
Message published successfully. Data: 14:52:54, 16
Message published successfully. Data: 14:52:55, 48
Message published successfully. Data: 14:52:56, 1
Message published successfully. Data: 14:52:57, 83
Message published successfully. Data: 14:52:58, 91
Message published successfully. Data: 14:52:59, 49
Message published successfully. Data: 14:53:00, 74
Message published successfully. Data: 14:53:01, 24
Message published successfully. Data: 14:53:02, 69
Message published successfully. Data: 14:53:03, 75
Message published successfully. Data: 14:53:04, 93
Message published successfully. Data: 14:53:05, 5
Message published successfully. Data: 14:53:06, 48
Message published successfully. Data: 14:53:07, 94
Message published successfully. Data: 14:53:08, 6
Message published successfully. Data: 14:53:09, 31
Message published successfully. Data: 14:53:10, 69
Message published successfully. Data: 14:53:11, 44
Message published successfully. Data: 14:53:12, 89
Message published successfully. Data: 14:53:13, 44
Message published successfully. Data: 14:53:14, 21
Message published successfully. Data: 14:53:15, 0
Message published successfully. Data: 14:53:16, 62
Message published successfully. Data: 14:53:17, 6
Message published successfully. Data: 14:53:18, 64
Message published successfully. Data: 14:53:19, 19
Message published successfully. Data: 14:53:20, 44
Message published successfully. Data: 14:53:21, 2
Message published successfully. Data: 14:53:22, 78
Message published successfully. Data: 14:53:23, 57
Message published successfully. Data: 14:53:24, 35
Message published successfully. Data: 14:53:25, 7
Message published successfully. Data: 14:53:26, 23
Message published successfully. Data: 14:53:27, 95
Message published successfully. Data: 14:53:28, 52
Message published successfully. Data: 14:53:29, 48
Message published successfully. Data: 14:53:30, 46
Message published successfully. Data: 14:53:31, 72
Message published successfully. Data: 14:53:32, 91
Message published successfully. Data: 14:53:33, 97
Message published successfully. Data: 14:53:34, 59
Message published successfully. Data: 14:53:35, 17
Message published successfully. Data: 14:53:36, 75
Message published successfully. Data: 14:53:37, 1
Message published successfully. Data: 14:53:38, 78
Message published successfully. Data: 14:53:40, 10
Message published successfully. Data: 14:53:41, 88
Message published successfully. Data: 14:53:42, 27
Message published successfully. Data: 14:53:46, 47
Message published successfully. Data: 14:54:12, 74
Message published successfully. Data: 14:54:13, 74
Message published successfully. Data: 14:54:14, 10
Message published successfully. Data: 14:54:15, 9
Message published successfully. Data: 14:54:16, 86
Message published successfully. Data: 14:54:17, 66
Message published successfully. Data: 14:54:18, 13
Message published successfully. Data: 14:54:19, 72
Message published successfully. Data: 14:54:20, 80
Message published successfully. Data: 14:54:21, 15
Message published successfully. Data: 14:54:22, 39
Message published successfully. Data: 14:54:23, 80
Message published successfully. Data: 14:54:24, 30
Message published successfully. Data: 14:54:25, 73
Message published successfully. Data: 14:54:26, 15
Message published successfully. Data: 14:54:27, 26
Message published successfully. Data: 14:54:28, 29
Message published successfully. Data: 14:54:29, 76
Message published successfully. Data: 14:54:30, 69
Message published successfully. Data: 14:54:31, 1
Message published successfully. Data: 14:54:32, 5
Message published successfully. Data: 14:54:34, 40
Message published successfully. Data: 14:54:35, 90
Message published successfully. Data: 14:54:36, 62
Message published successfully. Data: 14:54:37, 42
Message published successfully. Data: 14:54:38, 32
Message published successfully. Data: 14:54:39, 86
Message published successfully. Data: 14:54:40, 11
Message published successfully. Data: 14:54:41, 50
Message published successfully. Data: 14:54:42, 32
Message published successfully. Data: 14:54:43, 30
Message published successfully. Data: 14:54:44, 2
Message published successfully. Data: 14:54:45, 52
Message published successfully. Data: 14:54:46, 48
Message published successfully. Data: 14:54:47, 75
Message published successfully. Data: 14:54:48, 68
Message published successfully. Data: 14:54:49, 53
Message published successfully. Data: 14:54:50, 32
Message published successfully. Data: 14:54:51, 79
Message published successfully. Data: 14:54:52, 94
Message published successfully. Data: 14:54:53, 95
Message published successfully. Data: 14:54:54, 29
Message published successfully. Data: 14:54:55, 34
Message published successfully. Data: 14:54:56, 29
Message published successfully. Data: 14:54:57, 19
Message published successfully. Data: 14:54:58, 43
Message published successfully. Data: 14:54:59, 93
Message published successfully. Data: 14:55:00, 97
Message published successfully. Data: 14:55:01, 25
Message published successfully. Data: 14:55:02, 66
Message published successfully. Data: 14:55:03, 33
Message published successfully. Data: 14:55:04, 84
Message published successfully. Data: 14:55:05, 71
Message published successfully. Data: 14:55:06, 20
Message published successfully. Data: 14:55:07, 52
Message published successfully. Data: 14:55:08, 20
Message published successfully. Data: 14:55:09, 90
Message published successfully. Data: 14:55:10, 23
Message published successfully. Data: 14:55:11, 88
Message published successfully. Data: 14:55:12, 52
Message published successfully. Data: 14:55:13, 83
Message published successfully. Data: 14:55:14, 55
Message published successfully. Data: 14:55:15, 56
Message published successfully. Data: 14:55:16, 33
Message published successfully. Data: 14:55:17, 29
Message published successfully. Data: 14:55:18, 86
Message published successfully. Data: 14:55:19, 97
Message published successfully. Data: 14:55:20, 9
Message published successfully. Data: 14:55:21, 91
Message published successfully. Data: 14:55:22, 65
Message published successfully. Data: 14:55:23, 37
Message published successfully. Data: 14:55:24, 75
Message published successfully. Data: 14:55:25, 22
Message published successfully. Data: 14:55:26, 60
Message published successfully. Data: 14:55:27, 64
Message published successfully. Data: 14:55:28, 95
Message published successfully. Data: 14:55:29, 65
Message published successfully. Data: 14:55:30, 77
Message published successfully. Data: 14:55:31, 36
Message published successfully. Data: 14:55:32, 44
Message published successfully. Data: 14:55:33, 31
Message published successfully. Data: 14:55:34, 20
Message published successfully. Data: 14:55:35, 51
Message published successfully. Data: 14:55:36, 89
Message published successfully. Data: 14:55:37, 46
Message published successfully. Data: 14:55:38, 39
Message published successfully. Data: 14:55:39, 84
Message published successfully. Data: 14:55:40, 71
Message published successfully. Data: 14:55:41, 42
Message published successfully. Data: 14:55:42, 45
Message published successfully. Data: 14:55:43, 4
Message published successfully. Data: 14:55:44, 62
Message published successfully. Data: 14:55:45, 67
Message published successfully. Data: 14:55:46, 13
Message published successfully. Data: 14:55:47, 26
Message published successfully. Data: 14:55:48, 42
Message published successfully. Data: 14:55:49, 47
Message published successfully. Data: 14:55:50, 12
Message published successfully. Data: 14:55:51, 90
Message published successfully. Data: 14:55:52, 61
Message published successfully. Data: 14:55:53, 63
Message published successfully. Data: 14:55:54, 37
Message published successfully. Data: 14:55:55, 69
Message published successfully. Data: 14:55:56, 51
Message published successfully. Data: 14:55:57, 92
Message published successfully. Data: 14:55:58, 96
Message published successfully. Data: 14:55:59, 48
Message published successfully. Data: 14:56:00, 50
Message published successfully. Data: 14:56:01, 71
Message published successfully. Data: 14:56:02, 96
Message published successfully. Data: 14:56:03, 27
Message published successfully. Data: 14:56:04, 75
Message published successfully. Data: 14:56:05, 22
Message published successfully. Data: 14:56:06, 65
Message published successfully. Data: 14:56:07, 14
Message published successfully. Data: 14:56:08, 79
Message published successfully. Data: 14:56:09, 49
Message published successfully. Data: 14:56:10, 26
Message published successfully. Data: 14:56:11, 39
Message published successfully. Data: 14:56:12, 44
Message published successfully. Data: 14:56:13, 44
Message published successfully. Data: 14:56:14, 36
Message published successfully. Data: 14:56:15, 1
Message published successfully. Data: 14:56:16, 61
Message published successfully. Data: 14:56:17, 71
Message published successfully. Data: 14:56:18, 83
Message published successfully. Data: 14:56:19, 63
Message published successfully. Data: 14:56:20, 87
Message published successfully. Data: 14:56:21, 88
Message published successfully. Data: 14:56:22, 44
Message published successfully. Data: 14:56:23, 51
Message published successfully. Data: 14:56:24, 84
Message published successfully. Data: 14:56:25, 55
Message published successfully. Data: 14:56:26, 30
Message published successfully. Data: 14:56:27, 55
Message published successfully. Data: 14:56:28, 63
Message published successfully. Data: 14:56:29, 65
Message published successfully. Data: 14:56:30, 85
Message published successfully. Data: 14:56:31, 55
Message published successfully. Data: 14:56:32, 64
Message published successfully. Data: 14:56:33, 10
Message published successfully. Data: 14:56:34, 33
Message published successfully. Data: 14:56:35, 27
Message published successfully. Data: 14:56:36, 43
Message published successfully. Data: 14:56:37, 12
Message published successfully. Data: 14:56:38, 28
Message published successfully. Data: 14:56:39, 84
Message published successfully. Data: 14:56:40, 2
Message published successfully. Data: 14:56:41, 9
Message published successfully. Data: 14:56:42, 74
Message published successfully. Data: 14:56:43, 52
Message published successfully. Data: 14:56:44, 13
Message published successfully. Data: 14:56:45, 88
Message published successfully. Data: 14:56:46, 99
Message published successfully. Data: 14:56:47, 99
Message published successfully. Data: 14:56:48, 98
Message published successfully. Data: 14:56:49, 90
Message published successfully. Data: 14:56:50, 28
Message published successfully. Data: 14:56:51, 62
Message published successfully. Data: 14:56:52, 88
Message published successfully. Data: 14:56:53, 38
Message published successfully. Data: 14:56:54, 13
Message published successfully. Data: 14:56:55, 62
Message published successfully. Data: 14:56:56, 14
Message published successfully. Data: 14:56:57, 63
Message published successfully. Data: 14:56:58, 80
Message published successfully. Data: 14:56:59, 54
Message published successfully. Data: 14:57:00, 92
Message published successfully. Data: 14:57:01, 37
Message published successfully. Data: 14:57:02, 73
Message published successfully. Data: 14:57:03, 0
Message published successfully. Data: 14:57:04, 30
Message published successfully. Data: 14:57:05, 76
Message published successfully. Data: 14:57:06, 54
Message published successfully. Data: 14:57:07, 69
Message published successfully. Data: 14:57:08, 47
Message published successfully. Data: 14:57:10, 58
Message published successfully. Data: 14:57:11, 7
Message published successfully. Data: 14:57:12, 89
Message published successfully. Data: 14:57:13, 65
Message published successfully. Data: 14:57:14, 77
Message published successfully. Data: 14:57:15, 94
Message published successfully. Data: 14:57:16, 37
Message published successfully. Data: 14:57:17, 16
Message published successfully. Data: 14:57:18, 26
Message published successfully. Data: 14:57:19, 40
Message published successfully. Data: 14:57:20, 58
Message published successfully. Data: 14:57:21, 51
Message published successfully. Data: 14:57:22, 70
Message published successfully. Data: 14:57:23, 15
Message published successfully. Data: 14:57:24, 61
Message published successfully. Data: 14:57:25, 19
Message published successfully. Data: 14:57:26, 96
Message published successfully. Data: 14:57:27, 44
Message published successfully. Data: 14:57:28, 42
—————————————————————————
KeyboardInterrupt Traceback (most recent call last)
39 data = str(dt.datetime.now().strftime(“%X”)) + ‘, ‘ + str(random.randrange(0,100))
40 publish_message(producer, topic, ‘parsed’, data)
—> 41 sleep(1)
KeyboardInterrupt:
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com