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

Function WriteInBatches

cpp/examples/arrow/parquet_read_write.cc:127–166  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

125}
126
127arrow::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
168arrow::Status RunExamples(const std::string& path_to_file) {
169 ARROW_RETURN_NOT_OK(WriteFullFile(path_to_file));

Callers 1

RunExamplesFunction · 0.85

Calls 13

GetRBRFunction · 0.85
default_memory_poolFunction · 0.85
store_schemaMethod · 0.80
ARROW_ASSIGN_OR_RAISEFunction · 0.70
BuilderFunction · 0.50
OKFunction · 0.50
buildMethod · 0.45
compressionMethod · 0.45
getMethod · 0.45
schemaMethod · 0.45
WriteTableMethod · 0.45
num_rowsMethod · 0.45

Tested by

no test coverage detected