Execute the `DataFrame` and write the results to CSV file(s). # Example ``` # use datafusion::prelude::*; # use datafusion::error::Result; # use std::fs; # #[tokio::main] # async fn main() -> Result<()> { use datafusion::dataframe::DataFrameWriteOptions; let ctx = SessionContext::new(); // Sort the data by column "b" and write it to a new location ctx.read_csv("tests/data/example.csv", CsvReadOpt
(
self,
path: &str,
options: DataFrameWriteOptions,
writer_options: Option<CsvOptions>,
)
| 2043 | /// # } |
| 2044 | /// ``` |
| 2045 | pub async fn write_csv( |
| 2046 | self, |
| 2047 | path: &str, |
| 2048 | options: DataFrameWriteOptions, |
| 2049 | writer_options: Option<CsvOptions>, |
| 2050 | ) -> Result<Vec<RecordBatch>, DataFusionError> { |
| 2051 | if options.insert_op != InsertOp::Append { |
| 2052 | return not_impl_err!( |
| 2053 | "{} is not implemented for DataFrame::write_csv.", |
| 2054 | options.insert_op |
| 2055 | ); |
| 2056 | } |
| 2057 | |
| 2058 | let format = if let Some(csv_opts) = writer_options { |
| 2059 | Arc::new(CsvFormatFactory::new_with_options(csv_opts)) |
| 2060 | } else { |
| 2061 | Arc::new(CsvFormatFactory::new()) |
| 2062 | }; |
| 2063 | |
| 2064 | let file_type = format_as_file_type(format); |
| 2065 | |
| 2066 | let copy_options = options.build_sink_options(); |
| 2067 | |
| 2068 | let plan = if options.sort_by.is_empty() { |
| 2069 | self.plan |
| 2070 | } else { |
| 2071 | LogicalPlanBuilder::from(self.plan) |
| 2072 | .sort(options.sort_by)? |
| 2073 | .build()? |
| 2074 | }; |
| 2075 | |
| 2076 | let plan = LogicalPlanBuilder::copy_to( |
| 2077 | plan, |
| 2078 | path.into(), |
| 2079 | file_type, |
| 2080 | copy_options, |
| 2081 | options.partition_by, |
| 2082 | )? |
| 2083 | .build()?; |
| 2084 | |
| 2085 | DataFrame { |
| 2086 | session_state: self.session_state, |
| 2087 | plan, |
| 2088 | projection_requires_validation: self.projection_requires_validation, |
| 2089 | } |
| 2090 | .collect() |
| 2091 | .await |
| 2092 | } |
| 2093 | |
| 2094 | /// Execute the `DataFrame` and write the results to JSON file(s). |
| 2095 | /// |