Spark Java编辑列中的数据

Spark Java编辑列中的数据,java,apache-spark,apache-spark-sql,apache-spark-dataset,Java,Apache Spark,Apache Spark Sql,Apache Spark Dataset,我希望遍历sparkDataFrame中列的内容,并在单元格中更正满足特定条件的数据 +-------------+ |column_title | +-------------+ +-----+ |null | +-----+ +-----+ |0 | +-----+ +-----+ |1 | +-----+ 假设我想在列的值为null时显示其他内容,我尝试了 Column.when() DataSet.withColumn() 但是我找不到正确的方法,我认为没有必要转换为RDD

我希望遍历spark
DataFrame
中列的内容,并在单元格中更正满足特定条件的数据

+-------------+
|column_title |
+-------------+
+-----+
|null |
+-----+
+-----+
|0    |
+-----+
+-----+
|1    |
+-----+
假设我想在列的值为null时显示其他内容,我尝试了

Column.when()
DataSet.withColumn()


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

当和
等于
时,可以使用
,当
为空时,可以使用

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")));
Dataset<Row> df3 = df.na().fill("ccc");

另一种方法是使用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');
      }
    };
在数据集上应用自定义项

   Dataset<Row> dataset = dataset.withColumn("city", functions.callUDF("myudf", col("city")));
Dataset-Dataset=Dataset.withColumn(“city”,functions.callUDF(“myudf”,col(“city”);
希望有帮助

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