|
| 1 | +/*! |
| 2 | +Example demonstrating snapshot expiration functionality |
| 3 | +
|
| 4 | +This example shows how to use the expire_snapshots API to clean up old snapshots |
| 5 | +from an Iceberg table. Note that this is a conceptual example - in practice, you |
| 6 | +would need a fully configured table with a catalog and object store. |
| 7 | +*/ |
| 8 | + |
| 9 | +use std::error::Error; |
| 10 | + |
| 11 | +#[tokio::main] |
| 12 | +async fn main() -> Result<(), Box<dyn Error>> { |
| 13 | + // Note: This is a conceptual example. In practice, you would load a real table: |
| 14 | + // let mut table = catalog.load_table(&identifier).await?; |
| 15 | + |
| 16 | + println!("Iceberg Snapshot Expiration Example"); |
| 17 | + println!("=================================="); |
| 18 | + |
| 19 | + // Example 1: Expire snapshots older than 30 days |
| 20 | + println!("\n1. Expire snapshots older than 30 days:"); |
| 21 | + demonstrate_time_based_expiration().await; |
| 22 | + |
| 23 | + // Example 2: Keep only the last 10 snapshots |
| 24 | + println!("\n2. Keep only the last 10 snapshots:"); |
| 25 | + demonstrate_count_based_expiration().await; |
| 26 | + |
| 27 | + // Example 3: Combined criteria with orphan file cleanup |
| 28 | + println!("\n3. Combined criteria with file cleanup:"); |
| 29 | + demonstrate_combined_expiration().await; |
| 30 | + |
| 31 | + // Example 4: Dry run to preview what would be expired |
| 32 | + println!("\n4. Dry run to preview expiration:"); |
| 33 | + demonstrate_dry_run().await; |
| 34 | + |
| 35 | + Ok(()) |
| 36 | +} |
| 37 | + |
| 38 | +async fn demonstrate_time_based_expiration() { |
| 39 | + // Calculate timestamp for 30 days ago |
| 40 | + let thirty_days_ago = chrono::Utc::now().timestamp_millis() - 30 * 24 * 60 * 60 * 1000; |
| 41 | + |
| 42 | + println!(" expire_snapshots()"); |
| 43 | + println!(" .expire_older_than({}) // 30 days ago", thirty_days_ago); |
| 44 | + println!(" .execute().await"); |
| 45 | + println!(" // This would expire all snapshots older than 30 days"); |
| 46 | +} |
| 47 | + |
| 48 | +async fn demonstrate_count_based_expiration() { |
| 49 | + println!(" expire_snapshots()"); |
| 50 | + println!(" .retain_last(10)"); |
| 51 | + println!(" .execute().await"); |
| 52 | + println!(" // This would keep only the 10 most recent snapshots"); |
| 53 | +} |
| 54 | + |
| 55 | +async fn demonstrate_combined_expiration() { |
| 56 | + let seven_days_ago = chrono::Utc::now().timestamp_millis() - 7 * 24 * 60 * 60 * 1000; |
| 57 | + |
| 58 | + println!(" expire_snapshots()"); |
| 59 | + println!(" .expire_older_than({}) // 7 days ago", seven_days_ago); |
| 60 | + println!(" .retain_last(5) // But keep at least 5"); |
| 61 | + println!(" .clean_orphan_files(true) // Also delete unreferenced files"); |
| 62 | + println!(" .execute().await"); |
| 63 | + println!(" // This would expire snapshots older than 7 days, but always keep"); |
| 64 | + println!(" // the 5 most recent snapshots and clean up orphaned files"); |
| 65 | +} |
| 66 | + |
| 67 | +async fn demonstrate_dry_run() { |
| 68 | + println!(" let result = expire_snapshots()"); |
| 69 | + println!(" .retain_last(5)"); |
| 70 | + println!(" .dry_run(true) // Preview mode"); |
| 71 | + println!(" .execute().await?;"); |
| 72 | + println!(" "); |
| 73 | + println!(" println!(\"Would expire {{}} snapshots\", result.expired_snapshot_ids.len());"); |
| 74 | + println!(" println!(\"Would delete {{}} manifest files\", result.deleted_files.manifests.len());"); |
| 75 | + println!(" // No actual changes made in dry run mode"); |
| 76 | +} |
| 77 | + |
| 78 | +// This function would show a real example if we had a table instance |
| 79 | +#[allow(dead_code)] |
| 80 | +async fn real_expiration_example() -> Result<(), Box<dyn Error>> { |
| 81 | + // In a real implementation, you would: |
| 82 | + // 1. Load a table from your catalog |
| 83 | + // 2. Call expire_snapshots with your desired criteria |
| 84 | + // 3. Handle the results |
| 85 | + |
| 86 | + /* Example code (commented out since we don't have a real table): |
| 87 | + |
| 88 | + let mut table = catalog.load_table(&table_identifier).await?; |
| 89 | + |
| 90 | + let result = table.expire_snapshots() |
| 91 | + .expire_older_than(chrono::Utc::now().timestamp_millis() - 30 * 24 * 60 * 60 * 1000) |
| 92 | + .retain_last(10) |
| 93 | + .clean_orphan_files(true) |
| 94 | + .execute() |
| 95 | + .await?; |
| 96 | + |
| 97 | + println!("Expired {} snapshots", result.expired_snapshot_ids.len()); |
| 98 | + println!("Deleted {} manifest lists", result.deleted_files.manifest_lists.len()); |
| 99 | + println!("Deleted {} manifest files", result.deleted_files.manifests.len()); |
| 100 | + println!("Deleted {} data files", result.deleted_files.data_files.len()); |
| 101 | + |
| 102 | + if !result.expired_snapshot_ids.is_empty() { |
| 103 | + println!("Expired snapshot IDs: {:?}", result.expired_snapshot_ids); |
| 104 | + } |
| 105 | + */ |
| 106 | + |
| 107 | + Ok(()) |
| 108 | +} |
0 commit comments