scala - 如何在 Spark Scala 的 For 循环中动态创建多个数据帧
问题描述
我有一个 Array[string] ,其值为 ["DF1" , "DF2", "DF3"]
我的配置文件中有如下值。
DF1=select * from TB1
DF2=select * from TB2
DF3=select * from TB3
我需要通过卸载相应的表在 For 循环中动态创建 3 个数据框。
假设我的数组名称是 ARRAY1。
我的代码是
for (for1 <- ARRAY1){val $for1+_DF = spark.sql(for1)}
以上只是一种伪代码。
请通过提供正确的代码语法来帮助我。
谢谢,纳文
解决方案
检查下面的代码。
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 |
+---+
推荐阅读
- java - 在 Apache JsonToStringStyle 中格式化日期
- python - 为什么“浏览”方法使用不存在的 id 返回错误的记录集?
- javascript - react-native 的水平滑块插件
- json - 如何从文件中获取所有 JSON 名称并将其存储到字符串中
- css - 为什么相同厚度和相同位置的边框不完全重叠?
- vegan - 为什么在运行 adonis 函数(permanova)时,df 总是得到 1?
- sharepoint - 如何为 Sharepoint Online 2018 中的文档创建匿名链接
- mysql - 如何在 Laravel 中查询和显示结果
- ionic-framework - Google Flutter 能否在一个应用中针对 iOS 和 Android
- apache-kafka - 如何在单个异步调用中将多条消息推送到一个主题