|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::collections::HashMap; |
| 19 | + |
| 20 | +use futures::stream::StreamExt; |
| 21 | +use iceberg::{Catalog, NamespaceIdent, TableIdent}; |
| 22 | +use iceberg_catalog_rest::{RestCatalog, RestCatalogConfig}; |
| 23 | + |
| 24 | +// Configure these values according to your environment |
| 25 | + |
| 26 | +static REST_URI: &str = "http://127.0.0.1:8181"; |
| 27 | +static NAMESPACE: &str = "default"; |
| 28 | +static TABLE_NAME: &str = "t1"; |
| 29 | +static OSS_ENDPOINT: &str = "https://oss-cn-hangzhou.aliyuncs.com/bucket"; |
| 30 | +static OSS_ACCESS_KEY_ID: &str = "LTAI5t999999999999999"; |
| 31 | +static OSS_ACCESS_KEY_SECRET: &str = "99999999999999999999999999999999"; |
| 32 | + |
| 33 | +/// This is a simple example that demonstrates how to use [`RestCatalog`] to read data from OSS. |
| 34 | +/// |
| 35 | +/// The demo reads data from an existing table in OSS storage. |
| 36 | +/// |
| 37 | +/// A running instance of the iceberg-rest catalog on port 8181 is required. You can find how to run |
| 38 | +/// the iceberg-rest catalog with `docker compose` in the official |
| 39 | +/// [quickstart documentation](https://iceberg.apache.org/spark-quickstart/). |
| 40 | +/// |
| 41 | +/// The example also requires valid OSS credentials and endpoint to be configured. |
| 42 | +#[tokio::main] |
| 43 | +async fn main() { |
| 44 | + // Create the REST iceberg catalog. |
| 45 | + let config = RestCatalogConfig::builder() |
| 46 | + .uri(REST_URI.to_string()) |
| 47 | + .props(HashMap::from([ |
| 48 | + ( |
| 49 | + iceberg::io::OSS_ENDPOINT.to_string(), |
| 50 | + OSS_ENDPOINT.to_string(), |
| 51 | + ), |
| 52 | + ( |
| 53 | + iceberg::io::OSS_ACCESS_KEY_ID.to_string(), |
| 54 | + OSS_ACCESS_KEY_ID.to_string(), |
| 55 | + ), |
| 56 | + ( |
| 57 | + iceberg::io::OSS_ACCESS_KEY_SECRET.to_string(), |
| 58 | + OSS_ACCESS_KEY_SECRET.to_string(), |
| 59 | + ), |
| 60 | + ])) |
| 61 | + .build(); |
| 62 | + let catalog = RestCatalog::new(config); |
| 63 | + |
| 64 | + // Create the table identifier. |
| 65 | + let namespace_ident = NamespaceIdent::from_vec(vec![NAMESPACE.to_string()]).unwrap(); |
| 66 | + let table_ident = TableIdent::new(namespace_ident.clone(), TABLE_NAME.to_string()); |
| 67 | + |
| 68 | + // Check if the table exists. |
| 69 | + if !catalog.table_exists(&table_ident).await.unwrap() { |
| 70 | + println!("Table {TABLE_NAME} must exists."); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + let table = catalog.load_table(&table_ident).await.unwrap(); |
| 75 | + println!("Table {TABLE_NAME} loaded!"); |
| 76 | + |
| 77 | + let scan = table.scan().select_all().build().unwrap(); |
| 78 | + let reader = scan.to_arrow().await.unwrap(); |
| 79 | + let buf = reader.collect::<Vec<_>>().await; |
| 80 | + |
| 81 | + println!("Table {TABLE_NAME} has {} batches.", buf.len()); |
| 82 | + |
| 83 | + assert!(!buf.is_empty()); |
| 84 | + assert!(buf.iter().all(|x| x.is_ok())); |
| 85 | +} |
0 commit comments