some code 2 rust
extern crate tokio; pub mod datatable; pub mod handle_error; pub mod common; use chrono::prelude::*; use chrono::offset::LocalResult; use datafusion::arrow::array::{ArrayData, ArrayRef, BooleanArray, BooleanBuilder, Int32Array, Date64Array, PrimitiveArray, Time32SecondBuilder, PrimitiveBuilder}; use datafusion::arrow::datatypes::{DataType, Field, Schema, Date64Type, Time32SecondType, TimeUnit}; use datafusion::arrow::record_batch::RecordBatch; use datafusion::datasource::MemTable; use datafusion::error::Result; use datafusion::from_slice::FromSlice; use datafusion::prelude::*; use std::sync::Arc; use datafusion::arrow::{ array::{ Array, // ArrayData Date32Builder, Date64Builder, Float32Builder, TimestampSecondBuilder, Float64Builder, Int64Builder, StringArray, StringBuilder, StringDictionaryBuilder, StructArray, }, datatypes::Int8Type,array::TimestampSecondArray }; use crate::datatable::DataTable; // path:&str, head:u32, sheetname: &str #[tokio::main] async fn main() { // DataTable::read_excel("abc.xlsx", 1, "Sheet1"); // =================================================================================================================== // 字符串 let mut sbuilder = StringBuilder::new(100); sbuilder.append_value("a").unwrap(); sbuilder.append_null().unwrap(); sbuilder.append_value("多编程语").unwrap(); sbuilder.append_value("add").unwrap(); sbuilder.append_value("aaaaaa").unwrap(); let astr = sbuilder.finish(); // i32 let mut builder = Int32Array::builder(5); builder.append_value(5).unwrap(); builder.append_null().unwrap(); builder.append_slice(&[1, 2, 3]).unwrap(); let ai32 = builder.finish(); // bool let mut c = BooleanBuilder::new(5); c.append_value(true).unwrap(); c.append_value(true).unwrap(); c.append_null().unwrap(); c.append_value(false).unwrap(); c.append_value(true).unwrap(); let abool = c.finish(); // f64 // let mut f64a = Float64Array::builder(5); let mut f64a = Float64Builder::new(5); f64a.append_value(5.9).unwrap(); f64a.append_value(5.1).unwrap(); f64a.append_null().unwrap(); f64a.append_value(5.2).unwrap(); f64a.append_value(5.6).unwrap(); let af64 = f64a.finish(); // i64 let mut i64a = Int64Builder::new(5); i64a.append_value(4).unwrap(); i64a.append_value(4).unwrap(); i64a.append_null().unwrap(); i64a.append_value(4).unwrap(); i64a.append_value(4).unwrap(); let ai64 = i64a.finish(); // date32 let mut d32a = Date32Builder::new(5); d32a.append_value(45001).unwrap(); d32a.append_value(45001).unwrap(); d32a.append_null().unwrap(); d32a.append_value(45001).unwrap(); d32a.append_value(45001).unwrap(); let ad32 = d32a.finish(); // date64 let mut d64a = Date64Builder::new(5); d64a.append_value(46502).unwrap(); d64a.append_value(46502).unwrap(); d64a.append_null().unwrap(); d64a.append_value(46502).unwrap(); d64a.append_value(46502).unwrap(); let ad64 = d64a.finish(); let mut f32l = Float32Builder::new(100); f32l.append_value(1.1).unwrap(); f32l.append_null(); f32l.append_value(1.2).unwrap(); f32l.append_value(1.3).unwrap(); f32l.append_value(1.4).unwrap(); let af32 = f32l.finish(); let mut t32t = Time32SecondBuilder::new(10); t32t.append_value(43953).unwrap(); t32t.append_value(11546).unwrap(); t32t.append_value(11246).unwrap(); t32t.append_value(10436).unwrap(); t32t.append_value(10436).unwrap(); let at32 = t32t.finish(); let mut tmsarr2 = TimestampSecondArray::builder(100); tmsarr2.append_value(3850459893).unwrap(); tmsarr2.append_value(3850459894).unwrap(); tmsarr2.append_value(3850459895).unwrap(); tmsarr2.append_value(1641211893).unwrap(); tmsarr2.append_value(1641211893).unwrap(); let atmstmp2 = tmsarr2.finish(); let schema = Arc::new(Schema::new(vec![ Field::new("astr", DataType::Utf8, false), Field::new("ai32", DataType::Int32, false), Field::new("abool", DataType::Boolean, false), Field::new("af64", DataType::Float64, false), Field::new("ai64", DataType::Int64, false), Field::new("ad32", DataType::Date32, false), Field::new("ad64", DataType::Date64, false), Field::new("af32", DataType::Float32, false), Field::new("at32", DataType::Time32(TimeUnit::Second), false), Field::new("atmstmp", DataType::Timestamp(TimeUnit::Second, None), false), ])); let batch = RecordBatch::try_new( schema.clone(), vec![ Arc::new(astr), Arc::new(ai32), Arc::new(abool), Arc::new(af64), Arc::new(ai64), Arc::new(ad32), Arc::new(ad64), Arc::new(af32), Arc::new(at32), Arc::new(atmstmp2) ], ) .unwrap(); for each in batch.columns() { println!("====================================="); println!("{:?}", each); } // println!("{:?}", at32); // let e = at32.as_any().downcast_ref::<PrimitiveArray<Date32Type>>(); // let f: &Date64Array = e.unwrap(); // println!("{:?}", f); // let xxxx = f.value(0); // print!("{:?}", xxxx); // match d{ // DataType::Utf8 => print!("utf8"), // _ => print!("unknow"), // } // ================================================================================================================== // println!("===================="); // let ctx = SessionContext::new(); // let provider = MemTable::try_new(schema, vec![vec![batch]]).unwrap(); // ctx.register_table("t", Arc::new(provider)).unwrap(); // let df:Arc<DataFrame> = ctx.table("t").unwrap(); // df.show().await().unwrap(); // ================================================================================================================== // 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 mut sd = Float32Builder::new(5); // sd.append_value(DataType::Decimal(4, 2)); // 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(); // let c = batch.column(0); // let d = c.data_type(); // println!("{:?}", d); // let e = c.as_any().downcast_ref::<StringArray>(); // let f: &StringArray = e.unwrap(); // println!("{:?}", f); // let xxxx = f.value(0); // print!("{:?}", xxxx); // match d{ // DataType::Utf8 => print!("utf8"), // _ => print!("unknow"), // } }