【发布时间】:2019-02-19 10:48:27
【问题描述】:
我有一个Dataset<Row>,其列的值为"null"(空文字)。
我正在尝试将 "null" 文本替换为文本:\N。
为此,我使用了一个逻辑,我将添加一个名称附加“_nulled”的新列,例如abc 列变为 abc_nulled,如果当前值为文本 null,则此新列将具有值 "\N" > 否则价值保持不变。
为此,我使用了withColumn(<new name>, when(col.equalTo("null"), "\\N").otherwise(<existing_value>))。
我如何获得这个<existing_value>.
当我通过otherwise(ds.col(col_nm)) 时它不起作用,可能是因为它在otherwise() 中期待String 并找到Column。
我应该如何解决这个问题?代码如下:
ArrayList<String> newCols = new ArrayList<String>();
List<String> reqColListCopy = Arrays.asList(reqCols);
Dataset<Row> testingDS = DS.selectExpr(JavaConverters.asScalaIteratorConverter(reqColListCopy.iterator()).asScala().toSeq())
//Creating newCols (ArrayList so that I can add/remove column names.
Iterator itrTmp2 = reqColListCopy.iterator();
while(itrTmp2.hasNext()){
newCols.add((String)itrTmp2.next());
}
//Creating a List reference for newCols ArrayList. This will be used to get Seq(<columns>).
List<String> newColsList = newCols;
Iterator colListItr = reqColListCopy.iterator();
while(colListItr.hasNext())
{
String col = colListItr.next().toString();
testingDS = testingDS.selectExpr(convertListToSeq(newColsList))
.withColumn(col+"_nulled", functions.when(testingDS.col(col).equalTo("null"), functions.lit("\\N")).otherwise(testingDS.col(col))) //'otherwise' needs a string parameter
.drop(testingDS.col(col));
newCols.add(col+"_nulled");
newCols.remove(col);
newColsList = newCols;
}
Dataset<Row> testingDS = DS.selectExpr(JavaConverters.asScalaIteratorConverter(newColsList.iterator()).asScala().toSeq())
testingDS.show(false);
【问题讨论】:
标签: java apache-spark apache-spark-sql apache-spark-dataset