Skip to content

How to write csv file to disk from a empty dataframe? #16240

Closed
@mmooyyii

Description

@mmooyyii

I want write a csv file only include headers;

use datafusion::config::CsvOptions;
use datafusion::dataframe::DataFrameWriteOptions;
use datafusion::error::Result;
use datafusion::prelude::*;
use std::path::Path;

#[tokio::main]
async fn main() -> Result<()> {
    let ctx = SessionContext::new();

    let df = ctx.sql("SELECT 1 limit 0").await?;

    let mut cfg1 = DataFrameWriteOptions::new();
    cfg1 = cfg1.with_single_file_output(true);

    let mut cfg2 = CsvOptions::default();
    cfg2.has_header = Some(true);

    df.write_csv("/tmp/t.csv", cfg1, Some(cfg2)).await?;
    assert_eq!(Path::new("/tmp/t.csv").exists(), true);  // panic!
    Ok(())
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions