pyspark-streaming实战

版本

Spark2.3.0[Using Scala version 2.11.8, Java HotSpot(TM) 64-Bit Server VM, 1.8.0_40]
Python 3.5.1
Flume 1.8.0
Kafka kafka_2.11-0.9.0.1

8个小类

  1. socket_wordcount
  2. stateful_wordcount to mysql
  3. filter data
  4. flume push-based
  5. flume pull-based
  6. kafka receiver-based not support in spark2.3.0 with kafka 0.10.0 or higher
  7. kafka direct
  8. 使用Structured Streaming【类flume source-sink】(file socket kafka)

一点总结

此处不在进行目录分类,全在代码中,本地运行调试即可,也是做一个API学习的记录。
未知只会更多,仍须探索学习应用。
代码用到的jar包会在注释中呈现,也包括官方文档的连接。
代码末是程序运行的控制台交互和结果输出。

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
from __future__ import print_function

import os

from pyspark import SparkContext
from pyspark.streaming import StreamingContext
from pyspark.sql import *
from pyspark.sql.functions import explode
from pyspark.sql.functions import split
from pyspark.sql.functions import window

# pyspark-streaming-action
# pip3.5 install mysql-connector-python
# mysql驱动jar包 /usr/local/env/lib2/mysql-connector-java-5.1.42.jar
# spark-defaults.conf spark.driver.extraClassPath /usr/local/env/lib2/*
import mysql.connector
from pyspark.streaming.flume import FlumeUtils
from pyspark.streaming.kafka import KafkaUtils

# Spark2.3.0[Using Scala version 2.11.8, Java HotSpot(TM) 64-Bit Server VM, 1.8.0_40]
# Python 3.5.1
# Flume 1.8.0
# Kafka kafka_2.11-0.9.0.1
# 1. socket_wordcount
# 2. stateful_wordcount to mysql
# 3. filter data
# 4. flume push-based
# 5. flume pull-based
# 6. kafka receiver-based not support in spark2.3.0 with kafka 0.10.0 or higher
# 7. kafka direct
# 8. 使用Structured Streaming【类flume source-sink】(file socket kafka)

if __name__ == "__main__":
os.environ["SPARK_HOME"] = "/usr/local/spark-2.3.0-bin-hadoop2.7"
os.environ["PYSPARK_PYTHON"] = "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/bin/python3"

MYSQL_HOST = '127.0.0.1'
MYSQL_PORT = '3306'
MYSQL_DB_NAME = '0db_test'
MYSQL_USER = 'root'
MYSQL_PWD = 'root'

# sc = SparkContext(appName="pyspark-streaming-action")
# self, sparkContext, batchDuration=None, jssc=None
# ssc = StreamingContext(sc, 5)
# online-hdfs
# ssc.checkpoint('./checkpoint')

# nc -lk 6789
# lines = ssc.socketTextStream(hostname="127.0.0.1", port=6789)
# 1. socket_wordcount
# counts = lines.flatMap(lambda line: line.split(" ")).map(lambda word: (word, 1)).reduceByKey(lambda c1, c2: (c1 + c2))

# takeAndPrint
# counts.pprint()

# 初始化最初值,或者默认全0
# 放在此处的意义在于如果中断或挂掉,重启时从持久化存储中获取最新的结果数据作为初始数据[MySQL|HBase etc.]
# initial_state_rdd = sc.parallelize([(u'hello', 1), (u'world', 1)])

# 更新状态【数据】
def update_func(new_values, last_sum):
return sum(new_values) + (last_sum or 0)


# 2. stateful_wordcount
# running_counts = lines.flatMap(lambda line: line.split(" ")) \
# .map(lambda word: (word, 1)) \
# .updateStateByKey(update_func, initialRDD=initial_state_rdd)

# running_counts.pprint()

# 数据持久化存储
def output_data(record, cur):
if record is not None:
ls = list(record)
replace_sql = "replace into wordcount(word, wordcount) values('%s','%s')" % (ls[0], ls[1])
cur.execute(replace_sql)

