This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split IO-bounded from CPU-bounded tasks (#706)
- Loading branch information
1 parent
50d1dc5
commit ce865fb
Showing
12 changed files
with
453 additions
and
796 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::fs::File; | ||
use std::io::BufReader; | ||
|
||
use arrow2::error::Result; | ||
use arrow2::io::json::read; | ||
use arrow2::record_batch::RecordBatch; | ||
|
||
fn read_path(path: &str, projection: Option<Vec<&str>>) -> Result<RecordBatch> { | ||
// Example of reading a JSON file. | ||
let mut reader = BufReader::new(File::open(path)?); | ||
|
||
let fields = read::infer_and_reset(&mut reader, None)?; | ||
|
||
let fields = if let Some(projection) = projection { | ||
fields | ||
.into_iter() | ||
.filter(|field| projection.contains(&field.name().as_ref())) | ||
.collect() | ||
} else { | ||
fields | ||
}; | ||
|
||
// at most 1024 rows. This container can be re-used across batches. | ||
let mut rows = vec![String::default(); 1024]; | ||
|
||
// Reads up to 1024 rows. | ||
// this is IO-intensive and performs minimal CPU work. In particular, | ||
// no deserialization is performed. | ||
let read = read::read_rows(&mut reader, &mut rows)?; | ||
let rows = &rows[..read]; | ||
|
||
// deserialize `rows` into a `RecordBatch`. This is CPU-intensive, has no IO, | ||
// and can be performed on a different thread pool via a channel. | ||
read::deserialize(&rows, fields) | ||
} | ||
|
||
fn main() -> Result<()> { | ||
use std::env; | ||
let args: Vec<String> = env::args().collect(); | ||
|
||
let file_path = &args[1]; | ||
|
||
let batch = read_path(file_path, None)?; | ||
println!("{:#?}", batch); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# JSON read | ||
|
||
When compiled with feature `io_json`, you can use this crate to read JSON files. | ||
|
||
```rust | ||
{{#include ../../../examples/json_read.rs}} | ||
``` | ||
|
||
Note how deserialization can be performed on a separate thread pool to avoid | ||
blocking the runtime (see also [here](https://ryhl.io/blog/async-what-is-blocking/)). |
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
Oops, something went wrong.