Skip to content

Commit

Permalink
fix cli json print and avro example
Browse files Browse the repository at this point in the history
  • Loading branch information
houqp committed Jan 20, 2022
1 parent 1e352c3 commit 2698383
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
25 changes: 11 additions & 14 deletions datafusion-cli/src/print_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,21 @@ impl fmt::Display for PrintFormat {
}

fn print_batches_to_json<J: JsonFormat>(batches: &[RecordBatch]) -> Result<String> {
use arrow::io::json::write as json_write;

if batches.is_empty() {
return Ok("{}".to_string());
}
let mut bytes = vec![];
let schema = batches[0].schema();
let names = schema
.fields
.iter()
.map(|f| f.name.clone())
.collect::<Vec<String>>();
for batch in batches {
arrow::io::json::write::serialize(
&names,
batch.columns(),
J::default(),
&mut bytes,
);
}

let format = J::default();
let blocks = json_write::Serializer::new(
batches.iter().map(|r| Ok(r.clone())),
vec![],
format,
);
json_write::write(&mut bytes, format, blocks)?;

let formatted = String::from_utf8(bytes)
.map_err(|e| DataFusionError::Execution(e.to_string()))?;
Ok(formatted)
Expand Down
4 changes: 2 additions & 2 deletions datafusion-examples/examples/avro_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use datafusion::arrow::io::print;
use datafusion::arrow_print;

use datafusion::error::Result;
use datafusion::prelude::*;
Expand Down Expand Up @@ -45,7 +45,7 @@ async fn main() -> Result<()> {
let results = df.collect().await?;

// print the results
print::print(&results);
println!("{}", arrow_print::write(&results));

Ok(())
}

0 comments on commit 2698383

Please sign in to comment.