Skip to main content

map2batch

Function map2batch 

Source
pub fn map2batch<T>(
    m: BTreeMap<T::Native, u64>,
    sch: &SchemaRef,
) -> Result<RecordBatch, Error>
Expand description

Converts a BTreeMap of frequencies into an Arrow RecordBatch.

§Arguments

  • m: A BTreeMap where keys are the values and values are their frequencies.
  • sch: The SchemaRef for the output RecordBatch.

§Returns

A Result containing a RecordBatch with two columns, whose names are defined by the provided SchemaRef, or an io::Error if an error occurs.

§Example

use std::collections::BTreeMap;
use std::sync::Arc;
use arrow::datatypes::{DataType, Field, Schema};
use rs_arrow_array2freq2rbat::map2batch;

let mut freqs = BTreeMap::new();
freqs.insert(1, 1);
freqs.insert(2, 2);
freqs.insert(3, 3);

let schema = Arc::new(Schema::new(vec![
    Field::new("value", DataType::Int32, false),
    Field::new("frequency", DataType::UInt64, false),
]));

let batch = map2batch::<arrow::datatypes::Int32Type>(freqs, &schema).unwrap();

assert_eq!(batch.num_columns(), 2);
assert_eq!(batch.num_rows(), 3);