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/io/file.h"
23#include "arrow/ipc/reader.h"
24#include "arrow/ipc/writer.h"
25#include "arrow/status.h"
26
27#include "arrow/util/io-util.h"
28
29namespace arrow {
30
31class RecordBatch;
32
33namespace ipc {
34
35// Reads a file on the file system and prints to stdout the stream version of it.
36Status ConvertToStream(const char* path) {
37 std::shared_ptr<io::ReadableFile> in_file;
38 std::shared_ptr<RecordBatchFileReader> reader;
39
40 RETURN_NOT_OK(io::ReadableFile::Open(path, &in_file));
41 RETURN_NOT_OK(ipc::RecordBatchFileReader::Open(in_file.get(), &reader));
42
43 io::StdoutStream sink;
44 std::shared_ptr<RecordBatchWriter> writer;
45 RETURN_NOT_OK(RecordBatchStreamWriter::Open(&sink, reader->schema(), &writer));
46 for (int i = 0; i < reader->num_record_batches(); ++i) {
47 std::shared_ptr<RecordBatch> chunk;
48 RETURN_NOT_OK(reader->ReadRecordBatch(i, &chunk));
49 RETURN_NOT_OK(writer->WriteRecordBatch(*chunk));
50 }
51 return writer->Close();
52}
53
54} // namespace ipc
55} // namespace arrow
56
57int main(int argc, char** argv) {
58 if (argc != 2) {
59 std::cerr << "Usage: file-to-stream <input arrow file>" << std::endl;
60 return 1;
61 }
62 arrow::Status status = arrow::ipc::ConvertToStream(argv[1]);
63 if (!status.ok()) {
64 std::cerr << "Could not convert to stream: " << status.ToString() << std::endl;
65 return 1;
66 }
67 return 0;
68}
69