Skip to content

Commit fd121d3

Browse files
Add examples of DataFrame::write* methods without S3 dependency (#8606)
1 parent df806bd commit fd121d3

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

datafusion-examples/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ cargo run --example csv_sql
4747
- [`catalog.rs`](examples/external_dependency/catalog.rs): Register the table into a custom catalog
4848
- [`custom_datasource.rs`](examples/custom_datasource.rs): Run queries against a custom datasource (TableProvider)
4949
- [`dataframe.rs`](examples/dataframe.rs): Run a query using a DataFrame against a local parquet file
50-
- [`dataframe-to-s3.rs`](examples/external_dependency/dataframe-to-s3.rs): Run a query using a DataFrame against a parquet file from s3
50+
- [`dataframe-to-s3.rs`](examples/external_dependency/dataframe-to-s3.rs): Run a query using a DataFrame against a parquet file from s3 and writing back to s3
51+
- [`dataframe_output.rs`](examples/dataframe_output.rs): Examples of methods which write data out from a DataFrame
5152
- [`dataframe_in_memory.rs`](examples/dataframe_in_memory.rs): Run a query using a DataFrame against data in memory
5253
- [`deserialize_to_struct.rs`](examples/deserialize_to_struct.rs): Convert query results into rust structs using serde
5354
- [`expr_api.rs`](examples/expr_api.rs): Create, execute, simplify and anaylze `Expr`s
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
use datafusion::{dataframe::DataFrameWriteOptions, prelude::*};
19+
use datafusion_common::{parsers::CompressionTypeVariant, DataFusionError};
20+
21+
/// This example demonstrates the various methods to write out a DataFrame to local storage.
22+
/// See datafusion-examples/examples/external_dependency/dataframe-to-s3.rs for an example
23+
/// using a remote object store.
24+
#[tokio::main]
25+
async fn main() -> Result<(), DataFusionError> {
26+
let ctx = SessionContext::new();
27+
28+
let mut df = ctx.sql("values ('a'), ('b'), ('c')").await.unwrap();
29+
30+
// Ensure the column names and types match the target table
31+
df = df.with_column_renamed("column1", "tablecol1").unwrap();
32+
33+
ctx.sql(
34+
"create external table
35+
test(tablecol1 varchar)
36+
stored as parquet
37+
location './datafusion-examples/test_table/'",
38+
)
39+
.await?
40+
.collect()
41+
.await?;
42+
43+
// This is equivalent to INSERT INTO test VALUES ('a'), ('b'), ('c').
44+
// The behavior of write_table depends on the TableProvider's implementation
45+
// of the insert_into method.
46+
df.clone()
47+
.write_table("test", DataFrameWriteOptions::new())
48+
.await?;
49+
50+
df.clone()
51+
.write_parquet(
52+
"./datafusion-examples/test_parquet/",
53+
DataFrameWriteOptions::new(),
54+
None,
55+
)
56+
.await?;
57+
58+
df.clone()
59+
.write_csv(
60+
"./datafusion-examples/test_csv/",
61+
// DataFrameWriteOptions contains options which control how data is written
62+
// such as compression codec
63+
DataFrameWriteOptions::new().with_compression(CompressionTypeVariant::GZIP),
64+
None,
65+
)
66+
.await?;
67+
68+
df.clone()
69+
.write_json(
70+
"./datafusion-examples/test_json/",
71+
DataFrameWriteOptions::new(),
72+
)
73+
.await?;
74+
75+
Ok(())
76+
}

0 commit comments

Comments
 (0)