[英]How to create Multiple Data frames Dynamically in a For Loop in Spark Scala
I have a Array[string] which has the value ["DF1" , "DF2", "DF3"]我有一个 Array[string] 其值为 ["DF1" , "DF2", "DF3"]
I have values like below in my config file.我的配置文件中有如下值。
DF1=select * from TB1
DF2=select * from TB2
DF3=select * from TB3
I need to create 3 Data frames dynamically in a For loop by unloading the respective tables.我需要通过卸载相应的表在 For 循环中动态创建 3 个数据帧。
lets say my Array name is ARRAY1.假设我的数组名称是 ARRAY1。
My code would be我的代码是
for (for1 <- ARRAY1){val $for1+_DF = spark.sql(for1)}
Above is just kind of pseudo code.以上只是一种伪代码。
please help me by providing the correct code syntax.请通过提供正确的代码语法来帮助我。
Thanks, Naveen谢谢,纳文
Check below code.检查下面的代码。
scala> val queries = Map(
"df1" -> "select * from tb1",
"df2" -> "select * from TB2",
"df3" -> "select * from TB3"
) // After reading from config file.
scala>
queries
.values
.par
.map(spark.sql)
.foreach(_.show(false))
// Used .par for parallel loading & all three DataFrame object will be in list & you can do any operation on that, here i am using show function to display values
+---+
|id |
+---+
|1 |
|2 |
|3 |
|4 |
|5 |
|6 |
|7 |
|8 |
|9 |
+---+
+---+
|id |
+---+
|1 |
|2 |
|3 |
|4 |
|5 |
|6 |
|7 |
|8 |
|9 |
+---+
+---+
|id |
+---+
|1 |
|2 |
|3 |
|4 |
|5 |
|6 |
|7 |
|8 |
|9 |
+---+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.