Expand description
JSON Writer
This JSON writer converts Arrow RecordBatch
es into arrays of
JSON objects or JSON formatted byte streams.
Writing JSON Objects
To serialize RecordBatch
es into array of
JSON objects, use
record_batches_to_json_rows
:
use std::sync::Arc;
use arrow::array::Int32Array;
use arrow::datatypes::{DataType, Field, Schema};
use arrow::json;
use arrow::record_batch::RecordBatch;
let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);
let a = Int32Array::from(vec![1, 2, 3]);
let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a)]).unwrap();
let json_rows = json::writer::record_batches_to_json_rows(&[batch]).unwrap();
assert_eq!(
serde_json::Value::Object(json_rows[1].clone()),
serde_json::json!({"a": 2}),
);
Writing JSON formatted byte streams
To serialize RecordBatch
es into line-delimited JSON bytes, use
LineDelimitedWriter
:
use std::sync::Arc;
use arrow::array::Int32Array;
use arrow::datatypes::{DataType, Field, Schema};
use arrow::json;
use arrow::record_batch::RecordBatch;
let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);
let a = Int32Array::from(vec![1, 2, 3]);
let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a)]).unwrap();
// Write the record batch out as JSON
let buf = Vec::new();
let mut writer = json::LineDelimitedWriter::new(buf);
writer.write_batches(&vec![batch]).unwrap();
writer.finish().unwrap();
// Get the underlying buffer back,
let buf = writer.into_inner();
assert_eq!(r#"{"a":1}
{"a":2}
{"a":3}
"#, String::from_utf8(buf).unwrap())
To serialize RecordBatch
es into a well formed JSON array, use
ArrayWriter
:
use std::sync::Arc;
use arrow::array::Int32Array;
use arrow::datatypes::{DataType, Field, Schema};
use arrow::json;
use arrow::record_batch::RecordBatch;
let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);
let a = Int32Array::from(vec![1, 2, 3]);
let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a)]).unwrap();
// Write the record batch out as a JSON array
let buf = Vec::new();
let mut writer = json::ArrayWriter::new(buf);
writer.write_batches(&vec![batch]).unwrap();
writer.finish().unwrap();
// Get the underlying buffer back,
let buf = writer.into_inner();
assert_eq!(r#"[{"a":1},{"a":2},{"a":3}]"#, String::from_utf8(buf).unwrap())
Structs
Produces JSON output as a single JSON array. For example
Produces JSON output with one record per line. For example
A JSON writer which serializes RecordBatch
es to a stream of
u8
encoded JSON objects. See the module level documentation for
detailed usage and examples. The specific format of the stream is
controlled by the JsonFormat
type parameter.
Traits
This trait defines how to format a sequence of JSON objects to a byte stream.
Functions
Converts an arrow ArrayRef
into a Vec
of Serde JSON serde_json::Value
’s
Converts an arrow RecordBatch
into a Vec
of Serde JSON
JsonMap
s (objects)
Type Definitions
A JSON writer which serializes RecordBatch
es to JSON arrays
A JSON writer which serializes RecordBatch
es to newline delimited JSON objects