apache-spark-sql - 如何在 for 循环中使用 Spark 随后在数据集中添加列(其中 for 循环包含列名)
问题描述
在这里尝试将后续列添加到数据集行,出现的问题是最后一列仅可见。之前添加的列不保留
private static void populate(Dataset<Row> res, String[] args)
{
String[] propArr = args[0].split(","); // Eg: [abc, def, ghi]
// Dataset<Row> addColToMergedData = null;
/** Here each element is the name of the column to be inserted */
for(int i = 0; i < propArr.length; i++){
// addColToMergedData = res.withColumn(propArr[i], lit(null));
}
}
解决方案
for 循环中的逻辑有缺陷,因此是问题所在。您可以按如下方式修改程序:
private static void populate(Dataset<Row> res, String[] args)
{
String[] propArr = args[0].split(","); // Eg: [abc, def, ghi]
Dataset<Row> addColToMergedData = null;
/** Here each element is the name of the column to be inserted */
for(int i = 0; i < propArr.length; i++)
{
res = res.withColumn(propArr[i], lit(null));
}
addColToMergedData = res
}
推荐阅读
- automation - 如何在不使用 RQM 和 RQM 适配器的情况下在远程机器上运行 RFT 脚本?
- spring-boot - Spring Boot + Swagger-Ui yml 生成
- git - git add 多次而不提交
- python - 在python中对数据名称进行For循环
- kotlin - Kotlin 中的构造函数与参数
- c# - 在 EF Core 中禁用并发检查
- python-3.x - 有没有在 Julia 中使用 SQLAlchemy 的简单方法?
- elasticsearch - 用于百分位聚合值的 Elastic Search 存储桶脚本
- android - 第二个活动是空白的
- php - 变量失去引用,当被引用的变量后来引用另一个