# 每一个partition建立一个数据库连接;同一个连接跨executor【机器】时会有socket序列化异常
def with_partition(partition):
conn = mysql.connector.connect(host=MYSQL_HOST, port=MYSQL_PORT, database=MYSQL_DB_NAME, user=MYSQL_USER, password=MYSQL_PWD)
cur = conn.cursor(buffered=True)
for record in partition:
output_data(record=record, cur=cur)
conn.commit()
conn.close()


# running_counts.foreachRDD(
# lambda rdd: rdd.foreachPartition(
# with_partition
# )
# )

# 3. filter data
# line 20180101,吐槽大会 20180502,鸿鹄hao 20180420,北大约谈student 20180504,台大新五四运动
# filter_rdd = sc.parallelize([(u'鸿鹄hao', u'true'), (u'北大约谈student', u'true')])
filter_list = ['鸿鹄hao', '北大约谈student']

# (吐槽大会,'20180101,吐槽大会')
# hots = lines.map(lambda line: (line.split(",")[1], line)).transform(
# lambda rdd:
# # RDD transformations and actions can only be invoked by the driver, not inside of other transformations
# # rdd.leftOuterJoin(filter_rdd).filter(lambda x: x[1][2] != 'true').map(lambda x: x[1])
# rdd.filter(lambda x: x[0] not in filter_list).map(lambda x: x[1])
# )

# hots.pprint()

# 4. flume push-based
# **Note: Flume support is deprecated as of Spark 2.3.0.**
# 话说spark socket可以自己"接收"数据...
# nohup flume-ng agent --conf conf --conf-file conf/flume-push-streaming.conf --name flume-push-streaming > flume-push-streaming.out 2>&1 &
# /usr/local/env/lib2/spark-streaming-flume_2.11-2.3.0.jar
# /usr/local/env/lib2/flume-ng-sdk-1.8.0.jar
# 直接telnet 127.0.0.1 55555 org.apache.avro.AvroRuntimeException: Excessively large list allocation request detected:【发送tcp接收avro】
# telnet 127.0.0.1 45555向flume的source端口发送数据
# flumeStream = FlumeUtils.createStream(ssc, hostname='127.0.0.1', port=55555)
# flume_lines = flumeStream.map(lambda x: x[1])
# flume_counts = flume_lines.flatMap(lambda line: line.split(" ")) \
# .map(lambda word: (word, 1)) \
# .reduceByKey(lambda a, b: a + b)
#
# flume_counts.pprint()

# 5. flume pull-based
# note:: Flume support is deprecated as of Spark 2.3.0.
# nohup flume-ng agent --conf conf --conf-file conf/flume-pull-streaming.conf --name flume-pull-streaming > flume-pull-streaming.out 2>&1 &
# telnet 127.0.0.1 43333
# 注意查看/usr/local/apache-flume-1.8.0-bin/logs/flume.log
# /usr/local/apache-flume-1.8.0-bin/lib/spark-streaming-flume-assembly_2.11-2.3.0.jar
# org.apache.spark.streaming.flume.sink.SparkSink
# OSError: [Errno 23] Too many open files in system
# /usr/local/env/lib2/spark-streaming-flume-sink_2.11-2.3.0.jar
# Error while processing transaction.
# java.lang.IllegalStateException: begin() called when transaction is OPEN!
# /usr/local/apache-flume-1.8.0-bin/lib/scala-library-2.10.5.jar 版本低 mv scala-library-2.10.5.jar scala-library-2.10.5.jar.old
# /usr/local/apache-flume-1.8.0-bin/scala-library-2.11.8.jar
# addresses = [('127.0.0.1', 43334)]
# flumeStream = FlumeUtils.createPollingStream(ssc, addresses)
# flume_lines = flumeStream.map(lambda x: x[1])
# flume_counts = flume_lines.flatMap(lambda line: line.split(" ")) \
# .map(lambda word: (word, 1)) \
# .reduceByKey(lambda a, b: a + b)
# flume_counts.pprint()
#
# ssc.start()
# ssc.awaitTermination()

