我有一个List:hdtList
,其中包含代表Hive表的列的列:
forecast_id bigint,period_year bigint,period_num bigint,period_name string,drm_org string,ledger_id bigint,currency_code string,source_system_name string,source_record_type string,gl_source_name string,gl_source_system_name string,year string
我有一个List: partition_columns
,其中包含两个元素:source_system_name, period_year
使用List: partition_columns
,我试图匹配它们并将List: hdtList
中的相应列移动到其末尾,如下所示:
val (pc, notPc) = hdtList.partition(c => partition_columns.contains(c.takeWhile(x => x != ' ')))
但是当我将它们打印为:println(notPc.mkString(",") + "," + pc.mkString(","))
我看到输出无序如下:
forecast_id bigint,period_num bigint,period_name string,drm_org string,ledger_id bigint,currency_code string,source_record_type string,gl_source_name string,gl_source_system_name string,year string,period string,period_year bigint,source_system_name string
列period_year
在前,列source_system_name
在后。无论如何,我可以按照以下方式进行数据处理,以便保持List: partition_columns
中的列顺序。
forecast_id bigint,period_num bigint,period_name string,drm_org string,ledger_id bigint,currency_code string,source_record_type string,gl_source_name string,gl_source_system_name string,year string,period string,source_system_name string,period_year bigint
我知道有一个反向列表的选项,但是我想了解我是否可以实现一个保持插入顺序的集合。
答案 0 :(得分:2)
使用哪个集合都没有关系;您仅使用partition_columns
来调用contains
,而这并不取决于其顺序,那么如何维护它呢?
但是您的代码确实保持顺序:只是hdtList
。
类似
// get is ugly, but safe here
val pc1 = partition_columns.map(x => pc.find(y => y.startsWith(x)).get)
您的代码将为您提供所需的顺序,尽管可能有更有效的方法。