Ask Your Question
4

How can I convert a CSV file to Parquet format using C#?

asked 2023-07-09 14:54:36 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-07-09 15:03:01 +0000

huitzilopochtli gravatar image

To convert a CSV file to Parquet format using C#, you can use the Apache Arrow library. Here's an example code snippet:

using System.IO;
using Apache.Arrow;
using Apache.Arrow.Csv;
using Apache.Arrow.Ipc;

// Input CSV file path
var inputFile = "input.csv";
// Output Parquet file path
var outputFile = "output.parquet";

// Read CSV file as Arrow table
var arrowTable = CsvReader.Read(inputFile);

// Write Arrow table to Parquet file
using (var fileStream = new FileStream(outputFile, FileMode.Create))
{
    using (var fileWriter = new ArrowStreamWriter(fileStream, arrowTable.Schema))
    {
        fileWriter.WriteTable(arrowTable);
    }
}

In this example, we first read the CSV file using the CsvReader class from the Apache Arrow library. This returns an Arrow table containing the data from the CSV file.

We then write this Arrow table to a Parquet file using the ArrowStreamWriter class. We create a file stream for the output file, and use the Arrow table's schema to initialize the ArrowStreamWriter. Finally, we call the WriteTable method on the ArrowStreamWriter to write the table to the output file.

It's worth noting that the Apache Arrow library supports many different data sources and formats, so you can use similar code to convert data between different formats.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-09 14:54:36 +0000

Seen: 13 times

Last updated: Jul 09 '23