Execute the `DataFrame` and write the results to JSON 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", CsvReadOp
(
self,
path: &str,
options: DataFrameWriteOptions,
writer_options: Option<JsonOptions>,
)
| 2113 | /// # } |
| 2114 | /// ``` |
| 2115 | pub async fn write_json( |
| 2116 | self, |
| 2117 | path: &str, |
| 2118 | options: DataFrameWriteOptions, |
| 2119 | writer_options: Option<JsonOptions>, |
| 2120 | ) -> Result<Vec<RecordBatch>, DataFusionError> { |
| 2121 | if options.insert_op != InsertOp::Append { |
| 2122 | return not_impl_err!( |
| 2123 | "{} is not implemented for DataFrame::write_json.", |
| 2124 | options.insert_op |
| 2125 | ); |
| 2126 | } |
| 2127 | |
| 2128 | let format = if let Some(json_opts) = writer_options { |
| 2129 | Arc::new(JsonFormatFactory::new_with_options(json_opts)) |
| 2130 | } else { |
| 2131 | Arc::new(JsonFormatFactory::new()) |
| 2132 | }; |
| 2133 | |
| 2134 | let file_type = format_as_file_type(format); |
| 2135 | |
| 2136 | let copy_options = options.build_sink_options(); |
| 2137 | |
| 2138 | let plan = if options.sort_by.is_empty() { |
| 2139 | self.plan |
| 2140 | } else { |
| 2141 | LogicalPlanBuilder::from(self.plan) |
| 2142 | .sort(options.sort_by)? |
| 2143 | .build()? |
| 2144 | }; |
| 2145 | |
| 2146 | let plan = LogicalPlanBuilder::copy_to( |
| 2147 | plan, |
| 2148 | path.into(), |
| 2149 | file_type, |
| 2150 | copy_options, |
| 2151 | options.partition_by, |
| 2152 | )? |
| 2153 | .build()?; |
| 2154 | |
| 2155 | DataFrame { |
| 2156 | session_state: self.session_state, |
| 2157 | plan, |
| 2158 | projection_requires_validation: self.projection_requires_validation, |
| 2159 | } |
| 2160 | .collect() |
| 2161 | .await |
| 2162 | } |
| 2163 | |
| 2164 | /// Add or replace a column in the DataFrame. |
| 2165 | /// |