我试图在Dataframe映射函数中进行模式匹配—将一行与具有嵌套Case类的行模式进行匹配。此数据帧是联接的结果,其架构如下所示。它有一些基本类型的列和两个复合列:
case class MyList(values: Seq[Integer])
case class MyItem(key1: String, key2: String, field1: Integer, group1: MyList, group2: MyList, field2: Integer)
val myLine1 = new MyItem ("MyKey01", "MyKey02", 1, new MyList(Seq(1)), new MyList(Seq(2)), 2)
val myLine2 = new MyItem ("YourKey01", "YourKey02", 2, new MyList(Seq(2,3)), new MyList(Seq(4,5)), 20)
val dfRaw = Seq(myLine1, myLine2).toDF
dfRaw.printSchema
dfRaw.show
val df2 = dfRaw.map(r => r match {
case Row(key1: String, key2: String, field1: Integer, group1: MyList, group2: MyList, field2: Integer) => "Matched"
case _ => "Un matched"
})
df2.show
我的问题是,在映射函数之后,我得到的只是“不匹配”:
root
|-- key1: string (nullable = true)
|-- key2: string (nullable = true)
|-- field1: integer (nullable = true)
|-- group1: struct (nullable = true)
| |-- values: array (nullable = true)
| | |-- element: integer (containsNull = true)
|-- group2: struct (nullable = true)
| |-- values: array (nullable = true)
| | |-- element: integer (containsNull = true)
|-- field2: integer (nullable = true)
+---------+---------+------+--------------------+--------------------+------+
| key1| key2|field1| group1| group2|field2|
+---------+---------+------+--------------------+--------------------+------+
| MyKey01| MyKey02| 1| [WrappedArray(1)]| [WrappedArray(2)]| 2|
|YourKey01|YourKey02| 2|[WrappedArray(2, 3)]|[WrappedArray(4, 5)]| 20|
+---------+---------+------+--------------------+--------------------+------+
df2: org.apache.spark.sql.Dataset[String] = [value: string]
+----------+
| value|
+----------+
|Un matched|
|Un matched|
+----------+
如果忽略case分支中的两个struct列(替换
组1:MyList,组2:MyList
具有
_,则_
,那么它就起作用了
case Row(key1: String, key2: String, field1: Integer, group1: MyList, group2: MyList, field2: Integer) => "Matched"
你能帮助我如何在那个案例类中进行模式匹配吗?
谢谢