|
| 1 | +//! PostgreSQL catalog example |
| 2 | +//! |
| 3 | +//! ## Usage |
| 4 | +//! |
| 5 | +//! Default (connects to localhost:5432): |
| 6 | +//! ```bash |
| 7 | +//! cargo run --example postgres_catalog --features metadata-postgres |
| 8 | +//! ``` |
| 9 | +//! |
| 10 | +//! Custom PostgreSQL connection: |
| 11 | +//! ```bash |
| 12 | +//! POSTGRES_URL="postgresql://user:pass@host:5432/dbname" \ |
| 13 | +//! cargo run --example postgres_catalog --features metadata-postgres |
| 14 | +//! ``` |
| 15 | +
|
| 16 | +use datafusion::prelude::*; |
| 17 | +use datafusion_ducklake::{DuckLakeCatalog, PostgresMetadataProvider}; |
| 18 | +use std::sync::Arc; |
| 19 | + |
| 20 | +#[cfg(feature = "metadata-postgres")] |
| 21 | +#[tokio::main] |
| 22 | +async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 23 | + use std::env; |
| 24 | + |
| 25 | + println!("==> PostgreSQL catalog example\n"); |
| 26 | + |
| 27 | + let conn_str = env::var("POSTGRES_URL").unwrap_or_else(|_| { |
| 28 | + "postgresql://postgres:postgres@localhost:5432/ducklake_catalog".to_string() |
| 29 | + }); |
| 30 | + println!("Connecting to: {}", conn_str); |
| 31 | + |
| 32 | + let provider = PostgresMetadataProvider::new(&conn_str).await?; |
| 33 | + println!("✓ Connected\n"); |
| 34 | + |
| 35 | + let catalog = DuckLakeCatalog::new(provider)?; |
| 36 | + let ctx = SessionContext::new(); |
| 37 | + ctx.register_catalog("ducklake", Arc::new(catalog)); |
| 38 | + |
| 39 | + println!("==> Schemas:"); |
| 40 | + ctx.sql("SELECT * FROM ducklake.information_schema.schemata") |
| 41 | + .await? |
| 42 | + .show() |
| 43 | + .await?; |
| 44 | + |
| 45 | + println!("\n==> Tables:"); |
| 46 | + ctx.sql("SELECT * FROM ducklake.information_schema.tables") |
| 47 | + .await? |
| 48 | + .show() |
| 49 | + .await?; |
| 50 | + |
| 51 | + println!("\n==> Snapshots:"); |
| 52 | + ctx.sql("SELECT * FROM ducklake.information_schema.snapshots") |
| 53 | + .await? |
| 54 | + .show() |
| 55 | + .await?; |
| 56 | + |
| 57 | + println!("\n✓ Example completed successfully"); |
| 58 | + println!("\nNext steps:"); |
| 59 | + println!(" 1. Populate the catalog with schemas, tables, and data files"); |
| 60 | + println!(" 2. Query your DuckLake tables using SQL"); |
| 61 | + println!(" 3. Enjoy PostgreSQL metadata catalog support!"); |
| 62 | + |
| 63 | + Ok(()) |
| 64 | +} |
| 65 | + |
| 66 | +#[cfg(not(feature = "metadata-postgres"))] |
| 67 | +fn main() { |
| 68 | + eprintln!("Error: This example requires the 'metadata-postgres' feature"); |
| 69 | + eprintln!("Run with: cargo run --example postgres_catalog --features metadata-postgres"); |
| 70 | + std::process::exit(1); |
| 71 | +} |
0 commit comments