【问题标题】:Loop through the list which has queries to be executed and appended to dataframe循环遍历要执行并附加到数据帧的查询的列表
【发布时间】:2021-02-09 15:26:30
【问题描述】:
我需要遍历列表中的每个元素并对数据库运行此查询并将结果附加到相同的数据帧 (df)。请告诉我如何实现这一目标。
PS:我正在为此使用 spark scala。
List((select * from table1 where a=10 ) as rules,
(select * from table1 where b=10) as rules,
(select * from table1 where c=10 ) as rules)
谢谢。
【问题讨论】:
标签:
scala
apache-spark
apache-spark-sql
【解决方案1】:
当您从同一个表 table1 加载数据时,您可以简单地在 where 子句中使用多个条件:
val df = spark.sql("select * from table1 where a=10 or b=10 or c=10")
如果查询在不同的表上,您可以加载到数据框列表中,然后联合:
val queries = List(
"select * from table1 where a=10",
"select * from table1 where b=10",
"select * from table1 where c=10"
)
val df = queries.map(spark.sql).reduce(_ unionAll _)