读取配置单元结构类型并修改值

8yoxcaq7  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(301)

我将配置单元表作为Dataframe读取,并在新的数据集中检索它。我正在从结构类型中读取特定的值(字符串),我想在将这些值存储在case类中之前格式化它们。
例如:我将结构类型读作“listelements.sneaker.colors”,它返回一个数组,因为有几种颜色。在将它们存储到新数据集中之前,我希望颜色的格式如下:
“红”、“蓝”、“黄”(引号和逗号分隔)
并存储为单个字符串。
concat\u ws用逗号连接数组元素,但我还需要用双引号将它们括起来。

session.read
      .table(footWear)
      .select(
        $"id",
        $"footWearCategory".as("category"),
        concat_ws(",", $"listelements".getField("sneaker").getField("colors")).as("availableColors"))
.where($"date" === runDate)
      .as[FootWearInformation]

case class FootWearInformation(id: String, category: String, availableColors: String)
bvuwiixz

bvuwiixz1#

自定义项:

def formatArray = udf((arr: collection.mutable.WrappedArray[String]) =>
  arr.map(x => s""""$x\"""").mkString(","))

相关问题