-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_schema.py
58 lines (54 loc) · 2.09 KB
/
get_schema.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
import os
import pyspark as pyspark
import json
from dotenv import load_dotenv
from delta import *
from IPython.display import display
from pyspark.sql.functions import expr
from pyspark.sql.functions import from_json, col
from pyspark.sql.avro.functions import from_avro
from pyspark.sql.types import *
def get_schema(spark):
load_dotenv()
broker_ip = os.environ.get('REDPANDA_BROKER_IP')
topic = os.environ.get('REDPANDA_TOPIC')
df_json = (spark.read
.format("kafka")
.option("kafka.bootstrap.servers", broker_ip)
.option("subscribe", topic)
.option("startingOffsets", "earliest")
.option("endingOffsets", "latest")
.option("failOnDataLoss", "false")
.load()
# filter out empty values
.withColumn("value", expr("string(value)"))
.filter(col("value").isNotNull())
# get latest version of each record
.select("key", expr("struct(offset, value) r"))
.groupBy("key").agg(expr("max(r) r"))
.select("r.value"))
# decode the json values
df_read = spark.read.json(df_json.rdd.map(lambda x: x.value), multiLine=True)
return df_read.schema.json()
def get_table_df(spark):
load_dotenv()
broker_ip = os.environ.get('REDPANDA_BROKER_IP')
topic = os.environ.get('REDPANDA_TOPIC')
schema = get_schema(spark)
table_df = (
spark
.readStream
.format("kafka")
.option("kafka.bootstrap.servers", broker_ip)
.option("subscribe", topic)
.option("startingOffsets", "earliest")
.option("mergeSchema", "true")
.option("includeHeaders", "true")
.option("failOnDataLoss", "false")
.load()
.withColumn("value", expr("string(value)"))
.filter(col("value").isNotNull())
.withColumn('value', from_json(col("value"), schema))
.select('value.*')
)
return table_df