Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

what's the best practice to get a single value from arrow array? #3497

Closed
heyGrant opened this issue Sep 15, 2022 · 0 comments
Closed

what's the best practice to get a single value from arrow array? #3497

heyGrant opened this issue Sep 15, 2022 · 0 comments

Comments

@heyGrant
Copy link

heyGrant commented Sep 15, 2022

code first as below:
(you can skip and see the bottom codes about how to get a single value, it's my way, but I don't like it.)

use std::sync::Arc;
use datafusion::arrow::datatypes::{
  DataType,
  Field,
  Schema};
use datafusion::arrow::record_batch::RecordBatch;
use datafusion::arrow::array::StringBuilder;
use datafusion::arrow::array::StringArray;
 
#[tokio::main]
async fn main(){
  // create an arrow array first:
    let mut sbuilder = StringBuilder::new(100);
    sbuilder.append_value("a").unwrap();
    sbuilder.append_null().unwrap();
    sbuilder.append_value("hello").unwrap();
    sbuilder.append_value("add").unwrap();
    sbuilder.append_value("aaaaaa").unwrap();
    let a = sbuilder.finish();
 
    let schema = Arc::new(Schema::new(vec![
        Field::new("a", DataType::Utf8, false),
    ]));
    let batch = RecordBatch::try_new(
        schema.clone(),
        vec![
            Arc::new(a),
        ],
    ).unwrap();
 
    // print the first column:
    let firstcol = batch.column(0);
    println!("{:?}", firstcol);

//below, get single value, I don't like this , is there a simple way to do that?
    let da = firstcol.as_any();
    let da1 = da.downcast_ref::<StringArray>();
    let singlevalue3 = da1.unwrap();
    let s = singlevalue3.value(3);
    println!("{:?}",s);
}

so many steps to get a single value from this array. is there a simple way to do that? who can help me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant