This is an automated email from the ASF dual-hosted git repository.

tustvold pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/master by this push:
     new ed58e767d7 chore: add csv example (#4904)
ed58e767d7 is described below

commit ed58e767d7607e954085842a57a680b6807794e0
Author: fan <[email protected]>
AuthorDate: Mon Oct 9 23:03:10 2023 +0800

    chore: add csv example (#4904)
    
    * chore: add csv example
    
    Signed-off-by: fan <[email protected]>
    
    * follow reviews
    
    Signed-off-by: fan <[email protected]>
    
    ---------
    
    Signed-off-by: fan <[email protected]>
---
 arrow-csv/examples/README.md          | 21 +++++++++++++
 arrow-csv/examples/csv_calculation.rs | 56 +++++++++++++++++++++++++++++++++++
 arrow-csv/test/data/example.csv       |  4 +++
 3 files changed, 81 insertions(+)

diff --git a/arrow-csv/examples/README.md b/arrow-csv/examples/README.md
new file mode 100644
index 0000000000..340413e76d
--- /dev/null
+++ b/arrow-csv/examples/README.md
@@ -0,0 +1,21 @@
+<!---
+  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.
+-->
+
+# Examples
+- [`csv_calculation.rs`](csv_calculation.rs): performs a simple calculation 
using the CSV reader
\ No newline at end of file
diff --git a/arrow-csv/examples/csv_calculation.rs 
b/arrow-csv/examples/csv_calculation.rs
new file mode 100644
index 0000000000..12aaadde44
--- /dev/null
+++ b/arrow-csv/examples/csv_calculation.rs
@@ -0,0 +1,56 @@
+// 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.
+
+use arrow_array::cast::AsArray;
+use arrow_array::types::Int16Type;
+use arrow_csv::ReaderBuilder;
+
+use arrow_schema::{DataType, Field, Schema};
+use std::fs::File;
+use std::sync::Arc;
+
+fn main() {
+    // read csv from file
+    let file = File::open("arrow-csv/test/data/example.csv").unwrap();
+    let csv_schema = Schema::new(vec![
+        Field::new("c1", DataType::Int16, true),
+        Field::new("c2", DataType::Float32, true),
+        Field::new("c3", DataType::Utf8, true),
+        Field::new("c4", DataType::Boolean, true),
+    ]);
+    let mut reader = ReaderBuilder::new(Arc::new(csv_schema))
+        .has_header(true)
+        .build(file)
+        .unwrap();
+
+    match reader.next() {
+        Some(r) => match r {
+            Ok(r) => {
+                // get the column(0) max value
+                let col = r.column(0).as_primitive::<Int16Type>();
+                let max = col.iter().max().flatten();
+                println!("max value column(0): {max:?}")
+            }
+            Err(e) => {
+                println!("{e:?}");
+            }
+        },
+        None => {
+            println!("csv is empty");
+        }
+    }
+}
diff --git a/arrow-csv/test/data/example.csv b/arrow-csv/test/data/example.csv
new file mode 100644
index 0000000000..0c03cee845
--- /dev/null
+++ b/arrow-csv/test/data/example.csv
@@ -0,0 +1,4 @@
+c1,c2,c3,c4
+1,1.1,"hong kong",true
+3,323.12,"XiAn",false
+10,131323.12,"cheng du",false
\ No newline at end of file

Reply via email to