scala - 如何将多个 Spark 数据帧转换为 Dataset[Map[String, Array]]?
问题描述
我需要获取 Map[String, DataFrame] 并将其转换为 Dataset[Map[String, Array]]
val map_of_df = Map(
"df1"->sc.parallelize(1 to 4).map(i => (i,i*1000)).toDF("id","x").repartition(4)
,"df2"->sc.parallelize(1 to 4).map(i => (i,i*100)).toDF("id","y").repartition(4)
)
//map_of_df: scala.collection.immutable.Map[String,org.apache.spark.sql.Dataset[org.apache.spark.sql.Row]] = Map(df1 -> [id: int, x: int], df2 -> [id: int, y: int])
//magic here, I need a type of org.apache.spark.sql.Dataset[Map[String, Array[org.apache.spark.sql.Row]]] with four partitions
//where the keys to the map are "df1" and "df2"
解决方案
你只是collect
所有的DataFrames:
map_of_df
.mapValues(_.collect())
.toSeq
.toDS
请记住,这不会扩展 - 所有数据都将加载到驱动程序内存中。换句话说,您不需要 Spark。
推荐阅读
- java - 如何在 ScrollPane 中创建流畅的 2 列 GridLayout?
- python - 当我运行scrapy时,它显示一个错误,如 __import__(name) ImportError: No module named home in ubuntu
- r - r-闪亮的服务器选择输入
- strapi - 将产品 API 密钥作为产品添加到 Strapi
- mongodb - MacOS Catalina 无法连接到 Google Compute Engine 上的 MONGODB 服务器
- javascript - 如何停止放大和缩小反应?
- reactjs - “react-native init”生成的“App”声明中发生了什么?
- android - 为什么android在多次构建后显示错误的向量
- android - 只有使用两个组合关键字搜索时才能找到应用名称
- date - 如何获得上周日的日期?