| 125 | } |
| 126 | |
| 127 | arrow::Status WriteInBatches(std::string path_to_file) { |
| 128 | // #include "parquet/arrow/writer.h" |
| 129 | // #include "arrow/util/type_fwd.h" |
| 130 | using parquet::ArrowWriterProperties; |
| 131 | using parquet::WriterProperties; |
| 132 | |
| 133 | // Data is in RBR |
| 134 | std::shared_ptr<arrow::RecordBatchReader> batch_stream; |
| 135 | ARROW_ASSIGN_OR_RAISE(batch_stream, GetRBR()); |
| 136 | |
| 137 | // Choose compression |
| 138 | std::shared_ptr<WriterProperties> props = |
| 139 | WriterProperties::Builder().compression(arrow::Compression::SNAPPY)->build(); |
| 140 | |
| 141 | // Opt to store Arrow schema for easier reads back into Arrow |
| 142 | std::shared_ptr<ArrowWriterProperties> arrow_props = |
| 143 | ArrowWriterProperties::Builder().store_schema()->build(); |
| 144 | |
| 145 | // Create a writer |
| 146 | std::shared_ptr<arrow::io::FileOutputStream> outfile; |
| 147 | ARROW_ASSIGN_OR_RAISE(outfile, arrow::io::FileOutputStream::Open(path_to_file)); |
| 148 | std::unique_ptr<parquet::arrow::FileWriter> writer; |
| 149 | ARROW_ASSIGN_OR_RAISE( |
| 150 | writer, parquet::arrow::FileWriter::Open(*batch_stream->schema().get(), |
| 151 | arrow::default_memory_pool(), outfile, |
| 152 | props, arrow_props)); |
| 153 | |
| 154 | // Write each batch as a row_group |
| 155 | for (arrow::Result<std::shared_ptr<arrow::RecordBatch>> maybe_batch : *batch_stream) { |
| 156 | ARROW_ASSIGN_OR_RAISE(auto batch, maybe_batch); |
| 157 | ARROW_ASSIGN_OR_RAISE(auto table, |
| 158 | arrow::Table::FromRecordBatches(batch->schema(), {batch})); |
| 159 | ARROW_RETURN_NOT_OK(writer->WriteTable(*table.get(), batch->num_rows())); |
| 160 | } |
| 161 | |
| 162 | // Write file footer and close |
| 163 | ARROW_RETURN_NOT_OK(writer->Close()); |
| 164 | |
| 165 | return arrow::Status::OK(); |
| 166 | } |
| 167 | |
| 168 | arrow::Status RunExamples(const std::string& path_to_file) { |
| 169 | ARROW_RETURN_NOT_OK(WriteFullFile(path_to_file)); |
no test coverage detected