你知道如何将 python 数据帧数据插入到分区 SQL 中吗?
此代码适用于非分区表。
df.to_sql('my_table', engine, if_exists='append', method='multi')
我也遇到了同样的问题,这是我的解决方案。
首先将数据插入到没有分区的临时表中,然后从临时表中选择所有数据插入到指定分区中。
from pyhive import hive
conn = hive.connect(host='localhost',
port=10000,
username='your_username',
database='your_database')
df.to_sql(f'xxxx_tmp', hive_engine, if_exists='replace', index=False, method='multi')
with conn.cursor() as cursor:
cursor.execute(f"""insert overwrite table xxxx partition(dt=20240326) select * from xxxx_tmp""")