1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18#include <iostream>
19#include <memory>
20#include <string>
21
22#include "arrow/ipc/reader.h"
23#include "arrow/ipc/writer.h"
24#include "arrow/record_batch.h"
25#include "arrow/status.h"
26
27#include "arrow/util/io-util.h"
28
29namespace arrow {
30namespace ipc {
31
32// Converts a stream from stdin to a file written to standard out.
33// A typical usage would be:
34// $ <program that produces streaming output> | stream-to-file > file.arrow
35Status ConvertToFile() {
36 io::StdinStream input;
37 std::shared_ptr<RecordBatchReader> reader;
38 RETURN_NOT_OK(RecordBatchStreamReader::Open(&input, &reader));
39
40 io::StdoutStream sink;
41 std::shared_ptr<RecordBatchWriter> writer;
42 RETURN_NOT_OK(RecordBatchFileWriter::Open(&sink, reader->schema(), &writer));
43
44 std::shared_ptr<RecordBatch> batch;
45 while (true) {
46 RETURN_NOT_OK(reader->ReadNext(&batch));
47 if (batch == nullptr) break;
48 RETURN_NOT_OK(writer->WriteRecordBatch(*batch));
49 }
50 return writer->Close();
51}
52
53} // namespace ipc
54} // namespace arrow
55
56int main(int argc, char** argv) {
57 arrow::Status status = arrow::ipc::ConvertToFile();
58 if (!status.ok()) {
59 std::cerr << "Could not convert to file: " << status.ToString() << std::endl;
60 return 1;
61 }
62 return 0;
63}
64