MCPcopy Create free account
hub / github.com/apache/datafusion / write_out

Function write_out

datafusion-examples/examples/dataframe/dataframe.rs:203–270  ·  view source on GitHub ↗

Use the DataFrame API to: 1. Write out a DataFrame to a table 2. Write out a DataFrame to a parquet file 3. Write out a DataFrame to a csv file 4. Write out a DataFrame to a json file

(ctx: &SessionContext)

Source from the content-addressed store, hash-verified

201/// 3. Write out a DataFrame to a csv file
202/// 4. Write out a DataFrame to a json file
203async fn write_out(ctx: &SessionContext) -> Result<()> {
204 let array = StringViewArray::from(vec!["a", "b", "c"]);
205 let schema = Arc::new(Schema::new(vec![Field::new(
206 "tablecol1",
207 DataType::Utf8View,
208 false,
209 )]));
210 let batch = RecordBatch::try_new(schema.clone(), vec![Arc::new(array)])?;
211 let mem_table = MemTable::try_new(schema.clone(), vec![vec![batch]])?;
212 ctx.register_table("initial_data", Arc::new(mem_table))?;
213 let df = ctx.table("initial_data").await?;
214
215 // Create a single temp root with subdirectories
216 let tmp_root = TempDir::new()?;
217 let examples_root = tmp_root.path().join("datafusion-examples");
218 create_dir_all(&examples_root).await?;
219 let table_dir = examples_root.join("test_table");
220 let parquet_dir = examples_root.join("test_parquet");
221 let csv_dir = examples_root.join("test_csv");
222 let json_dir = examples_root.join("test_json");
223 create_dir_all(&table_dir).await?;
224 create_dir_all(&parquet_dir).await?;
225 create_dir_all(&csv_dir).await?;
226 create_dir_all(&json_dir).await?;
227
228 let create_sql = format!(
229 "CREATE EXTERNAL TABLE test(tablecol1 varchar)
230 STORED AS parquet
231 LOCATION '{}'",
232 table_dir.display()
233 );
234 ctx.sql(&create_sql).await?.collect().await?;
235
236 // This is equivalent to INSERT INTO test VALUES ('a'), ('b'), ('c').
237 // The behavior of write_table depends on the TableProvider's implementation
238 // of the insert_into method.
239 df.clone()
240 .write_table("test", DataFrameWriteOptions::new())
241 .await?;
242
243 df.clone()
244 .write_parquet(
245 parquet_dir.to_str().unwrap(),
246 DataFrameWriteOptions::new(),
247 None,
248 )
249 .await?;
250
251 df.clone()
252 .write_csv(
253 csv_dir.to_str().unwrap(),
254 // DataFrameWriteOptions contains options which control how data is written
255 // such as compression codec
256 DataFrameWriteOptions::new(),
257 Some(CsvOptions::default().with_compression(CompressionTypeVariant::GZIP)),
258 )
259 .await?;
260

Callers 1

dataframe_exampleFunction · 0.85

Calls 13

newFunction · 0.85
collectMethod · 0.80
sqlMethod · 0.80
write_tableMethod · 0.80
with_compressionMethod · 0.80
cloneMethod · 0.45
register_tableMethod · 0.45
tableMethod · 0.45
joinMethod · 0.45
pathMethod · 0.45
write_parquetMethod · 0.45
write_csvMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…