遍历数据帧中的每一列并执行比较

时间:2020-04-19 16:58:42

标签: scala apache-spark for-loop if-statement apache-spark-sql

我想在一个数据帧上的表之间创建一个比较函数,我想看看是否可以为if语句创建一个更快的过程。我希望能够遍历数据帧中的每一列,并且很好奇我是否可以使用列位置或其他方式代替为每个列名称进行配置。当我有100列时,我不想在conf文件中声明每个列的名称,所以有人可以向我指出一种更快的方法吗?

Conf文件:

matching_col1= member_id
matching_col2= activation_idn
matching_col3= addition_dt

实际代码:

 val mismatches_df_1 = Df1_renamed_matching.except(Df2_renamed_matching)
    if (mismatches_df_1.count() > 0) {

if(DF1_with_err_cols.matching_col1 != DF2_with_err_cols.matching_col1)
{insert into mismatches_df_1 VALUES (DF1_with_err_cols.matching_col1 as matching_col1, ERR_COLUMN                         
= matching_col1, ERR_VALUE_SOURCE = DF1_with_err_cols.matching_col1, ERR_DESCRIPTION = 
matching_col1 + " does not match value in " + source_db_jdbc_table_name2  )}
else{ insert into mismatches_df_1 VALUES(DF1_with_err_cols.matching_col1 as matching_col1) 
  ....
if(DF1_with_err_cols.matching_col14 != DF2_with_err_cols.matching_col14)
{insert into mismatches_df_1 VALUES (DF1_with_err_cols.matching_col14 as matching_col14, 
ERR_COLUMN = matching_col14, ERR_VALUE_SOURCE = DF1_with_err_cols.matching_col14, ERR_DESCRIPTION 
= matching_col14 + " does not match value in " + source_db_jdbc_table_name2  )}
else{ insert into mismatches_df_1 VALUES(DF1_with_err_cols.matching_col14 as matching_col14) }

因此,除了在每个表/数据帧中执行1-100次之外,还没有创建循环的方法?还是针对每种类型的函数都基于列的位置?

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

git push heroku master