# 6. kafka receiver-based not support in spark2.3.0 with kafka 0.10.0 or higher
# 集群逗号隔开
#
zkQuorum = '127.0.0.1:2181'
topic = 'kafka_streaming_topic'
# Kafka 0.8 support is deprecated as of Spark 2.3.0.
# 需要spark-streaming-kafka-0-8:2.3.0或者spark-streaming-kafka-0-8-assembly, Version = 2.3.0.
# 使用Scala吧【可以使用高版本的spark-streaming-kafka,If you're using Python API to writing Streaming application, then it is not supported.】【这里会提示0-8版本的spark-streaming-kafka】
# /usr/local/env/lib2/spark-streaming-kafka-0-8-assembly_2.11-2.3.0.jar
# /usr/local/env/lib2/spark-streaming-kafka-0-8_2.11-2.3.0.jar
# lz4版本冲突【0-10的也冲突,需要修改jar里面的class文件】java.lang.NoSuchMethodError: net.jpountz.lz4.LZ4BlockInputStream
# spark lz4-java-1.4.0.jar
brokers = "127.0.0.1:9092"
# kafka_stream = KafkaUtils.createStream(ssc, zkQuorum=zkQuorum, groupId="spark-streaming-consumer", topics={topic: 1})
# 7. kafka direct
# kafka_stream = KafkaUtils.createDirectStream(ssc, [topic], {"metadata.broker.list": brokers})
# kafka_lines = kafka_stream.map(lambda x: x[1])
# kafka_counts = kafka_lines.flatMap(lambda line: line.split(" ")) \
# .map(lambda word: (word, 1)) \
# .reduceByKey(lambda a, b: a + b)
#
# kafka_counts.pprint()

# 8. 使用Structured Streaming【类flume source-sink】(file socket kafka)
# The Dataset API is available in Scala and Java. Python does not have the support for the Dataset API.
# http://spark.apache.org/docs/latest/structured-streaming-programming-guide.html
spark = SparkSession.builder.appName("pyspark-streaming-action-struct").getOrCreate()
# Path in Structured Streaming has to be a directory not a file.
# 拷贝一个新的文件放入文件夹中,文件夹的变化,而非文件本身内容的变化
# lines = spark.readStream.text("file:///Users/shaozhipeng/2017/python/ishangzu-offlinedata-analysis/spark230/data/")
# nc -lk 43335
# lines = spark.readStream.format("socket").option("host", "127.0.0.1").option("port", "43335").load()
# http://spark.apache.org/docs/latest/structured-streaming-kafka-integration.html
# /usr/local/env/lib2/spark-sql-kafka-0-10_2.11-2.3.0.jar
# /usr/local/env/lib2/kafka-clients-0.10.1.0.jar
# export KAFKA_HOME=/usr/local/kafka_2.11-0.10.1.0 INFO Kafka version : 0.10.1.0 (org.apache.kafka.common.utils.AppInfoParser)
# 默认 latest 可是每次query都是新的groupid-格式 spark-kafka-source-32位-9位数字-driver-0 如果重启,中间生产的消息就丢了...除非earliest全部,可是kafka存储的消息N天后也会自动删除
# .option("startingOffsets", "earliest")
# 流、批处理查询都可以指定Offset范围 """{"topic1":{"0":23,"1":-2},"topic2":{"0":-2}}""" "0" "1" 为 partition,后面为offset
# 如lines.value一样拿到lines.partition lines.offset,出故障时存储下来,重启时获取再加1或者加1存储直接获取
# lines = spark.readStream.format("kafka").option("kafka.bootstrap.servers", brokers).option("subscribe", topic).option("startingOffsets", """{"%s":{"0": 7}}""" % topic).load().selectExpr("CAST(value AS STRING)")

# words = lines.select(
# explode(
# split(lines.value, ' ')
# ).alias('word')
# )
# word_counts = words.groupBy('word').count()

# query = word_counts.writeStream.outputMode("complete").format("console").start()
# query.awaitTermination()

# 8.final 使用window时间窗口,统计连续时间窗口内的数据
lines = spark.readStream.format("kafka") \
.option("kafka.bootstrap.servers", brokers) \
.option("subscribe", topic) \
.option("startingOffsets", """{"%s":{"0": 10}}""" % topic) \
.option('includeTimestamp', 'true') \
.load()

words = lines.select(
explode(split(lines.value, ' ')).alias('word'),
lines.timestamp
)

windowDuration = '{} seconds'.format(30)
slideDuration = '{} seconds'.format(10)

windowedCounts = words.groupBy(
window(words.timestamp, windowDuration, slideDuration),
words.word
).count().orderBy('window')

