1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
//! A native Rust implementation of [Apache Hudi](https://hudi.apache.org).
//!
//! This crate creates, writes, and reads Hudi tables on local disk or cloud object storage
//! (`s3://`, `az://`, `gs://`), exchanging data as Arrow [`RecordBatch`]es. It re-exports
//! [`hudi_core`], and with the `datafusion` feature also the DataFusion table provider from
//! `hudi-datafusion`. [`table::Table`] is the entry point for both reads and writes.
//!
//! # Resources
//!
//! - Quick start: the [Python and Rust quick start guide][quick start guide] on the Hudi website,
//! and the shorter [README quick start].
//! - Rust API reference: this crate, [`hudi-core`](https://docs.rs/hudi-core), and
//! [`hudi-datafusion`](https://docs.rs/hudi-datafusion).
//! - Python API reference: the docstrings in the package's type stubs,
//! [`python/hudi/_internal.pyi`][python stubs].
//! - Runnable Rust example: [`crates/core/examples/table_writes.rs`][rust example].
//! - Reader semantics: [`docs/reader-spec.md`][reader spec]. Writer design:
//! [`docs/writer-design.md`][writer design].
//! - Packages: [crates.io](https://crates.io/crates/hudi) and
//! [PyPI](https://pypi.org/project/hudi/). Source:
//! [github.com/apache/hudi-rs](https://github.com/apache/hudi-rs).
//! - Hudi concepts (timeline, table types, query types):
//! [hudi.apache.org/docs/overview](https://hudi.apache.org/docs/overview).
//!
//! [quick start guide]: https://hudi.apache.org/docs/python-rust-quick-start-guide
//! [README quick start]: https://github.com/apache/hudi-rs#quick-start
//! [python stubs]: https://github.com/apache/hudi-rs/blob/main/python/hudi/_internal.pyi
//! [rust example]: https://github.com/apache/hudi-rs/blob/main/crates/core/examples/table_writes.rs
//! [reader spec]: https://github.com/apache/hudi-rs/blob/main/docs/reader-spec.md
//! [writer design]: https://github.com/apache/hudi-rs/blob/main/docs/writer-design.md
//!
//! [`RecordBatch`]: https://docs.rs/arrow/latest/arrow/array/struct.RecordBatch.html
//!
//! # Features
//!
//! - `datafusion` (off by default): adds `HudiDataSource`, a DataFusion `TableProvider`, and
//! `HudiTableFactory` for `CREATE EXTERNAL TABLE ... STORED AS HUDI`.
//! - `spill-rocksdb` (on by default): the on-disk tier of the merge-on-read merge map, used when a
//! merge exceeds `hoodie.memory.merge.max.size`. RocksDB is built from source with `bindgen`, so
//! it needs `libclang` and a C++ toolchain. With `default-features = false` the tier is gone, and
//! a merge past the budget fails with [`CoreError::Unsupported`](error::CoreError::Unsupported)
//! naming the config instead of spilling.
//!
//! # Create a table and write to it
//!
//! A write that changes data is one Hudi commit. [`Table::append`](table::Table::append) inserts without an
//! index lookup; [`Table::upsert`](table::Table::upsert) updates rows whose record key exists and
//! inserts the rest; [`Table::delete`](table::Table::delete) and
//! [`Table::delete_keys`](table::Table::delete_keys) remove rows by predicate or by key.
//!
//! ```
//! # use std::sync::Arc;
//! # use arrow::array::{ArrayRef, Int64Array, RecordBatch, StringArray};
//! use hudi::config::table::TableTypeValue;
//! use hudi::table::{HoodieKey, Table};
//!
//! # fn trips(rows: &[(&str, &str, i64)]) -> RecordBatch {
//! # let id: ArrayRef = Arc::new(StringArray::from_iter_values(rows.iter().map(|r| r.0)));
//! # let city: ArrayRef = Arc::new(StringArray::from_iter_values(rows.iter().map(|r| r.1)));
//! # let ts: ArrayRef = Arc::new(Int64Array::from_iter_values(rows.iter().map(|r| r.2)));
//! # RecordBatch::try_from_iter([("id", id), ("city", city), ("ts", ts)]).unwrap()
//! # }
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let dir = tempfile::tempdir()?;
//! # let base_uri = dir.path().to_str().unwrap();
//! let mut table = Table::create(base_uri)
//! .with_table_name("trips")
//! .with_table_type(TableTypeValue::CopyOnWrite) // or MergeOnRead
//! .with_record_key_fields(["id"])
//! .with_partition_fields(["city"])
//! .with_ordering_fields(["ts"]) // the larger `ts` wins when keys collide
//! .create()
//! .await?;
//!
//! // `trips` builds a batch with columns `id`, `city`, and `ts`.
//! table.append([trips(&[("a", "sf", 1), ("b", "sf", 1), ("c", "nyc", 1)])]).await?;
//!
//! let result = table.upsert([trips(&[("b", "sf", 2), ("d", "nyc", 2)])]).await?;
//! assert_eq!((result.num_updates, result.num_inserts), (1, 1));
//!
//! let result = table.delete("id = 'a'").await?;
//! assert_eq!(result.num_deletes, 1);
//!
//! let result = table
//! .delete_keys([HoodieKey {
//! record_key: "c".to_string(),
//! partition_path: "city=nyc".to_string(),
//! }])
//! .await?;
//! assert_eq!(result.num_deletes, 1);
//! # Ok(())
//! # }
//! ```
//!
//! # Compact a merge-on-read table
//!
//! On a merge-on-read table, upserts, updates, and deletes write log files.
//! [`Table::compact`](table::Table::compact) (experimental) folds them into new base files under
//! one compaction commit. Rows keep their commit time, so incremental reads see no new rows from
//! it. Compaction can also run after every `hoodie.compact.inline.max.delta.commits` (default 5)
//! delta commits: in the write call with `hoodie.compact.inline=true`, or on a background task
//! with `hoodie.datasource.compaction.async.enable=true`, which
//! [`Table::await_compaction`](table::Table::await_compaction) waits for.
//!
//! ```
//! # use std::sync::Arc;
//! # use arrow::array::{ArrayRef, Int64Array, RecordBatch, StringArray};
//! use hudi::config::table::TableTypeValue;
//! use hudi::table::Table;
//!
//! # fn trips(rows: &[(&str, &str, i64)]) -> RecordBatch {
//! # let id: ArrayRef = Arc::new(StringArray::from_iter_values(rows.iter().map(|r| r.0)));
//! # let city: ArrayRef = Arc::new(StringArray::from_iter_values(rows.iter().map(|r| r.1)));
//! # let ts: ArrayRef = Arc::new(Int64Array::from_iter_values(rows.iter().map(|r| r.2)));
//! # RecordBatch::try_from_iter([("id", id), ("city", city), ("ts", ts)]).unwrap()
//! # }
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let dir = tempfile::tempdir()?;
//! # let base_uri = dir.path().to_str().unwrap();
//! let mut table = Table::create(base_uri)
//! .with_table_name("trips")
//! .with_table_type(TableTypeValue::MergeOnRead)
//! .with_record_key_fields(["id"])
//! .with_partition_fields(["city"])
//! .with_ordering_fields(["ts"])
//! .create()
//! .await?;
//! table.append([trips(&[("a", "sf", 1), ("b", "nyc", 1)])]).await?;
//! table.upsert([trips(&[("a", "sf", 2)])]).await?; // writes a log file in `city=sf`
//!
//! let result = table.compact().await?;
//! assert_eq!((result.num_file_groups, result.num_log_files), (1, 1));
//!
//! // Nothing left to compact: no commit is created.
//! assert!(table.compact().await?.requested_time.is_none());
//! # Ok(())
//! # }
//! ```
//!
//! # Snapshot read
//!
//! [`Table::new`](table::Table::new) opens an existing table. A read returns the latest committed
//! state, narrowed by optional filters and a projection from [`ReadOptions`](table::ReadOptions).
//!
//! ```
//! # use std::sync::Arc;
//! # use arrow::array::{ArrayRef, Int64Array, RecordBatch, StringArray};
//! use hudi::table::{ReadOptions, Table};
//! # fn trips(rows: &[(&str, &str, i64)]) -> RecordBatch {
//! # let id: ArrayRef = Arc::new(StringArray::from_iter_values(rows.iter().map(|r| r.0)));
//! # let city: ArrayRef = Arc::new(StringArray::from_iter_values(rows.iter().map(|r| r.1)));
//! # let ts: ArrayRef = Arc::new(Int64Array::from_iter_values(rows.iter().map(|r| r.2)));
//! # RecordBatch::try_from_iter([("id", id), ("city", city), ("ts", ts)]).unwrap()
//! # }
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let dir = tempfile::tempdir()?;
//! # let base_uri = dir.path().to_str().unwrap();
//! # let mut writer = Table::create(base_uri)
//! # .with_table_name("trips")
//! # .with_record_key_fields(["id"])
//! # .with_partition_fields(["city"])
//! # .create()
//! # .await?;
//! # writer.append([trips(&[("a", "sf", 1), ("b", "sf", 1), ("c", "nyc", 1)])]).await?;
//! let table = Table::new(base_uri).await?;
//! let options = ReadOptions::new()
//! .with_filters([("city", "=", "sf")])?
//! .with_projection(["id", "ts"]);
//! let batches = table.read(&options).await?;
//! let num_rows: usize = batches.iter().map(|b| b.num_rows()).sum();
//! assert_eq!(num_rows, 2);
//! # Ok(())
//! # }
//! ```
//!
//! [`Table::read_stream`](table::Table::read_stream) yields the same snapshot one batch at a time.
//!
//! # Time-travel read
//!
//! [`ReadOptions::with_as_of_timestamp`](table::ReadOptions::with_as_of_timestamp) reads the table
//! as of an earlier commit. It accepts a Hudi instant time (`yyyyMMddHHmmssSSS` or
//! `yyyyMMddHHmmss`), Unix epoch time, or RFC 3339 with a timezone offset.
//!
//! ```
//! # use std::sync::Arc;
//! # use arrow::array::{ArrayRef, Int64Array, RecordBatch, StringArray};
//! use hudi::table::{ReadOptions, Table};
//! # fn trips(rows: &[(&str, &str, i64)]) -> RecordBatch {
//! # let id: ArrayRef = Arc::new(StringArray::from_iter_values(rows.iter().map(|r| r.0)));
//! # let city: ArrayRef = Arc::new(StringArray::from_iter_values(rows.iter().map(|r| r.1)));
//! # let ts: ArrayRef = Arc::new(Int64Array::from_iter_values(rows.iter().map(|r| r.2)));
//! # RecordBatch::try_from_iter([("id", id), ("city", city), ("ts", ts)]).unwrap()
//! # }
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let dir = tempfile::tempdir()?;
//! # let base_uri = dir.path().to_str().unwrap();
//! # let mut table = Table::create(base_uri)
//! # .with_table_name("trips")
//! # .with_record_key_fields(["id"])
//! # .with_partition_fields(["city"])
//! # .create()
//! # .await?;
//! let first = table.append([trips(&[("a", "sf", 1), ("b", "sf", 1)])]).await?;
//! table.append([trips(&[("c", "nyc", 2)])]).await?;
//!
//! // A write's requested time names its commit for time travel.
//! let as_of = first.requested_time.expect("the append made a commit");
//! let options = ReadOptions::new().with_as_of_timestamp(as_of);
//! let batches = table.read(&options).await?;
//! let num_rows: usize = batches.iter().map(|b| b.num_rows()).sum();
//! assert_eq!(num_rows, 2);
//! # Ok(())
//! # }
//! ```
//!
//! # Incremental read
//!
//! An incremental query returns the records changed by commits in the half-open window
//! `(start, end]`; `end` defaults to everything committed so far. On tables at version 8 and
//! above (timeline layout 2) the window bounds commit completion times, so start from a write's
//! [`completion_time`](table::WriteResult::completion_time), not its `requested_time`.
//!
//! ```
//! # use std::sync::Arc;
//! # use arrow::array::{ArrayRef, Int64Array, RecordBatch, StringArray};
//! use hudi::table::{QueryType, ReadOptions, Table};
//! # fn trips(rows: &[(&str, &str, i64)]) -> RecordBatch {
//! # let id: ArrayRef = Arc::new(StringArray::from_iter_values(rows.iter().map(|r| r.0)));
//! # let city: ArrayRef = Arc::new(StringArray::from_iter_values(rows.iter().map(|r| r.1)));
//! # let ts: ArrayRef = Arc::new(Int64Array::from_iter_values(rows.iter().map(|r| r.2)));
//! # RecordBatch::try_from_iter([("id", id), ("city", city), ("ts", ts)]).unwrap()
//! # }
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let dir = tempfile::tempdir()?;
//! # let base_uri = dir.path().to_str().unwrap();
//! # let mut table = Table::create(base_uri)
//! # .with_table_name("trips")
//! # .with_record_key_fields(["id"])
//! # .with_partition_fields(["city"])
//! # .create()
//! # .await?;
//! let first = table.append([trips(&[("a", "sf", 1), ("b", "sf", 1)])]).await?;
//! table.upsert([trips(&[("b", "sf", 2), ("c", "nyc", 2)])]).await?;
//!
//! let start = first.completion_time.ok_or("layout 2 records a completion time")?;
//! let options = ReadOptions::new()
//! .with_query_type(QueryType::Incremental)
//! .with_start_timestamp(start);
//! let changed = table.read(&options).await?;
//! let num_rows: usize = changed.iter().map(|b| b.num_rows()).sum();
//! assert_eq!(num_rows, 2); // `b` updated, `c` inserted
//! # Ok(())
//! # }
//! ```
//!
//! # Query with DataFusion
//!
//! With the `datafusion` feature, register a table with a DataFusion `SessionContext` and query
//! it with SQL.
//!
//! ```
//! # use std::sync::Arc;
//! # use arrow::array::{ArrayRef, Int64Array, RecordBatch, StringArray};
//! # fn trips(rows: &[(&str, &str, i64)]) -> RecordBatch {
//! # let id: ArrayRef = Arc::new(StringArray::from_iter_values(rows.iter().map(|r| r.0)));
//! # let city: ArrayRef = Arc::new(StringArray::from_iter_values(rows.iter().map(|r| r.1)));
//! # let ts: ArrayRef = Arc::new(Int64Array::from_iter_values(rows.iter().map(|r| r.2)));
//! # RecordBatch::try_from_iter([("id", id), ("city", city), ("ts", ts)]).unwrap()
//! # }
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let dir = tempfile::tempdir()?;
//! # let base_uri = dir.path().to_str().unwrap();
//! # let mut table = hudi::table::Table::create(base_uri)
//! # .with_table_name("trips")
//! # .with_record_key_fields(["id"])
//! # .with_partition_fields(["city"])
//! # .create()
//! # .await?;
//! # table.append([trips(&[("a", "sf", 1), ("b", "sf", 1), ("c", "nyc", 1)])]).await?;
//! # #[cfg(feature = "datafusion")]
//! # {
//! use datafusion::prelude::SessionContext;
//! use hudi::HudiDataSource;
//!
//! let ctx = SessionContext::new();
//! let provider =
//! HudiDataSource::new_with_options(base_uri, [("hoodie.read.input.partitions", "2")]).await?;
//! ctx.register_table("trips", Arc::new(provider))?;
//! let df = ctx.sql("SELECT id, ts FROM trips WHERE city = 'sf'").await?;
//! let batches = df.collect().await?;
//! let num_rows: usize = batches.iter().map(|b| b.num_rows()).sum();
//! assert_eq!(num_rows, 2);
//! # }
//! # Ok(())
//! # }
//! ```
pub use *;
pub use *;