|
1 | 1 | #include <cstdlib> |
2 | 2 | #include <filesystem> |
| 3 | +#include <fstream> |
3 | 4 | #include <iostream> |
4 | 5 | #include <vector> |
5 | 6 |
|
6 | 7 | #include "integration_tools.hpp" |
7 | 8 |
|
8 | 9 | /** |
9 | | - * @brief Reads a JSON file containing record batches and outputs the serialized Arrow IPC stream to stdout. |
| 10 | + * @brief Reads a JSON file containing record batches and outputs the serialized Arrow IPC stream to a file. |
10 | 11 | * |
11 | | - * This program takes a JSON file path as a command-line argument, parses the record batches |
12 | | - * from the JSON data, serializes them into Arrow IPC stream format, and writes the binary |
13 | | - * stream to stdout. The output can be redirected to a file or piped to another program. |
| 12 | + * This program takes a JSON file path and an output file path as command-line arguments, |
| 13 | + * parses the record batches from the JSON data, serializes them into Arrow IPC stream format, |
| 14 | + * and writes the binary stream to the specified output file. |
14 | 15 | * |
15 | | - * Usage: arrow_file_to_stream <json_file_path> |
| 16 | + * Usage: arrow_file_to_stream <json_file_path> <output_arrow_file> |
16 | 17 | * |
17 | 18 | * @param argc Number of command-line arguments |
18 | 19 | * @param argv Array of command-line arguments |
19 | 20 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on error |
20 | 21 | */ |
21 | 22 | int main(int argc, char* argv[]) |
22 | 23 | { |
23 | | - // Check command-line arguments |
24 | | - if (argc != 2) |
| 24 | + if (argc != 3) |
25 | 25 | { |
26 | | - std::cerr << "Usage: " << argv[0] << " <json_file_path>\n"; |
27 | | - std::cerr << "Reads a JSON file and outputs the serialized Arrow IPC stream to stdout.\n"; |
| 26 | + std::cerr << "Usage: " << argv[0] << " <json_file_path> <output_arrow_file>\n"; |
| 27 | + std::cerr << "Reads a JSON file and outputs the serialized Arrow IPC stream to a file.\n"; |
28 | 28 | return EXIT_FAILURE; |
29 | 29 | } |
30 | 30 |
|
31 | 31 | const std::filesystem::path json_path(argv[1]); |
| 32 | + const std::filesystem::path output_path(argv[2]); |
32 | 33 |
|
33 | 34 | try |
34 | 35 | { |
35 | | - // Convert JSON file to stream using the library |
36 | | - std::vector<uint8_t> stream_data = integration_tools::json_file_to_stream(json_path); |
37 | | - |
38 | | - // Write the binary stream to stdout |
39 | | - std::cout.write(reinterpret_cast<const char*>(stream_data.data()), stream_data.size()); |
40 | | - std::cout.flush(); |
| 36 | + const std::vector<uint8_t> stream_data = integration_tools::json_file_to_stream(json_path); |
| 37 | + |
| 38 | + std::ofstream output_file(output_path, std::ios::binary); |
| 39 | + if (!output_file) |
| 40 | + { |
| 41 | + std::cerr << "Error: Could not open output file: " << output_path << "\n"; |
| 42 | + return EXIT_FAILURE; |
| 43 | + } |
| 44 | + |
| 45 | + output_file.write(reinterpret_cast<const char*>(stream_data.data()), static_cast<std::streamsize>(stream_data.size())); |
| 46 | + output_file.close(); |
41 | 47 |
|
42 | 48 | return EXIT_SUCCESS; |
43 | 49 | } |
|
0 commit comments