简体   繁体   English

Scala Spark Join Dataframe in loop

[英]Scala Spark Join Dataframe in loop

I am trying to join DataFrames on the fly in loop.我正在尝试在循环中即时加入DataFrames I am using a properties file to get the column details to use in the final data frame.我正在使用属性文件来获取要在最终数据框中使用的列详细信息。 Properties file -属性文件 -

a01=status:single,perm_id:multi
a02=status:single,actv_id:multi
a03=status:single,perm_id:multi,actv_id:multi
............................
............................

For each row in the properties file, I need to create a DataFrame and save it in a file.对于属性文件中的每一行,我需要创建一个 DataFrame 并将其保存在一个文件中。 Loading the properties file using PropertiesReader .使用PropertiesReader加载属性文件。 if the mode is single then I need to get only the column value from the table.如果模式是单一的,那么我只需要从表中获取列值。 But if multi, then I need to get the list of values.但如果多,那么我需要获取值列表。

val propertyColumn = properties.get("a01") //a01 value we are getting as an argument. This might be a01,a02 or a0n
val columns = propertyColumn.toString.split(",").map(_.toString)

act_det table - act_det 表 -

+-------+--------+-----------+-----------+-----------+------------+
|id     |act_id  |status     |perm_id    |actv_id    | debt_id    |
+-------+--------+-----------+-----------+-----------+------------+
| 1     |1       |   4       | 1         | 10        | 1          |
+-------+--------+-----------+-----------+-----------+------------+
| 2     |1       |   4       | 2         | 20        | 2          |
+-------+--------+-----------+-----------+-----------+------------+
| 3     |1       |   4       | 3         | 30        | 1          |
+-------+--------+-----------+-----------+-----------+------------+
| 4     |2       |   4       | 5         | 10        | 3          |
+-------+--------+-----------+-----------+-----------+------------+
| 5     |2       |   4       | 6         | 20        | 1          |
+-------+--------+-----------+-----------+-----------+------------+
| 6     |2       |   4       | 7         | 30        | 1          |
+-------+--------+-----------+-----------+-----------+------------+
| 7     |3       |   4       | 1         | 10        | 3          |
+-------+--------+-----------+-----------+-----------+------------+
| 8     |3       |   4       | 5         | 20        | 1          |
+-------+--------+-----------+-----------+-----------+------------+
| 9     |3       |   4       | 2         | 30        | 3          |
+-------+--------+-----------+-----------+------------+-----------+

Main DataFrame -主DataFrame-

val data = sqlContext.sql("select * from act_det") val data = sqlContext.sql("select * from act_det")

I want the following output -我想要以下 output -

For a01 -对于 a01 -

+-------+--------+-----------+
|act_id |status  |perm_id    |
+-------+--------+-----------+
|     1 |   4    | [1,2,3]   |
+-------+--------+-----------+
|     2 |   4    |  [5,6,7]  |
+-------+--------+-----------+
|     3 |   4    |  [1,5,2]  |
+-------+--------+-----------+

For a02 -对于 a02 -

    +-------+--------+-----------+
    |act_id |status  |actv_id    |
    +-------+--------+-----------+
    |     1 |   4    | [10,20,30]|
    +-------+--------+-----------+
    |     2 |   4    | [10,20,30]|
    +-------+--------+-----------+
    |     3 |   4    | [10,20,30]|
    +-------+--------+-----------+

For a03 -对于 a03 -

    +-------+--------+-----------+-----------+
    |act_id |status  |perm_id    |actv_id    |
    +-------+--------+-----------+-----------+
    |     1 |   4    | [1,2,3]   |[10,20,30] |
    +-------+--------+-----------+-----------+
    |     2 |   4    |  [5,6,7]  |[10,20,30] |
    +-------+--------+-----------+-----------+
    |     3 |   4    |  [1,5,2]  |[10,20,30] |
    +-------+--------+-----------+-----------+

