|  | 
|  | 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