Serialize `batch` to JSON output
(&mut self, batch: &RecordBatch)
| 375 | |
| 376 | /// Serialize `batch` to JSON output |
| 377 | pub fn write(&mut self, batch: &RecordBatch) -> Result<(), ArrowError> { |
| 378 | if batch.num_rows() == 0 { |
| 379 | return Ok(()); |
| 380 | } |
| 381 | |
| 382 | // BufWriter uses a buffer size of 8KB |
| 383 | // We therefore double this and flush once we have more than 8KB |
| 384 | let mut buffer = Vec::with_capacity(16 * 1024); |
| 385 | |
| 386 | let mut is_first_row = !self.started; |
| 387 | if !self.started { |
| 388 | self.format.start_stream(&mut buffer)?; |
| 389 | self.started = true; |
| 390 | } |
| 391 | |
| 392 | let array = StructArray::from(batch.clone()); |
| 393 | let field = Arc::new(Field::new_struct( |
| 394 | "", |
| 395 | batch.schema().fields().clone(), |
| 396 | false, |
| 397 | )); |
| 398 | |
| 399 | let mut encoder = make_encoder(&field, &array, &self.options)?; |
| 400 | |
| 401 | // Validate that the root is not nullable |
| 402 | assert!(!encoder.has_nulls(), "root cannot be nullable"); |
| 403 | for idx in 0..batch.num_rows() { |
| 404 | self.format.start_row(&mut buffer, is_first_row)?; |
| 405 | is_first_row = false; |
| 406 | |
| 407 | encoder.encode(idx, &mut buffer); |
| 408 | if buffer.len() > 8 * 1024 { |
| 409 | self.writer.write_all(&buffer)?; |
| 410 | buffer.clear(); |
| 411 | } |
| 412 | self.format.end_row(&mut buffer)?; |
| 413 | } |
| 414 | |
| 415 | if !buffer.is_empty() { |
| 416 | self.writer.write_all(&buffer)?; |
| 417 | } |
| 418 | |
| 419 | Ok(()) |
| 420 | } |
| 421 | |
| 422 | /// Serialize `batches` to JSON output |
| 423 | pub fn write_batches(&mut self, batches: &[&RecordBatch]) -> Result<(), ArrowError> { |