Pyspark
操作json数组字符串
from pyspark.sql import SparkSession
from pyspark.sql.functions import explode, from_json, col
from pyspark.sql.types import StructType, StructField, StringType, ArrayType
# 创建 SparkSession
spark = SparkSession.builder.appName("JsonColumnExplode").getOrCreate()
# 示例 JSON 字符串
json_data = '''
{
"requestNo": "434-2024-01-01T17:30:37.297",
"requestType": "simulation",
"generalStoreId": "434",
"storeInfo": [
{
"storeId": "3170",
"isNeedFuse": null,
"environmentCapacities": [
{"environmentNo": "193", "capacity": 234.36, "remainingCapacity": 222.87787902956552},
{"environmentNo": "194", "capacity": 400.68, "remainingCapacity": 382.2316174320055},
{"environmentNo": "195", "capacity": 28.62, "remainingCapacity": 28.33775998878479},
{"environmentNo": "189", "capacity": 57.24, "remainingCapacity": 55.95100601226664},
{"environmentNo": "207", "capacity": 57.24, "remainingCapacity": 57.24}
],
"productInfo": [
{
"productId": "79200",
"productSpeciesId": "1487",
"isNew": 0,
"isTop": 0,
"isCross": 1,
"environmentNo": "193",
"lastStock": 2.79,
"allocateAmount": 0.0,
"avgSale": 0.0,
"avgSaleFactor": null,
"planQuantity": 2.79,
"price": -1,
"valid": 0,
"transferUnit": 1.0,
"purchaseUnit": "\u4efd",
"weight": 0.0,
"netWeight": 350,
"netWeightUnit": "g"
}
]
}
]
}
'''
# 创建 DataFrame
schema = StructType([
StructField("requestNo", StringType(), True),
StructField("requestType", StringType(), True),
StructField("generalStoreId", StringType(), True),
StructField("storeInfo", ArrayType(
StructType([
StructField("storeId", StringType(), True),
StructField("isNeedFuse", StringType(), True),
StructField("environmentCapacities", ArrayType(
StructType([
StructField("environmentNo", StringType(), True),
StructField("capacity", StringType(), True),
StructField("remainingCapacity", StringType(), True)
])
), True),
StructField("productInfo", ArrayType(
StructType([
StructField("productId", StringType(), True),
StructField("productSpeciesId", StringType(), True),
StructField("isNew", StringType(), True),
StructField("isTop", StringType(), True),
StructField("isCross", StringType(), True),
StructField("environmentNo", StringType(), True),
StructField("lastStock", StringType(), True),
StructField("allocateAmount", StringType(), True),
StructField("avgSale", StringType(), True),
StructField("avgSaleFactor", StringType(), True),
StructField("planQuantity", StringType(), True),
StructField("price", StringType(), True),
StructField("valid", StringType(), True),
StructField("transferUnit", StringType(), True),
StructField("purchaseUnit", StringType(), True),
StructField("weight", StringType(), True),
StructField("netWeight", StringType(), True),
StructField("netWeightUnit", StringType(), True)
])
), True)
])
), True)
])
# 读取 JSON 数据并创建 DataFrame
json_rdd = spark.sparkContext.parallelize([json_data])
json_df = spark.read.json(json_rdd, schema=schema)
# 展开 storeInfo 列
df = json_df.select(
"requestNo",
"requestType",
"generalStoreId",
explode("storeInfo").alias("store_info")
)
# 选择展开后的列
df = df.select(
"requestNo",
"requestType",
"generalStoreId",
"store_info.storeId",
"store_info.isNeedFuse",
"store_info.environmentCapacities",
"store_info.productInfo"
)
# 展示最终结果
df.show(truncate=False)
# 停止 SparkSession
spark.stop()
- 本文作者: ICU
- 本文链接: https://www.awayanan.wang/works.html
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!
Sci Hub
Code With Me
Marxists