【问题标题】:Append new column based on existing colum Dataset Spark-Java基于现有列数据集 Spark-Java 追加新列
【发布时间】:2018-12-25 22:54:29
【问题描述】:

我正在使用 Java-Spark。

我已将 JSON 数据加载到 Dataset<Row> 对象,如下所示:

Dataset<Row> df = sparkSession.read().json(jsonSet);
Dataset<Row> dfSelect = df.select(cols);//Where cols is Column[]

我的 JSON 格式如下:

ColA    ColB
 2       3
 1       2
 3       1

我想创建一个计算 ColA + ColC 值的新列,以使最终表格如下:

ColA    ColB    ColC
 2       3       5
 1       2       3  
 3       1       4

我如何使用我的dfSelect 对象来做到这一点?

dfSelect.withColumn("ColC", ?);

谢谢。

【问题讨论】:

    标签: apache-spark dataset


    【解决方案1】:

    你可以使用Column的plus函数:

    Dataset<Row> newDs = dfSelect.withColumn("ColC",dfSelect.col("ColA").plus(dfSelect.col("ColB")).cast(IntegerType));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多