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 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79bb05c
commit e2720e4
Showing
8 changed files
with
136 additions
and
39 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,48 @@ | ||
use std::fs::File; | ||
use std::io::BufReader; | ||
use std::sync::Arc; | ||
|
||
use arrow2::array::Array; | ||
use arrow2::chunk::Chunk; | ||
use arrow2::error::Result; | ||
use arrow2::io::json::read; | ||
|
||
fn read_path(path: &str, projection: Option<Vec<&str>>) -> Result<Chunk<Arc<dyn Array>>> { | ||
// Example of reading a NDJSON 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 `Chunk`. 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 |
---|---|---|
@@ -1,10 +1,16 @@ | ||
# JSON read | ||
|
||
When compiled with feature `io_json`, you can use this crate to read JSON files. | ||
When compiled with feature `io_json`, you can use this crate to read NDJSON files: | ||
|
||
```rust | ||
{{#include ../../../examples/json_read.rs}} | ||
{{#include ../../../examples/ndjson_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 crate also supports reading JSON, at the expense of being unable to read the file in chunks. | ||
|
||
```rust | ||
{{#include ../../../examples/json_read.rs}} | ||
``` |
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