Java Spark:如何获取数据集<;世界其他地区>;列';在循环中迭代时使用它。否则()?
我有一个Java Spark:如何获取数据集<;世界其他地区>;列';在循环中迭代时使用它。否则()?,java,apache-spark,apache-spark-sql,apache-spark-dataset,Java,Apache Spark,Apache Spark Sql,Apache Spark Dataset,我有一个数据集,其中的列具有值“null”(空的书面文本)。 我正在尝试将“null”文本替换为文本:\N 为此,我使用了一种逻辑,即我将添加一个新列,其名称后面加上“\u nulled”,例如,abc列变为abc\u nulled,此新列将具有值“\N”,如果当前值为textnull,则该值保持不变。 为此,我使用了列(,when(col.equalTo(“null”),“\\N”)。否则())。 如何获取此 当我传递否则(ds.col(col_nm))时,它不工作,可能是因为它在否则()中需
数据集
,其中的列具有值“null”
(空的书面文本)。我正在尝试将“null”文本替换为文本:\N
为此,我使用了一种逻辑,即我将添加一个新列,其名称后面加上“\u nulled”,例如,abc列变为abc\u nulled,此新列将具有值“\N”,如果当前值为textnull,则该值保持不变。
为此,我使用了列(,when(col.equalTo(“null”),“\\N”)。否则())。 如何获取此
当我传递
否则(ds.col(col_nm))
时,它不工作,可能是因为它在否则()
中需要一个字符串
,并找到一个列
我应该如何解决这个问题?代码如下:
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);
ArrayList newCols=new ArrayList();
List reqColListCopy=Arrays.asList(reqCols);
Dataset testingDS=DS.selectExpr(JavaConverters.AsscalAteratorConverter(reqColListCopy.iterator()).asScala().toSeq())
//创建newCols(ArrayList),以便添加/删除列名。
迭代器itrTmp2=reqColListCopy.Iterator();
while(itrTmp2.hasNext()){
add((字符串)itrTmp2.next());
}
//正在为newCols ArrayList创建列表引用。这将用于获取Seq()。
List newColsList=newCols;
迭代器collistir=reqColListCopy.Iterator();
while(colListItr.hasNext())
{
字符串col=colListItr.next().toString();
testingDS=testingDS.selectExpr(convertListToSeq(newColsList))
.withColumn(col+“_null”),functions.when(testingDS.col(col).equalTo(“null”),functions.lit(“\\N”))。否则(testingDS.col(col))/“否则”需要一个字符串参数
.drop(testingDS.col(col));
newCols.add(col+“_null”);
newCols.remove(col);
newColsList=newCols;
}
Dataset testingDS=DS.selectExpr(JavaConverters.AsscalAteratorConverter(newColsList.iterator()).asScala().toSeq())
显示(假);
我通过传递lit()中的列解决了这个问题。
:
有没有检查非空值的选项?我实际上希望非空值放在引号中,空值不被修改
.withColumn(col+"_nulled", functions.when(testingDS.col(col).equalTo("null"), functions.lit("\\N")).otherwise(functions.lit(testingDS.col(col)))) //'otherwise' needs a string parameter