You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/01/26 06:51:53 UTC

[GitHub] [arrow] houqp commented on a change in pull request #9256: WIP: ARROW-11310: [Rust] implement JSON writer

houqp commented on a change in pull request #9256:
URL: https://github.com/apache/arrow/pull/9256#discussion_r564283209



##########
File path: rust/arrow/src/json/writer.rs
##########
@@ -0,0 +1,301 @@
+// 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.
+
+//! JSON Writer
+//!
+//! This JSON writer allows converting Arrow record batches into array of JSON objects. It also
+//! provides a Writer struct to help serialize record batches directly into line-delimited JSON
+//! objects as bytes.
+//!
+//! Serialize record batches into array of JSON objects:
+//!
+//! ```
+//! 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]);
+//! assert_eq!(
+//!     serde_json::Value::Object(json_rows[1].clone()),
+//!     serde_json::json!({"a": 2}),
+//! );
+//! ```
+//!
+//! Serialize record batches into line-delimited JSON bytes:
+//!
+//! ```
+//! 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 buf = Vec::new();
+//! let mut writer = json::Writer::new(buf);
+//! writer.write_batches(&vec![batch]).unwrap();
+//! ```
+
+use std::io::{BufWriter, Write};
+use std::iter;
+
+use serde_json::map::Map as JsonMap;
+use serde_json::Value;
+
+use crate::array::*;
+use crate::datatypes::*;
+use crate::error::Result;
+use crate::record_batch::RecordBatch;
+
+fn set_column_by_primitive_type<T: ArrowPrimitiveType>(
+    rows: &mut [JsonMap<String, Value>],
+    row_count: usize,
+    array: &ArrayRef,
+    col_name: &str,
+) {
+    let primitive_arr = as_primitive_array::<T>(array);
+    for (i, row) in rows.iter_mut().enumerate().take(row_count) {
+        row.insert(
+            col_name.to_string(),
+            primitive_arr
+                .value(i)

Review comment:
       Good catch, will update the code and add test cases.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org