MCPcopy Create free account
hub / github.com/apache/arrow / main

Function main

cpp/examples/parquet/low_level_api/reader_writer2.cc:49–434  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

47const char PARQUET_FILENAME[] = "parquet_cpp_example2.parquet";
48
49int main(int argc, char** argv) {
50 /**********************************************************************************
51 PARQUET WRITER EXAMPLE
52 **********************************************************************************/
53 // parquet::REQUIRED fields do not need definition and repetition level values
54 // parquet::OPTIONAL fields require only definition level values
55 // parquet::REPEATED fields require both definition and repetition level values
56 try {
57 // Create a local file output stream instance.
58 using FileClass = ::arrow::io::FileOutputStream;
59 std::shared_ptr<FileClass> out_file;
60 PARQUET_ASSIGN_OR_THROW(out_file, FileClass::Open(PARQUET_FILENAME));
61
62 // Setup the parquet schema
63 std::shared_ptr<GroupNode> schema = SetupSchema();
64
65 // Add writer properties
66 parquet::WriterProperties::Builder builder;
67 builder.compression(parquet::Compression::SNAPPY);
68 std::shared_ptr<parquet::WriterProperties> props = builder.build();
69
70 // Create a ParquetFileWriter instance
71 std::shared_ptr<parquet::ParquetFileWriter> file_writer =
72 parquet::ParquetFileWriter::Open(out_file, schema, props);
73
74 // Append a BufferedRowGroup to keep the RowGroup open until a certain size
75 parquet::RowGroupWriter* rg_writer = file_writer->AppendBufferedRowGroup();
76
77 int num_columns = file_writer->num_columns();
78 std::vector<int64_t> buffered_values_estimate(num_columns, 0);
79 for (int i = 0; i < NUM_ROWS; i++) {
80 int64_t estimated_bytes = 0;
81 // Get the estimated size of the values that are not written to a page yet
82 for (int n = 0; n < num_columns; n++) {
83 estimated_bytes += buffered_values_estimate[n];
84 }
85
86 // We need to consider the compressed pages
87 // as well as the values that are not compressed yet
88 if ((rg_writer->total_bytes_written() + rg_writer->total_compressed_bytes() +
89 estimated_bytes) > ROW_GROUP_SIZE) {
90 rg_writer->Close();
91 std::fill(buffered_values_estimate.begin(), buffered_values_estimate.end(), 0);
92 rg_writer = file_writer->AppendBufferedRowGroup();
93 }
94
95 int col_id = 0;
96 // Write the Bool column
97 parquet::BoolWriter* bool_writer =
98 static_cast<parquet::BoolWriter*>(rg_writer->column(col_id));
99 bool bool_value = ((i % 2) == 0) ? true : false;
100 bool_writer->WriteBatch(1, nullptr, nullptr, &bool_value);
101 buffered_values_estimate[col_id] = bool_writer->estimated_buffered_value_bytes();
102
103 // Write the Int32 column
104 col_id++;
105 parquet::Int32Writer* int32_writer =
106 static_cast<parquet::Int32Writer*>(rg_writer->column(col_id));

Callers

nothing calls this directly

Calls 15

OpenFileFunction · 0.85
whatMethod · 0.80
SetupSchemaFunction · 0.70
compressionMethod · 0.45
buildMethod · 0.45
num_columnsMethod · 0.45
total_bytes_writtenMethod · 0.45
CloseMethod · 0.45
beginMethod · 0.45

Tested by

no test coverage detected