我有一个数据框,在其中创建一列并根据其他列中的条件填充值。
List<String> new_lst = new ArrayList<String>();
new_lst.add("value 1");
new_lst.add("value_2");
df_new = df.withColumn("new_column",functions.when(functions.col("col_1").equalTo("A")
.and(functions.col("col_2").isin(new_lst.stream().toArray(String[]::new)),functions.col("col_3"))
.otherwise(functions.col("col_1");
[当我使用上面的代码时,我得到警告
令人困惑的参数new_lst.stream()。toArray(String [] :: new,不清楚需要varargs或non-varargs调用
我的要求是,通过检查col_1是否等于A并创建一个名为new_column的列,col_2具有value_1和value_2,那么我必须从col_3获取值,否则要从col_1获取值。
请让我知道在java spark中实现此目标的最佳方法。
List<String> new_lst = new ArrayList<>();
new_lst.add("value_1");
new_lst.add("value_2");
Dataset<Row> df = spark.range(1).withColumn("col_1", lit("A"))
.withColumn("col_2", lit("value_2"))
.withColumn("col_3", lit("C"));
Dataset<Row> df_new = df.withColumn("new_column",functions.when(functions.col("col_1").equalTo("A")
.and(functions.col("col_2").isInCollection(new_lst)), functions.col("col_3"))
.otherwise(functions.col("col_1"))
);
df_new.show(false);
/**
* +---+-----+-------+-----+----------+
* |id |col_1|col_2 |col_3|new_column|
* +---+-----+-------+-----+----------+
* |0 |A |value_2|C |C |
* +---+-----+-------+-----+----------+
*/
df.withColumn("new_column",functions.when(functions.col("col_1").equalTo("A")
.and(functions.col("col_2").isin((Object[]) new_lst.toArray())),functions.col("col_3"))
.otherwise(functions.col("col_1"))).show(false);
/**
* +---+-----+-------+-----+----------+
* |id |col_1|col_2 |col_3|new_column|
* +---+-----+-------+-----+----------+
* |0 |A |value_2|C |C |
* +---+-----+-------+-----+----------+
*/