根据现有的Colum Dataset Spark-Java追加新列

时间:2018-07-18 06:25:03

标签: apache-spark dataset

我正在使用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", ?);

谢谢。

1 个答案:

答案 0 :(得分:1)

您可以使用Column的plus函数:

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