But the data frame creation process should be dynamic.但是数据框创建过程应该是动态的。

I have tried below code but I am not able to implement the join logic for the DataFrames in loop.我已经尝试过下面的代码,但我无法在循环中实现 DataFrames 的连接逻辑。

val finalDF:DataFrame = ??? //empty dataframe
    for {
        column <- columns
    } yeild {
        val eachColumn = column.toString.split(":").map(_.toString)
        val columnName = eachColumn(0)
        val mode = eachColumn(1)
        if(mode.equalsIgnoreCase("single")) {
            data.select($"act_id", $"status").distinct
            //I want to join finalDF with data.select($"act_id", $"status").distinct
        } else if(mode.equalsIgnoreCase("multi")) {
            data.groupBy($"act_id").agg(collect_list($"perm_id").as("perm_id"))
            //I want to join finalDF with data.groupBy($"act_id").agg(collect_list($"perm_id").as("perm_id"))
        }
    }

Any advice or guidance would be greatly appreciated.任何建议或指导将不胜感激。

Check below code.检查下面的代码。

scala> df.show(false)
+---+------+------+-------+-------+-------+
|id |act_id|status|perm_id|actv_id|debt_id|
+---+------+------+-------+-------+-------+
|1  |1     |4     |1      |10     |1      |
|2  |1     |4     |2      |20     |2      |
|3  |1     |4     |3      |30     |1      |
|4  |2     |4     |5      |10     |3      |
|5  |2     |4     |6      |20     |1      |
|6  |2     |4     |7      |30     |1      |
|7  |3     |4     |1      |10     |3      |
|8  |3     |4     |5      |20     |1      |
|9  |3     |4     |2      |30     |3      |
+---+------+------+-------+-------+-------+

Defining primary keys定义primary keys

scala> val primary_key = Seq("act_id").map(col(_))
primary_key: Seq[org.apache.spark.sql.Column] = List(act_id)

Configs配置

scala> configs.foreach(println)
/*
(a01,status:single,perm_id:multi)
(a02,status:single,actv_id:multi)
(a03,status:single,perm_id:multi,actv_id:multi)
*/

Constructing Expression.构造表达式。

scala> 
val columns = configs
                .map(c => {
                    c._2
                    .split(",")
                    .map(c => {
                            val cc = c.split(":"); 
                            if(cc.tail.contains("single")) 
                                first(col(cc.head)).as(cc.head) 
                            else 
                                collect_list(col(cc.head)).as(cc.head)
                        }
                    )
                })

/*
columns: scala.collection.immutable.Iterable[Array[org.apache.spark.sql.Column]] = List(
    Array(first(status, false) AS `status`, collect_list(perm_id) AS `perm_id`), 
    Array(first(status, false) AS `status`, collect_list(actv_id) AS `actv_id`), 
    Array(first(status, false) AS `status`, collect_list(perm_id) AS `perm_id`, collect_list(actv_id) AS `actv_id`)
)
*/

Final Result最后结果

scala> columns.map(c => df.groupBy(primary_key:_*).agg(c.head,c.tail:_*)).map(_.show(false))
+------+------+---------+
|act_id|status|perm_id  |
+------+------+---------+
|3     |4     |[1, 5, 2]|
|1     |4     |[1, 2, 3]|
|2     |4     |[5, 6, 7]|
+------+------+---------+

+------+------+------------+
|act_id|status|actv_id     |
+------+------+------------+
|3     |4     |[10, 20, 30]|
|1     |4     |[10, 20, 30]|
|2     |4     |[10, 20, 30]|
+------+------+------------+

+------+------+---------+------------+
|act_id|status|perm_id  |actv_id     |
+------+------+---------+------------+
|3     |4     |[1, 5, 2]|[10, 20, 30]|
|1     |4     |[1, 2, 3]|[10, 20, 30]|
|2     |4     |[5, 6, 7]|[10, 20, 30]|
+------+------+---------+------------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM