Skip to content

Commit cc83ad2

Browse files
committed
feat: create_table functionality improved
1 parent 0eb6637 commit cc83ad2

File tree

1 file changed

+34
-21
lines changed
  • docs/data_engineering/data_lakehouse/delta_lake

1 file changed

+34
-21
lines changed

docs/data_engineering/data_lakehouse/delta_lake/rust.md

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,51 @@ This guide provides an introduction to using the Delta Lake Rust library (delta-
77
```toml
88
[dependencies]
99
deltalake = { version = "0.22.3", features = ["s3"] }
10-
tokio = "1.42.0"
1110
```
1211

1312
## Create table
1413

1514
```rust
1615
use deltalake::kernel::DataType;
17-
use deltalake::operations::create::CreateBuilder;
18-
use std::collections::HashMap;
16+
use deltalake::{DeltaOps, DeltaTable, DeltaTableBuilder, DeltaTableError};
17+
use std::env;
18+
19+
/// Builds a `DeltaOps` instance for the specified Delta table.
20+
/// Enabling operations such as creating, reading, and writing data in the Delta Lake format.
21+
fn get_delta_ops(table_name: &str) -> Result<DeltaOps, DeltaTableError> {
22+
let delta_table = DeltaTableBuilder::from_uri(format!("s3://data-lakehouse/{}", table_name)).build()?;
23+
24+
Ok(DeltaOps::from(delta_table))
25+
}
26+
27+
async fn create_table(table_name: &str) -> Result<DeltaTable, DeltaTableError> {
28+
let delta_ops = get_delta_ops(table_name)?;
29+
30+
let table = delta_ops
31+
.create()
32+
.with_table_name("employee")
33+
.with_column("id", DataType::INTEGER, false, Default::default())
34+
.with_column("name", DataType::STRING, false, Default::default())
35+
.await?;
36+
37+
Ok(table)
38+
}
1939

2040
#[tokio::main()]
2141
async fn main() {
42+
let table_name = "employee";
43+
44+
// Set S3 configuration options using environment variables
45+
env::set_var("AWS_ENDPOINT_URL", "http://localhost:5561");
46+
env::set_var("AWS_REGION", "us-east-1");
47+
env::set_var("AWS_ACCESS_KEY_ID", "admin");
48+
env::set_var("AWS_SECRET_ACCESS_KEY", "password");
49+
env::set_var("AWS_ALLOW_HTTP", "true");
50+
env::set_var("AWS_S3_ALLOW_UNSAFE_RENAME", "true");
51+
2252
// Register AWS S3 handlers for Delta Lake operations.
2353
deltalake::aws::register_handlers(None);
2454

25-
// Configure S3 storage parameters
26-
let mut storage_options = HashMap::new();
27-
storage_options.insert("AWS_ENDPOINT_URL".to_string(), "http://localhost:5561".to_string());
28-
storage_options.insert("AWS_REGION".to_string(), "us-east-1".to_string());
29-
storage_options.insert("AWS_ACCESS_KEY_ID".to_string(), "admin".to_string());
30-
storage_options.insert("AWS_SECRET_ACCESS_KEY".to_string(), "password".to_string());
31-
storage_options.insert("AWS_ALLOW_HTTP".to_string(), "true".to_string());
32-
storage_options.insert("AWS_S3_ALLOW_UNSAFE_RENAME".to_string(), "true".to_string());
33-
34-
// Create table
35-
CreateBuilder::new()
36-
.with_location("s3://data-lakehouse/".to_string())
37-
.with_storage_options(storage_options)
38-
.with_column("id", DataType::INTEGER, false, Default::default())
39-
.with_column("name", DataType::STRING, false, Default::default())
40-
.await
41-
.expect("Table creation failed");
55+
create_table(&table_name).await.expect("Table creation failed");
4256
}
43-
4457
```

0 commit comments

Comments
 (0)