DuckDB
此功能处于测试阶段
预计会有快速变化、功能有限,并可能出现破坏性更新。随着我们改进体验并扩大访问,欢迎分享反馈。
🌐 Expect rapid changes, limited features, and possible breaking updates. Share feedback as we refine the experience and expand access.
DuckDB 是一个高性能的 SQL 数据库系统,针对分析型工作负载进行了优化。它可以直接查询存储在你的分析桶中的 Iceberg 表,非常适合数据探索和复杂的分析查询。
🌐 DuckDB is a high-performance SQL database system optimized for analytical workloads. It can directly query Iceberg tables stored in your analytics buckets, making it ideal for data exploration and complex analytical queries.
安装 #
🌐 Installation
安装 DuckDB 和 Iceberg 扩展:
🌐 Install DuckDB and the Iceberg extension:
1pip install duckdb duckdb-iceberg正在连接到分析存储桶 #
🌐 Connecting to Analytics buckets
这是一个完整的例子,展示如何连接到你的 Supabase 分析存储桶并查询 Iceberg 表:
🌐 Here's a complete example of connecting to your Supabase analytics bucket and querying Iceberg tables:
1import duckdb2import os34# Configuration5PROJECT_REF = "your-project-ref"6WAREHOUSE = "your-analytics-bucket-name"7SERVICE_KEY = "your-service-key"89# S3 credentials10S3_ACCESS_KEY = "your-access-key"11S3_SECRET_KEY = "your-secret-key"12S3_REGION = "us-east-1"1314# Construct endpoints15S3_ENDPOINT = f"https://{PROJECT_REF}.supabase.co/storage/v1/s3"16CATALOG_URI = f"https://{PROJECT_REF}.supabase.co/storage/v1/iceberg"1718# Initialize DuckDB connection19conn = duckdb.connect(":memory:")2021# Install and load the Iceberg extension22conn.install_extension("iceberg")23conn.load_extension("iceberg")2425# Configure Iceberg catalog with Supabase credentials26conn.execute(f"""27 CREATE SECRET (28 TYPE S3,29 KEY_ID '{S3_ACCESS_KEY}',30 SECRET '{S3_SECRET_KEY}',31 REGION '{S3_REGION}',32 ENDPOINT '{S3_ENDPOINT}',33 URL_STYLE 'virtual'34 );35""")3637# Configure the REST catalog38conn.execute(f"""39 ATTACH 'iceberg://{CATALOG_URI}' AS iceberg_catalog40 (41 TYPE ICEBERG_REST,42 WAREHOUSE '{WAREHOUSE}',43 TOKEN '{SERVICE_KEY}'44 );45""")4647# Query your Iceberg tables48result = conn.execute("""49 SELECT *50 FROM iceberg_catalog.default.events51 LIMIT 1052""").fetchall()5354for row in result:55 print(row)5657# Complex aggregation example58analytics = conn.execute("""59 SELECT60 event_name,61 COUNT(*) as event_count,62 COUNT(DISTINCT user_id) as unique_users63 FROM iceberg_catalog.default.events64 GROUP BY event_name65 ORDER BY event_count DESC66""").fetchdf()6768print(analytics)DuckDB 的主要特点 #
🌐 Key features with DuckDB
高效的数据探索 #
🌐 Efficient data exploration
DuckDB 的惰性计算意味着它只会扫描你需要的数据:
🌐 DuckDB's lazy evaluation means it only scans the data you need:
1# This only reads the columns you select2events = conn.execute("""3 SELECT event_id, event_name, event_timestamp4 FROM iceberg_catalog.default.events5 WHERE event_timestamp > NOW() - INTERVAL '7 days'6""").fetchdf()转换为 Pandas #
🌐 Converting to Pandas
把结果转换成 Pandas DataFrame 以便进一步分析:
🌐 Convert results to Pandas DataFrames for further analysis:
1df = conn.execute("""2 SELECT *3 FROM iceberg_catalog.default.events4""").fetchdf()56# Use pandas for visualization or further processing7print(df.describe())导出结果 #
🌐 Exporting results
把你的分析结果保存为多种格式:
🌐 Save your analytical results to various formats:
1# Export to Parquet2conn.execute("""3 COPY (4 SELECT * FROM iceberg_catalog.default.events5 ) TO 'results.parquet'6""")78# Export to CSV9conn.execute("""10 COPY (11 SELECT event_name, COUNT(*) as count12 FROM iceberg_catalog.default.events13 GROUP BY event_name14 ) TO 'summary.csv' (FORMAT CSV, HEADER true)15""")最佳实践 #
🌐 Best practices
- 连接池 - 为多个查询重复使用连接
- 分区剪枝 - 通过分区列过滤以提高查询性能
- 列选择 - 只选择你需要的列以减少 I/O
- 限制结果 - 在探索时使用 LIMIT 以避免处理大型数据集
下一步 #
🌐 Next steps