动态生成spark Sql selectExpr语句以进行合并

问题描述 投票:0回答:1

我正在开发一个项目,我需要动态地提供从不同来源到coalsace的多个列名。

e1.csv

id,code,type
1,,A
2,,
3,123,I

e2.csv

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


Dataset<Row> df1 = spark.read().format("csv").option("header", "true").load("C:\\Users\\System2\\Videos\\folder\\e1.csv");

Dataset<Row> df2 = spark.read().format("csv").option("header", "true").load("C:\\Users\\System2\\Videos\\folder\\e2.csv");



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

错误代码我得到了

Exception in thread "main" org.apache.spark.sql.catalyst.parser.ParseException: 
mismatched input ';' expecting <EOF>(line 1, pos 38)


我试过了什么

尝试与\n,;,但没有人工作


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

apache-spark
1个回答
0
投票

语法错了。您的代码将类似于:

Dataset<Row> newDS = df1.as("a").join(df2.as("b")).where("a.id== b.id").selectExpr("coalesce(a.id, b.id) AS `id`","coalesce(a.code, b.code) AS `code`");
© www.soinside.com 2019 - 2024. All rights reserved.