query = windowedCounts \
.writeStream \
.outputMode('complete') \
.format('console') \
.option('truncate', 'false') \
.start()

query.awaitTermination()

# ssc.start()
# ssc.awaitTermination()


"""
$ nc -lk 6789
what are you donging
are you ok?


-------------------------------------------
Time: 2018-05-05 17:38:00
-------------------------------------------
('are', 1)
('', 1)
('you', 1)
('donging', 1)
('what', 1)

-------------------------------------------
Time: 2018-05-05 17:38:05
-------------------------------------------

-------------------------------------------
Time: 2018-05-05 17:38:10
-------------------------------------------
('are', 1)
('you', 1)
('ok?', 1)


$ nc -l 6789
hello world


-------------------------------------------
Time: 2018-05-05 17:53:30
-------------------------------------------
('hello', 1)
('world', 1)

-------------------------------------------
Time: 2018-05-05 17:53:35
-------------------------------------------
('hello', 2)
('world', 2)
"""

# flume agent
# flume-push-streaming.conf

"""
# flume-push-streaming.conf: netcat to spark streaming

# Name the components on this agent
flume-push-streaming.sources = netcat-source
flume-push-streaming.sinks = avro-sink
flume-push-streaming.channels = memory-channel

# Describe/configure the source
flume-push-streaming.sources.netcat-source.type = netcat
flume-push-streaming.sources.netcat-source.bind = 127.0.0.1
flume-push-streaming.sources.netcat-source.port = 45555

# Describe the sink
flume-push-streaming.sinks.avro-sink.type = avro
flume-push-streaming.sinks.avro-sink.hostname = 127.0.0.1
flume-push-streaming.sinks.avro-sink.port = 55555

# Use a channel which buffers events in memory
flume-push-streaming.channels.memory-channel.type = memory

# Bind the source and sink to the channel
flume-push-streaming.sources.netcat-source.channels = memory-channel
flume-push-streaming.sinks.avro-sink.channel = memory-channel
"""

"""
$ telnet 127.0.0.1 45555
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
d=
OK
a b c d f g hello world world abc d a
OK


-------------------------------------------
Time: 2018-05-06 15:58:00
-------------------------------------------
('d=', 1)
('\r', 1)

-------------------------------------------
Time: 2018-05-06 15:58:30
-------------------------------------------
('g', 1)
('c', 1)
('b', 1)
('hello', 1)
('a', 1)
('abc', 1)
('d', 2)
('a\r', 1)
('world', 2)
('f', 1)
"""

# flume-pull-streaming.conf

"""
# flume-pull-streaming.conf: netcat to spark streaming

# Name the components on this agent
flume-pull-streaming.sources = netcat-source
flume-pull-streaming.sinks = spark-sink
flume-pull-streaming.channels = memory-channel

# Describe/configure the source
flume-pull-streaming.sources.netcat-source.type = netcat
flume-pull-streaming.sources.netcat-source.bind = 127.0.0.1
flume-pull-streaming.sources.netcat-source.port = 43333

# Describe the sink
flume-pull-streaming.sinks.spark-sink.type = org.apache.spark.streaming.flume.sink.SparkSink
flume-pull-streaming.sinks.spark-sink.hostname = 127.0.0.1
flume-pull-streaming.sinks.spark-sink.port = 43334

# Use a channel which buffers events in memory
flume-pull-streaming.channels.memory-channel.type = memory
flume-pull-streaming.channels.memory-channel.capacity = 1000
flume-pull-streaming.channels.memory-channel.transactionCapacity = 100

# Bind the source and sink to the channel
flume-pull-streaming.sources.netcat-source.channels = memory-channel
flume-pull-streaming.sinks.spark-sink.channel = memory-channel
"""

"""
$ sudo sysctl -w kern.maxfiles=65535
Password:
kern.maxfiles: 12288 -> 65535
$ sysctl kern.maxfiles
kern.maxfiles: 65535
$ sysctl -w kern.maxfilesperproc=65535
kern.maxfilesperproc: 10240
$ sudo sysctl -w kern.maxfilesperproc=65535
kern.maxfilesperproc: 10240 -> 65535
"""

