-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path03_mmingalov_kafka.py
146 lines (115 loc) · 4.25 KB
/
03_mmingalov_kafka.py
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
# export SPARK_KAFKA_VERSION=0.10
# pyspark --packages org.apache.spark:spark-sql-kafka-0-10_2.11:2.3.2
# pyspark --packages org.apache.spark:spark-sql-kafka-0-10_2.11:2.3.2 --master local[1]
from pyspark.sql import SparkSession
from pyspark.sql import functions as F
from pyspark.sql.types import StructType, StringType
spark = SparkSession.builder.appName("mmingalov_spark").getOrCreate()
#брокер кафки
kafka_brokers = "bigdataanalytics-worker-0.novalocal:6667"
#функция, чтобы выводить на консоль вместо show()
def console_output(df, freq):
return df.writeStream \
.format("console") \
.trigger(processingTime='%s seconds' % freq ) \
.options(truncate=True) \
.start()
#читаем без стрима
raw_orders = spark.read. \
format("kafka"). \
option("kafka.bootstrap.servers", kafka_brokers). \
option("subscribe", "orders_json"). \
option("startingOffsets", "earliest"). \
load()
raw_orders.show()
raw_orders.show(1,False)
#прочитали до 20го оффсета
raw_orders = spark.read. \
format("kafka"). \
option("kafka.bootstrap.servers", kafka_brokers). \
option("subscribe", "orders_json"). \
option("startingOffsets", "earliest"). \
option("endingOffsets", """{"orders_json":{"0":20}}"""). \
load()
#
raw_orders.show(100)
# прочитали в стриме ВСЁ
raw_orders = spark.readStream. \
format("kafka"). \
option("kafka.bootstrap.servers", kafka_brokers). \
option("subscribe", "orders_json"). \
option("startingOffsets", "earliest"). \
load()
out = console_output(raw_orders, 5)
out.stop()
# прочитали потихоньку
raw_orders = spark.readStream. \
format("kafka"). \
option("kafka.bootstrap.servers", kafka_brokers). \
option("subscribe", "orders_json"). \
option("startingOffsets", "earliest"). \
option("maxOffsetsPerTrigger", "5"). \
load()
out = console_output(raw_orders, 5)
out.stop()
# прочитали один раз с конца
raw_orders = spark.readStream. \
format("kafka"). \
option("kafka.bootstrap.servers", kafka_brokers). \
option("subscribe", "orders_json"). \
option("maxOffsetsPerTrigger", "5"). \
option("startingOffsets", "latest"). \
load()
out = console_output(raw_orders, 5)
out.stop()
# прочитали с 10го оффсета
raw_orders = spark.readStream. \
format("kafka"). \
option("kafka.bootstrap.servers", kafka_brokers). \
option("subscribe", "orders_json"). \
option("startingOffsets", """{"orders_json":{"0":10}}"""). \
option("maxOffsetsPerTrigger", "5"). \
load()
out = console_output(raw_orders, 5)
out.stop()
##разбираем value
schema = StructType() \
.add("order_id", StringType()) \
.add("customer_id", StringType()) \
.add("order_status", StringType()) \
.add("order_purchase_timestamp", StringType()) \
.add("order_approved_at", StringType()) \
.add("order_delivered_carrier_date", StringType()) \
.add("order_delivered_customer_date", StringType()) \
.add("order_estimated_delivery_date", StringType())
value_orders = raw_orders \
.select(F.from_json(F.col("value").cast("String"), schema).alias("value"), "offset")
value_orders.printSchema()
parsed_orders = value_orders.select("value.*", "offset")
parsed_orders.printSchema()
out = console_output(parsed_orders, 30)
out.stop()
#добавляем чекпоинт
def console_output_checkpointed(df, freq):
return df.writeStream \
.format("console") \
.trigger(processingTime='%s seconds' % freq) \
.option("truncate",False) \
.option("checkpointLocation", "orders_console_checkpoint") \
.start()
out = console_output_checkpointed(parsed_orders, 5)
out.stop()
########################################################################################################
schema = StructType() \
.add("sepalLength", StringType()) \
.add("sepalWidth", StringType()) \
.add("petalLength", StringType()) \
.add("petalWidth", StringType()) \
.add("species", StringType())
#все разом
raw_files = spark \
.readStream \
.format("json") \
.schema(schema) \
.options(path="input_csv_for_stream", header=True) \
.load()