alamb commented on code in PR #4360:
URL: https://github.com/apache/arrow-datafusion/pull/4360#discussion_r1032785312


##########
datafusion-examples/examples/dataframe.rs:
##########
@@ -41,3 +44,47 @@ async fn main() -> Result<()> {
 
     Ok(())
 }
+
+// Example to read data from a csv file with inferred schema
+async fn example_read_csv_file_with_inferred_schema() -> Arc<DataFrame> {
+    let path = "example.csv";
+    // Create the data to put into the csv file with headers
+    let content = r#"id,time,vote,unixtime,rating
+    a1,\"10 6, 2013\",3,1381017600,5.0
+    a2,\"08 9, 2013\",2,1376006400,4.5"#;
+    // write the data
+    fs::write(path, content).expect("Problem with writing file!");
+    // Create a session context
+    let ctx = SessionContext::new();
+    // Register a lazy DataFrame using the context
+    let df = ctx.read_csv(path, CsvReadOptions::default()).await.unwrap();
+    df
+}
+
+// Example to read csv file with a given csv file
+async fn example_read_csv_file_with_schema() -> Arc<DataFrame> {
+    let path = "example.csv";
+    // Create the data to put into the csv file with headers
+    let content = r#"id,time,vote,unixtime,rating
+    a1,\"10 6, 2013\",3,1381017600,5.0
+    a2,\"08 9, 2013\",2,1376006400,4.5"#;
+    // write the data
+    fs::write(path, content).expect("Problem with writing file!");
+    // Create a session context
+    let ctx = SessionContext::new();
+    // Define the schema
+    let schema = Schema::new(vec![
+        Field::new("id", DataType::Utf8, false),
+        Field::new("time", DataType::Utf8, false),
+        Field::new("vote", DataType::Int32, true),
+        Field::new("unixtime", DataType::Int64, false),
+        Field::new("rating", DataType::Float32, true),
+    ]);
+    // Create a csv option provider
+    let mut csv_read_option = CsvReadOptions::default();
+    // Update the option provider with the defined schema
+    csv_read_option.schema = Some(&schema);

Review Comment:
   If you wanted to use a slightly more idiomatic rust syntax (and avoid 
`mut`), you could do:
   
   ```suggestion
       // Create a csv option provider with the desired schema 
       let csv_read_option = CsvReadOptions {
         // Update the option provider with the defined schema
         schema: Some(&schema),
         ..default::Default()
       };
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to