"""
$ telnet 127.0.0.1 43333
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
a b c d d c d c s b
OK


-------------------------------------------
Time: 2018-05-06 17:47:50
-------------------------------------------
('s', 1)
('c', 3)
('b', 1)
('b\r', 1)
('a', 1)
('d', 3)
"""

# kafka

"""
$ kafka-topics.sh --create --zookeeper 127.0.0.1:2181 --replication-factor 1 --partitions 1 --topic kafka_streaming_topic
WARNING: Due to limitations in metric names, topics with a period ('.') or underscore ('_') could collide. To avoid issues it is best to use either, but not both.
Created topic "kafka_streaming_topic".

$ kafka-topics.sh --list --zookeeper 127.0.0.1:2181
__consumer_offsets
default-flume-topic
kafka_streaming_topic
test

$ kafka-console-producer.sh --broker-list 127.0.0.1:9092 --topic kafka_streaming_topic
测试

$ kafka-console-consumer.sh --zookeeper 127.0.0.1:2181 --topic kafka_streaming_topic
测试
"""

# Structured Streaming
# file directory
# access.log 和 dataAnalysis.log

"""
-------------------------------------------
Batch: 0
-------------------------------------------
+-------+-----+
| word|count|
+-------+-----+
| add| 3|
| avc| 1|
|content| 3|
| 三生三世| 1|
+-------+-----+

[Stage 3:========================> (87 + 4) / 200]-------------------------------------------
Batch: 1
-------------------------------------------
+--------------------+-----+
| word|count|
+--------------------+-----+
|14:25:42","end_st...| 1|
|'{"id":"8AB398CA5...| 2|
|'{"$oid":"5a68268...| 1|
|19:28:31","type":...| 1|
|20:20:15","type":...| 1|
|'{"$oid":"5a68261...| 1|
|18:55:34","house_...| 1|
|'{"id":"8AB398CA5...| 1|
|14:28:28","update...| 1|
|'{"id":"8AB398CA5...| 2|
|14:24:06","free_s...| 1|
|20:08:10","type":...| 1|
|19:44:21","type":...| 2|
|'{"id":"8AEF86896...| 1|
| 16:23:50'| 1|
|19:22:08","house_...| 4|
|11:05:02","role_i...| 1|
|20:03:37","house_...| 2|
|'{"door":"戊","typ...| 3|
|'{"$oid":"5a68269...| 1|
+--------------------+-----+
only showing top 20 rows
"""

# socket

"""
$ nc -lk 43335
who are you?
what are you want todo?^[[D^[[D^[[C

-------------------------------------------
Batch: 0
-------------------------------------------
+----+-----+
|word|count|
+----+-----+
| who| 1|
|you?| 1|
| are| 1|
+----+-----+

[Stage 3:======================================================>(198 + 2) / 200]-------------------------------------------
Batch: 1
-------------------------------------------
+--------------+-----+
| word|count|
+--------------+-----+
| who| 1|
| you| 1|
| want| 1|
| what| 1|
|todo?| 1|
| you?| 1|
| are| 2|
+--------------+-----+
"""

# kafka
# 处理采用10版本[之前是9]的kafka之后,消费者端消费不到消息的问题,删除brokers节点【这里只是本地的测试数据】重启broker
# startingOffsets earliest

"""
$ zkCli.sh -server 127.0.0.1:2181
[zk: 127.0.0.1:2181(CONNECTED) 2] ls /
[cluster, controller_epoch, brokers, zookeeper, dubbo, admin, isr_change_notification, consumers, config, disconf, hbase]
[zk: 127.0.0.1:2181(CONNECTED) 3] rmr /brokers
[zk: 127.0.0.1:2181(CONNECTED) 5] ls /
[cluster, controller_epoch, zookeeper, dubbo, admin, isr_change_notification, consumers, config, disconf, hbase]

$ kafka-server-start.sh $KAFKA_HOME/config/server.properties

$ kafka-topics.sh --create --zookeeper 127.0.0.1:2181 --replication-factor 1 --partitions 1 --topic kafka_streaming_topic

$ kafka-console-consumer.sh --bootstrap-server 127.0.0.1:9092 --topic kafka_streaming_topic --from-beginning --new-consumer
hello world
are you ok?
why
为什么
sdsvdfv
vdfvdv

$ kafka-console-producer.sh --broker-list 127.0.0.1:9092 --topic kafka_streaming_topic
终于正常了 哈哈哈 hello world yes ok


-------------------------------------------
Batch: 0
-------------------------------------------
+-------+-----+
| word|count|
+-------+-----+
|sdsvdfv| 1|
| you| 1|
| ok?| 1|
| hello| 1|
| 为什么| 1|
| are| 1|
| world| 1|
| why| 1|
| vdfvdv| 1|
+-------+-----+

[Stage 3:=====================================================> (193 + 4) / 200]-------------------------------------------
Batch: 1
-------------------------------------------
+-------+-----+
| word|count|
+-------+-----+
| 终于正常了| 1|
|sdsvdfv| 1|
| you| 1|
| ok?| 1|
| hello| 2|
| ok| 1|
| 哈哈哈| 1|
| 为什么| 1|
| are| 1|
| world| 2|
| yes| 1|
| why| 1|
| vdfvdv| 1|
+-------+-----+
"""

