我可以使用PySpark运行单个SQL查询。例如
spark = SparkSession.builder.appNmae("___").getOrCreate()
df = spark.sql("select * from ...")
但是我有一个.sql文件,其中包含许多我想运行的查询。我是否必须一次执行一次,还是有一种简单的方法可以运行所有这些程序?
示例:表1中有3行,3列,表2中相同
.sql文件
select * from table1;
select * from table2;
输出df:
A B C
0 1 2 3
1 4 5 6
2 7 8 9
3 a b c
4 d e f
5 g h i
要么
df1
A B C
0 1 2 3
1 4 5 6
2 7 8 9
和df2
A B C
0 a b c
1 d e f
2 g h i
如果要将所有查询结果合并到数据帧列表中(假设每一行都是一个查询)
with open('/path/to/file.sql', 'r') as f:
query = f.readlines()
dfs = []
for line in query:
dfs.append(spark.sql(line))
如果要合并所有数据框(假设它们都具有相同的架构)
from functools import reduce
df = reduce(lambda x, y: x.union(y), dfs)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句