Scala 在Spark Dataframe中将多个单独条目合并为单个条目

Scala 在Spark Dataframe中将多个单独条目合并为单个条目,scala,apache-spark-sql,Scala,Apache Spark Sql,假设我有一个如下所示的分区 part1: {"customerId":"1","name":"a"} {"customerId":"2","name":"b"} 假设我想将这个模式更改为 {"data":"customers":[{"customerId":"1","name":"a"},{"customerId":"2","name":"b"}]} 我试着做的是 case class Customer(customerId:Option[String],name:Option[Strin

假设我有一个如下所示的分区

part1:

{"customerId":"1","name":"a"}
{"customerId":"2","name":"b"}
假设我想将这个模式更改为

{"data":"customers":[{"customerId":"1","name":"a"},{"customerId":"2","name":"b"}]}
我试着做的是

case class Customer(customerId:Option[String],name:Option[String])
case class Customers(customers:Option[Seq[Customer]])
case class Datum(data:Option[Customers])
我尝试将分区读取为Json并转换为Dataframe

val inputJson = spark.read.format("json").load("part1")
inputJson.as[Datum]

不知何故,数据框架似乎并不隐含地推断模式

通过这种结构,我相信您正在隐藏/包装数据中真正有用的信息。这里唯一有用的信息是:
{“customerId”:“1”,“name”:“a”},{“customerId”:“2”,“name”:“b”}
客户和数据只会隐藏您真正需要的数据。为了立即访问数据,您必须首先将数据稍微更改为:

{"customers":[{"customerId":"1","name":"a"},{"customerId":"2","name":"b"}]}
然后使用下面的代码访问此JSON:

case class Customer(customerId:String, name:String)
case class Data(customers: Array[Customer])

val df = spark.read.json(path).as[Data]
如果尝试打印此数据帧,您会得到:

+----------------+
|       customers|
+----------------+
|[[1, a], [2, b]]|
+----------------+
当然,您的数据被包装到数组中。现在有一个有趣的部分,为了访问它,您必须执行以下操作:

df.foreach{ data => data.customers.foreach(println _) }
这将打印:

Customer(1,a)
Customer(2,b)
这是您需要的真实数据,但根本不容易访问

编辑:

我不会使用两个类,而是只使用一个,Customer类。然后利用内置的Spark过滤器选择内部JSON对象。最后,您可以分解每个客户数组,并从分解的列生成一个类Customer的强类型数据集

以下是最终代码:

case class Customer(customerId:String, name:String)

val path = "C:\\temp\\json_data.json"
val df = spark.read.json(path)

df.select(explode($"data.customers"))
  .map{ r => Customer(r.getStruct(0).getString(0), r.getStruct(0).getString(1))}
  .show(false)
以及输出:

+----------+----+
|customerId|name|
+----------+----+
|1         |a   |
|2         |b   |
+----------+----+

我最终操纵了数据帧本身

val inputJson = spark.read.format("json").load("part1")

val formatted = inputJson.withColumn("dummy",lit(1)).groupBy("dummy")
.agg(collect_list(struct(dataFrame.col("*"))).alias("customers"))

val finalFormatted=formatted.withColumn("data",struct(col("customers")))
.select("data")
现在当我这么做的时候

finalFormatted.printSchema
我得到了我需要的模式

  |-- data: struct (nullable = false)
  |    |-- customers: array (nullable = true)
  |    |    |-- element: struct (containsNull = true)
  |    |    |    |-- customerId: string (nullable = true)
  |    |    |    |-- name: string (nullable = true)

是否要将所有数据放在一行中?是。我确实需要它们在一个单独的逐行分区中,你是说一个单独的文件?因为如果只有一个文件可以放在一个节点中,那么可以跳过spark,使用circe或其他json解析库进行转换,以获得更自定义的程序。如果您仍然使用spark,那么您的代码将看起来像:
inputJson.as[Customer].mapPartitions(分区=>{List(数据)(一些(客户(一些(分区.toList‘‘‘‘‘‘‘‘‘‘‘)})
这应该满足您的需要这看起来更像是一种公平的解决方法。我的情况是这样的。我有一个Spark作业,它以这种格式转储json,并被许多其他下游作业使用。缺点是必须创建一个虚拟/容器类(在本例中称为Data)来保存真正需要的数据。在这种情况下,您必须通过类似data的方式访问数据。另一端的客户有一个解决方案。您可以首先分解客户的内容,以便检索客户的所有记录。然后用flatMap()生成最后一个数据集,其中包含上一次输出中显示的列[customerId,name]。这似乎是我一直在寻找的东西。接受回答Hello@Rakshith抱歉之前没有回答。我有点忙:)我已经用一种更优雅的方式更新了我的答案,用于将JSON检索到客户对象中,如上所述