# kafka offset range

"""
$ kafka-consumer-groups.sh --list --zookeeper 127.0.0.1:2181
console-consumer-61430
console-consumer-43276
console-consumer-99793
console-consumer-71130


$ kafka-console-producer.sh --broker-list 127.0.0.1:9092 --topic kafka_streaming_topic
yes use offset and partition
yes use it


-------------------------------------------
Batch: 0
-------------------------------------------
+------+-----+
| word|count|
+------+-----+
|latest| 1|
+------+-----+

-------------------------------------------
Batch: 1
-------------------------------------------
+---------+-----+
| word|count|
+---------+-----+
|partition| 1|
| use| 1|
| latest| 1|
| and| 1|
| yes| 1|
| offset| 1|
+---------+-----+

[Stage 5:======================================================>(199 + 1) / 200]-------------------------------------------
Batch: 2
-------------------------------------------
+---------+-----+
| word|count|
+---------+-----+
|partition| 1|
| use| 2|
| it| 1|
| latest| 1|
| and| 1|
| yes| 2|
| offset| 1|
+---------+-----+

"""

# 8.final

"""
-------------------------------------------
Batch: 0
-------------------------------------------
+------------------------------------------+-----+-----+
|window |word |count|
+------------------------------------------+-----+-----+
|[2018-05-07 19:21:20, 2018-05-07 19:21:50]|yes |1 |
|[2018-05-07 19:21:20, 2018-05-07 19:21:50]|oh |1 |
|[2018-05-07 19:21:30, 2018-05-07 19:22:00]|yes |2 |
|[2018-05-07 19:21:30, 2018-05-07 19:22:00]|oh |2 |
|[2018-05-07 19:21:40, 2018-05-07 19:22:10]|right|1 |
|[2018-05-07 19:21:40, 2018-05-07 19:22:10]|are |1 |
|[2018-05-07 19:21:40, 2018-05-07 19:22:10]|yes |3 |
|[2018-05-07 19:21:40, 2018-05-07 19:22:10]|you |1 |
|[2018-05-07 19:21:40, 2018-05-07 19:22:10]|oh |2 |
|[2018-05-07 19:21:50, 2018-05-07 19:22:20]|you |1 |
|[2018-05-07 19:21:50, 2018-05-07 19:22:20]|right|1 |
|[2018-05-07 19:21:50, 2018-05-07 19:22:20]|yes |2 |
|[2018-05-07 19:21:50, 2018-05-07 19:22:20]|oh |1 |
|[2018-05-07 19:21:50, 2018-05-07 19:22:20]|are |1 |
|[2018-05-07 19:22:00, 2018-05-07 19:22:30]|right|1 |
|[2018-05-07 19:22:00, 2018-05-07 19:22:30]|you |1 |
|[2018-05-07 19:22:00, 2018-05-07 19:22:30]|are |1 |
|[2018-05-07 19:22:00, 2018-05-07 19:22:30]|yes |1 |
|[2018-05-07 19:23:40, 2018-05-07 19:24:10]|c |3 |
|[2018-05-07 19:23:40, 2018-05-07 19:24:10]|f |1 |
+------------------------------------------+-----+-----+
only showing top 20 rows
"""
邵志鹏 wechat
扫一扫上面的二维码关注我的公众号
0%