| 46 | const char PARQUET_FILENAME[] = "parquet_cpp_example.parquet"; |
| 47 | |
| 48 | int main(int argc, char** argv) { |
| 49 | /********************************************************************************** |
| 50 | PARQUET WRITER EXAMPLE |
| 51 | **********************************************************************************/ |
| 52 | // parquet::REQUIRED fields do not need definition and repetition level values |
| 53 | // parquet::OPTIONAL fields require only definition level values |
| 54 | // parquet::REPEATED fields require both definition and repetition level values |
| 55 | try { |
| 56 | // Create a local file output stream instance. |
| 57 | using FileClass = ::arrow::io::FileOutputStream; |
| 58 | std::shared_ptr<FileClass> out_file; |
| 59 | PARQUET_ASSIGN_OR_THROW(out_file, FileClass::Open(PARQUET_FILENAME)); |
| 60 | |
| 61 | // Setup the parquet schema |
| 62 | std::shared_ptr<GroupNode> schema = SetupSchema(); |
| 63 | |
| 64 | // Add writer properties |
| 65 | parquet::WriterProperties::Builder builder; |
| 66 | builder.compression(parquet::Compression::SNAPPY); |
| 67 | std::shared_ptr<parquet::WriterProperties> props = builder.build(); |
| 68 | |
| 69 | // Create a ParquetFileWriter instance |
| 70 | std::shared_ptr<parquet::ParquetFileWriter> file_writer = |
| 71 | parquet::ParquetFileWriter::Open(out_file, schema, props); |
| 72 | |
| 73 | // Append a RowGroup with a specific number of rows. |
| 74 | parquet::RowGroupWriter* rg_writer = file_writer->AppendRowGroup(); |
| 75 | |
| 76 | // Write the Bool column |
| 77 | parquet::BoolWriter* bool_writer = |
| 78 | static_cast<parquet::BoolWriter*>(rg_writer->NextColumn()); |
| 79 | for (int i = 0; i < NUM_ROWS_PER_ROW_GROUP; i++) { |
| 80 | bool value = ((i % 2) == 0) ? true : false; |
| 81 | bool_writer->WriteBatch(1, nullptr, nullptr, &value); |
| 82 | } |
| 83 | |
| 84 | // Write the Int32 column |
| 85 | parquet::Int32Writer* int32_writer = |
| 86 | static_cast<parquet::Int32Writer*>(rg_writer->NextColumn()); |
| 87 | for (int i = 0; i < NUM_ROWS_PER_ROW_GROUP; i++) { |
| 88 | int32_t value = i; |
| 89 | int32_writer->WriteBatch(1, nullptr, nullptr, &value); |
| 90 | } |
| 91 | |
| 92 | // Write the Int64 column. Each row has repeats twice. |
| 93 | parquet::Int64Writer* int64_writer = |
| 94 | static_cast<parquet::Int64Writer*>(rg_writer->NextColumn()); |
| 95 | for (int i = 0; i < 2 * NUM_ROWS_PER_ROW_GROUP; i++) { |
| 96 | int64_t value = i * 1000 * 1000; |
| 97 | value *= 1000 * 1000; |
| 98 | int16_t definition_level = 1; |
| 99 | int16_t repetition_level = 0; |
| 100 | if ((i % 2) == 0) { |
| 101 | repetition_level = 1; // start of a new record |
| 102 | } |
| 103 | int64_writer->WriteBatch(1, &definition_level, &repetition_level, &value); |
| 104 | } |
| 105 |
nothing calls this directly
no test coverage detected