-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add simple operations example (#953)
# Description Adding a simple example how to use operations APIs to create and work with delta tables and some minor documentation tweaks. # Related Issue(s) <!--- For example: - closes #106 ---> # Documentation <!--- Share links to useful documentation ---> Co-authored-by: Will Jones <willjones127@gmail.com>
- Loading branch information
Showing
6 changed files
with
159 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use arrow::{ | ||
array::{Int32Array, StringArray}, | ||
datatypes::{DataType, Field, Schema as ArrowSchema}, | ||
record_batch::RecordBatch, | ||
}; | ||
use deltalake::operations::collect_sendable_stream; | ||
use deltalake::{action::SaveMode, DeltaOps, SchemaDataType, SchemaField}; | ||
use std::sync::Arc; | ||
|
||
fn get_table_columns() -> Vec<SchemaField> { | ||
vec![ | ||
SchemaField::new( | ||
String::from("int"), | ||
SchemaDataType::primitive(String::from("integer")), | ||
false, | ||
Default::default(), | ||
), | ||
SchemaField::new( | ||
String::from("string"), | ||
SchemaDataType::primitive(String::from("string")), | ||
true, | ||
Default::default(), | ||
), | ||
] | ||
} | ||
|
||
fn get_table_batches() -> RecordBatch { | ||
let schema = Arc::new(ArrowSchema::new(vec![ | ||
Field::new("int", DataType::Int32, false), | ||
Field::new("string", DataType::Utf8, true), | ||
])); | ||
|
||
let int_values = Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); | ||
let str_values = StringArray::from(vec!["A", "B", "A", "B", "A", "A", "A", "B", "B", "A", "A"]); | ||
|
||
RecordBatch::try_new(schema, vec![Arc::new(int_values), Arc::new(str_values)]).unwrap() | ||
} | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
async fn main() -> Result<(), deltalake::DeltaTableError> { | ||
// Create a delta operations client pointing at an un-initialized in-memory location. | ||
// In a production environment this would be created with "try_new" and point at | ||
// a real storage location. | ||
let ops = DeltaOps::new_in_memory(); | ||
|
||
// The operations module uses a builder pattern that allows specifying several options | ||
// on how the command behaves. The builders implement `Into<Future>`, so once | ||
// options are set you can run the command using `.await`. | ||
let table = ops | ||
.create() | ||
.with_columns(get_table_columns()) | ||
.with_table_name("my_table") | ||
.with_comment("A table to show how delta-rs works") | ||
.await?; | ||
|
||
assert_eq!(table.version(), 0); | ||
|
||
let batch = get_table_batches(); | ||
let table = DeltaOps(table).write(vec![batch.clone()]).await?; | ||
|
||
assert_eq!(table.version(), 1); | ||
|
||
// To overwrite instead of append (which is the default), use `.with_save_mode`: | ||
let table = DeltaOps(table) | ||
.write(vec![batch.clone()]) | ||
.with_save_mode(SaveMode::Overwrite) | ||
.await?; | ||
|
||
assert_eq!(table.version(), 2); | ||
|
||
let (_table, stream) = DeltaOps(table).load().await?; | ||
let data: Vec<RecordBatch> = collect_sendable_stream(stream).await?; | ||
|
||
println!("{:?}", data); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters