将 scala 数据帧中的值列表获取到变量中?

问题描述 投票:0回答:1

我有输入数据框。我想将可空列为空的路径键的值获取到变量中

val schema_json="""[{"orders":{"order_id":{"path":"orderid","type":"string"},"customer_id":{"path":"customers.customerId","type":"int","default_value":"null"},"offer_id":{"path":"Offers.Offerid","type":"string"},"eligible":{"path":"eligible.eligiblestatus","type":"string","nullable":true,"default_value":"not eligible"}},"products":{"product_id":{"path":"product_id","type":"string"},"product_name":{"path":"products.productname","type":"string"}}}]""";
val schemaRdd = spark.sparkContext.parallelize(schema_json :: Nil);
val schemaRdddf = spark.read.json(schemaRdd);
val schemaColumns = Seq("orders");
schemaRdddf.selectExpr(schemaColumns: _*).show(false)

expected output

abc:List[String]= List("orderid","customers.customerId","Offers.Offerid")
scala apache-spark
1个回答
0
投票

检查下面的代码。

val schema =
  "map<string, struct<path:string,type:string,nullable:boolean,default_value:string>>"
val schema_json = Seq(
  """[{"orders":{"order_id":{"path":"orderid","type":"string"},"customer_id":{"path":"customers.customerId","type":"int","default_value":"null"},"offer_id":{"path":"Offers.Offerid","type":"string"},"eligible":{"path":"eligible.eligiblestatus","type":"string","nullable":true,"default_value":"not eligible"}},"products":{"product_id":{"path":"product_id","type":"string"},"product_name":{"path":"products.productname","type":"string"}}}]"""
)
val columName = "Orders".toLowerCase
spark.read
  .json(input.toDS)
  .selectExpr(
    s"""
    concat_ws(
        ',', 
        transform(
            map_entries(
                map_filter(
                    from_json(to_json(${columName}), '${schema}'), 
                    (k,v) -> v.nullable is null
                )
            ), 
            e -> e.value.path
        )
    ) AS output
    """
  )
  .show(false)

+-------------------------------------------+
|output                                     |
+-------------------------------------------+
|customers.customerId,Offers.Offerid,orderid|
+-------------------------------------------+
© www.soinside.com 2019 - 2024. All rights reserved.