要实现这个功能,需要借助于pandas的ExcelWriter()方法,参考如下:
from impala.util import as_pandas
# 把数据库中表字段写入到Excel文件中便于查看
curs.execute("""describe ods.table1""")
media = as_pandas(curs)
curs.execute("""describe ods.table2""")
customers = as_pandas(curs)
curs.execute("""describe ods.table3""")
orders = as_pandas(curs)
with pd.ExcelWriter('./潜客表字段.xlsx') as writer:
media.to_excel(writer,encoding='utf-8',sheet_name='table1')
customers.to_excel(writer,encoding='utf-8',sheet_name='table2')
orders.to_excel(writer,encoding='utf-8',sheet_name='table3')
print('done')
1. as_pandas包
其中as_pandas是impala.util中的一个非常好用的包,可以直接将数据库读出的结果转为dataframe。
2. describe table查看表结构
这是impala支持的语法,可以查看表结构。spark sql的语法是desc table,有所不同需要注意。
关注微信公众号:数据分析师手记
数据分析之家联合JEE RAY品牌为粉丝派发福利
添加粉丝福利派发官,领取粉丝福利哦