import duckdb
import pandas as pd
from sqlalchemy import create_engine
# 定义连接到您的 MySQL 或 PostgreSQL 数据库的参数
db_type = 'mysql' # 或 'postgresql'
user = 'your_username'
password = 'your_password'
host = 'your_host'
port = 'your_port'
database = 'your_database'
table_name = 'your_table'
# 创建 SQLAlchemy 引擎
if db_type == 'mysql':
engine = create_engine(f'mysql+pymysql://{user}:{password}@{host}:{port}/{database}')
else: # postgresql
engine = create_engine(f'postgresql+psycopg2://{user}:{password}@{host}:{port}/{database}')
# 从 MySQL/PostgreSQL 读取数据
with engine.connect() as conn:
query = f'SELECT * FROM {table_name}'
df = pd.read_sql(query, conn)
# 使用 DuckDB
con = duckdb.connect(database=':memory:')
con.execute(f'CREATE TABLE {table_name} AS SELECT * FROM df')
con.execute(f'COPY {table_name} TO \'output.parquet\' (FORMAT \'parquet\')')文章来源:https://www.toymoban.com/news/detail-822535.html
print("数据已成功导出为 Parquet 格式")
文章来源地址https://www.toymoban.com/news/detail-822535.html
到了这里,关于python导出数据为parquet格式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!