如何将数据帧的每一列从二进制转换为字节数组

how to transform each column of a dataframe from binary to byte array

我有一个具有以下架构的数据集 ds1

root
 |-- binary_col1: binary (nullable = true)

我根据需要使用

进行转换
val ds2 = ds1.map(row => row.getAs[Array[Byte]]("binary_col1"))

但是当数据集有两列二进制类型时,我该如何转换它呢?

root
 |-- binary_col1: binary (nullable = true)
  -- binary_col2: binary (nullable = false)

我想创建包含 2 列的新数据集
( binary_col1.toByteArray , binary_col2.toByteArray)

您可以在 dataframe/dataset 上使用 as,并提供 tuple2 类型:

val ds2 = ds1.as[(Array[Byte], Array[Byte])]

这比使用 map 更好,因为它保留了列名。

当然也可以用map,例如

val ds2 = ds1.map(row => (row.getAs[Array[Byte]]("binary_col1"), row.getAs[Array[Byte]]("binary_col2")))