首页 > 解决方案 > 如何处理列名中的空格以在 expr 方法中使用 spark 合并函数

问题描述

我正在coalesce我的项目中研究火花功能。代码在没有空格的列上工作正常,但在间隔列上失败。

e1.csv

id,code,type,no root
1,,A,1
2,,,0
3,123,I,1

e2.csv

id,code,type,no root
1,456,A,1
2,789,A1,0
3,,C,0

逻辑代码

Dataset<Row> df1 = spark.read().format("csv").option("header", "true").load("/home/user/Videos/<folder>/e1.csv");

        Dataset<Row> df2 = spark.read().format("csv").option("header", "true").load("/home/user/Videos/<folder>/e2.csv");


Dataset<Row> newDS = df1.as("a").join(df2.as("b")).where("a.id== b.id").selectExpr("coalesce(`a.no root`,`b.no root`) AS `a.no root`");

newDS.show();


我试过的

Dataset<Row> newDS = df1.as("a").join(df2.as("b")).where("a.id== b.id").selectExpr("""coalesce(`a.no root`,`b.no root`) AS `a.no root`""");

espexted 的结果就像

no root
1
0
1

标签: apache-spark

解决方案


使用以下标准

val newDS = df1.as("a").join(df2.as("b")).where("a.id==b.id").selectExpr("coalesce(a.`no root`,b.`no root`) AS `a.no root`")

将产生预期的输出

+---------+
|a.no root|
+---------+
|        1|
|        0|
|        1|
+---------+

推荐阅读