import duckdb
import pandas as pd
from sqlalchemy import create_engine
# 定義連接到您的 MySQL 或 PostgreSQL 數(shù)據(jù)庫(kù)的參數(shù)
db_type = 'mysql' ?# 或 'postgresql'
user = 'your_username'
password = 'your_password'
host = 'your_host'
port = 'your_port'
database = 'your_database'
table_name = 'your_table'
# 創(chuàng)建 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 讀取數(shù)據(jù)
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\')')文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-822535.html
print("數(shù)據(jù)已成功導(dǎo)出為 Parquet 格式")
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-822535.html
到了這里,關(guān)于python導(dǎo)出數(shù)據(jù)為parquet格式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!