从 kafka 代理到 python 中的 Spark 流应用程序的 json 数据的正确架构是什么
执行此程序后,我在数据帧的所有字段中收到空值。如果架构是问题,请向我建议此 json 格式的正确架构,否则请为我提供从 kafka-topic 读取此 json 数据的正确答案使用 readStream api 并将其打印到控制台。 这是 kafka-topic 程序中没有错误的示例 json 数据
{
"invoice_no": 154132541653705,
"country": "United Kingdom",
"timestamp": "2020-09-18 10:55:23",
"type": "ORDER",
"items": [
{
"SKU": "21485",
"title": "RETROSPOT HEART HOT WATER BOTTLE",
"unit_price": 4.95,
"quantity": 6
},
{
"SKU": "23499",
"title": "SET 12 VINTAGE DOILY CHALK",
"unit_price": 0.42,
"quantity": 2
}
]
}
,我在这里发布主要部分
spark = SparkSession.builder.appName('retaildata').getOrCreate()
spark.sparkContext.setLogLevel("ERROR")
jsonSchema=StructType([ \
StructField("invoice_no",IntegerType()), \
StructField("country",StringType()), \
StructField("timestamp",TimestampType()), \
StructField("type",StringType()), \
StructField("items",StructType([StructField("SKU",IntegerType()), \
StructField("title",StringType()), \
StructField("unit_price",DoubleType()), \
StructField("quantity",IntegerType()) \
])) \
])
dfraw = spark \
.readStream \
.format("kafka") \
.option("kafka.bootstrap.servers", "18.211.252.152:9092") \
.option("subscribe", "real-time-project") \
.option("includeHeaders","true") \
.load() \
.select(from_json(col("value").cast("string"),jsonSchema).alias("data")).select("data.*") \
.writeStream \
.format("console") \
.start() \
.awaitTermination() \
输出:
=================================
I am receiving the data as
Batch: 0
-------------------------------------------
+----------+-------+---------+----+-----+
|invoice_no|country|timestamp|type|items|
+----------+-------+---------+----+-----+
+----------+-------+---------+----+-----+
-------------------------------------------
Batch: 1
-------------------------------------------
+----------+-------+---------+----+-----+
|invoice_no|country|timestamp|type|items|
+----------+-------+---------+----+-----+
| null| null| null|null| null|
| null| null| null|null| null|
| null| null| null|null| null|
| null| null| null|null| null|
+----------+-------+---------+----+-----+
After executing this program I am recieving null values in all fields of the dataframe.If schema is the problem,Kindly me suggest me the right schema for this json format or else kindly provide me the right answer for reading this json data from kafka-topic using readStream api and print that to console.
Here is the sample json data in kafka-topic
{
"invoice_no": 154132541653705,
"country": "United Kingdom",
"timestamp": "2020-09-18 10:55:23",
"type": "ORDER",
"items": [
{
"SKU": "21485",
"title": "RETROSPOT HEART HOT WATER BOTTLE",
"unit_price": 4.95,
"quantity": 6
},
{
"SKU": "23499",
"title": "SET 12 VINTAGE DOILY CHALK",
"unit_price": 0.42,
"quantity": 2
}
]
}
program without errors,I am posting main part here
spark = SparkSession.builder.appName('retaildata').getOrCreate()
spark.sparkContext.setLogLevel("ERROR")
jsonSchema=StructType([ \
StructField("invoice_no",IntegerType()), \
StructField("country",StringType()), \
StructField("timestamp",TimestampType()), \
StructField("type",StringType()), \
StructField("items",StructType([StructField("SKU",IntegerType()), \
StructField("title",StringType()), \
StructField("unit_price",DoubleType()), \
StructField("quantity",IntegerType()) \
])) \
])
dfraw = spark \
.readStream \
.format("kafka") \
.option("kafka.bootstrap.servers", "18.211.252.152:9092") \
.option("subscribe", "real-time-project") \
.option("includeHeaders","true") \
.load() \
.select(from_json(col("value").cast("string"),jsonSchema).alias("data")).select("data.*") \
.writeStream \
.format("console") \
.start() \
.awaitTermination() \
Output:
=================================
I am receiving the data as
Batch: 0
-------------------------------------------
+----------+-------+---------+----+-----+
|invoice_no|country|timestamp|type|items|
+----------+-------+---------+----+-----+
+----------+-------+---------+----+-----+
-------------------------------------------
Batch: 1
-------------------------------------------
+----------+-------+---------+----+-----+
|invoice_no|country|timestamp|type|items|
+----------+-------+---------+----+-----+
| null| null| null|null| null|
| null| null| null|null| null|
| null| null| null|null| null|
| null| null| null|null| null|
+----------+-------+---------+----+-----+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需读取 json 字符串并将其转换为 DataFrame,然后打印模式即可。
代码:
输出:
You can just read the json string and convert as a DataFrame and then print the schema.
Code:
Output: