Spark Java edit data in column

问题描述 投票:1回答:2

我想遍历spark DataFrame中一列的内容,并在满足特定条件的情况下更正单元格中的数据

+-------------+
|column_title |
+-------------+
+-----+
|null |
+-----+
+-----+
|0    |
+-----+
+-----+
|1    |
+-----+

让我说我想在column的值为null时显示其他内容,我尝试使用

Column.when()DataSet.withColumn()

但是我找不到正确的方法,我认为没有必要转换为RDD并对其进行迭代。

java apache-spark apache-spark-sql apache-spark-dataset
2个回答
2
投票

您可以使用whenequalTowhenisNull

Dataset<Row> df1 = df.withColumn("value", when(col("value").equalTo("bbb"), "ccc").otherwise(col("value")));

Dataset<Row> df2 = df.withColumn("value", when(col("value").isNull(), "ccc").otherwise(col("value")));

如果仅想替换空值,则还可以使用nafill

Dataset<Row> df3 = df.na().fill("ccc");

0
投票

另一种方法可以通过使用UDF。

创建UDF。

    private static UDF1 myUdf = new UDF1<String, String>() {
    public String call(final String str) throws Exception {
        // any condition or custom function can be used
        return StringUtils.rightPad(str, 25, 'A');
      }
    };

在SparkSession中注册UDF。

    sparkSession.udf().register("myUdf", myUdf, DataTypes.StringType);

在数据集上应用udf。

   Dataset<Row> dataset = dataset = dataset.withColumn("city", functions.callUDF("myudf", col("city")));

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.