配置单元查询中列名的变量替换

Variable substitution for column names in hive query

我有一个任务,我需要比较数据框的 2 列并得到 differences.There 是数据框中的 200 多列,我必须编写 100 多个查询来检查列中的值。 例如:DF1 https://i.stack.imgur.com/Aj1ca.png

我需要 X1 = X2 和列对具有不同值的所有值。 简单来说-

select A1,A2 from DF1 where X1=X2 and A1!=A2
select B1,B2 from DF1 where X1=X2 and B1!=B2
select C1,C2 from DF1 where X1=X2 and C1!=C2

现在我有 100 多个列,所以我必须编写 100 多个这样的查询。所以我想在 scala 中编写一个函数,在其中我只传递将在配置单元查询中替换的列名(A1、A2 或 B1、B2 等)。

 def comp_col(a:Any, b:Any):Any= {
var ret = sqlc.sql("SELECT $a, $b from DF1 WHERE X1= X2 $a!= $b");
return ret;

}

无论如何,函数中的查询是否会从我传递的变量中获取列名。 也欢迎任何不同的方法。

提前致谢。

是,对 scala 使用字符串插值。

def comp_col(a:Any, b:Any):Any= {
var ret = sqlc.sql(s"SELECT $a, $b from DF1 WHERE X1= X2 $a!= $b");
return ret;
}