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 <list> |
20 | #include <memory> |
21 | |
22 | #include "parquet/api/reader.h" |
23 | |
24 | int main(int argc, char** argv) { |
25 | if (argc > 5 || argc < 2) { |
26 | std::cerr << "Usage: parquet-reader [--only-metadata] [--no-memory-map] [--json] " |
27 | << "[--dump] [--print-key-value-metadata] [--columns=...] <file>" |
28 | << std::endl; |
29 | return -1; |
30 | } |
31 | |
32 | std::string filename; |
33 | bool print_values = true; |
34 | bool print_key_value_metadata = false; |
35 | bool memory_map = true; |
36 | bool format_json = false; |
37 | bool format_dump = false; |
38 | |
39 | // Read command-line options |
40 | const std::string COLUMNS_PREFIX = "--columns=" ; |
41 | std::list<int> columns; |
42 | |
43 | char *param, *value; |
44 | for (int i = 1; i < argc; i++) { |
45 | if ((param = std::strstr(argv[i], "--only-metadata" ))) { |
46 | print_values = false; |
47 | } else if ((param = std::strstr(argv[i], "--print-key-value-metadata" ))) { |
48 | print_key_value_metadata = true; |
49 | } else if ((param = std::strstr(argv[i], "--no-memory-map" ))) { |
50 | memory_map = false; |
51 | } else if ((param = std::strstr(argv[i], "--json" ))) { |
52 | format_json = true; |
53 | } else if ((param = std::strstr(argv[i], "--dump" ))) { |
54 | format_dump = true; |
55 | } else if ((param = std::strstr(argv[i], COLUMNS_PREFIX.c_str()))) { |
56 | value = std::strtok(param + COLUMNS_PREFIX.length(), "," ); |
57 | while (value) { |
58 | columns.push_back(std::atoi(value)); |
59 | value = std::strtok(nullptr, "," ); |
60 | } |
61 | } else { |
62 | filename = argv[i]; |
63 | } |
64 | } |
65 | |
66 | try { |
67 | std::unique_ptr<parquet::ParquetFileReader> reader = |
68 | parquet::ParquetFileReader::OpenFile(filename, memory_map); |
69 | parquet::ParquetFilePrinter printer(reader.get()); |
70 | if (format_json) { |
71 | printer.JSONPrint(std::cout, columns, filename.c_str()); |
72 | } else { |
73 | printer.DebugPrint(std::cout, columns, print_values, |
74 | format_dump, print_key_value_metadata, filename.c_str()); |
75 | } |
76 | } catch (const std::exception& e) { |
77 | std::cerr << "Parquet error: " << e.what() << std::endl; |
78 | return -1; |
79 | } |
80 | |
81 | return 0; |
82 | } |
83 | |