DataFrame.to_sql 需要指定分区列,因为目标表已分区

问题描述 投票:0回答:1

你知道如何将 python 数据帧数据插入到分区 SQL 中吗?

此代码适用于非分区表。

df.to_sql('my_table', engine, if_exists='append', method='multi')
python pandas sqlalchemy
1个回答
0
投票

我也遇到了同样的问题,这是我的解决方案。
首先将数据插入到没有分区的临时表中,然后从临时表中选择所有数据插入到指定分区中。

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""")

© www.soinside.com 2019 - 2024. All rights reserved.