cat.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. /**
  18. * Unix-like cat tool example.
  19. *
  20. * Reads the specified file from HDFS and outputs to stdout.
  21. *
  22. * Usage: cat /<path-to-file>
  23. *
  24. * Example: cat /dir/file
  25. *
  26. * @param path-to-file Absolute path to the file to read.
  27. *
  28. **/
  29. #include "hdfspp/hdfspp.h"
  30. #include <google/protobuf/stubs/common.h>
  31. #include "tools_common.h"
  32. const std::size_t BUF_SIZE = 1048576; //1 MB
  33. static char input_buffer[BUF_SIZE];
  34. int main(int argc, char *argv[]) {
  35. if (argc != 2) {
  36. std::cerr << "usage: cat /<path-to-file>" << std::endl;
  37. exit(EXIT_FAILURE);
  38. }
  39. std::string path = argv[1];
  40. //Building a URI object from the given uri path
  41. hdfs::URI uri = hdfs::parse_path_or_exit(path);
  42. std::shared_ptr<hdfs::FileSystem> fs = hdfs::doConnect(uri, false);
  43. if (!fs) {
  44. std::cerr << "Could not connect the file system. " << std::endl;
  45. exit(EXIT_FAILURE);
  46. }
  47. hdfs::FileHandle *file_raw = nullptr;
  48. hdfs::Status status = fs->Open(path, &file_raw);
  49. if (!status.ok()) {
  50. std::cerr << "Could not open file " << path << ". " << status.ToString() << std::endl;
  51. exit(EXIT_FAILURE);
  52. }
  53. //wrapping file_raw into a unique pointer to guarantee deletion
  54. std::unique_ptr<hdfs::FileHandle> file(file_raw);
  55. ssize_t total_bytes_read = 0;
  56. size_t last_bytes_read = 0;
  57. do{
  58. //Reading file chunks
  59. status = file->Read(input_buffer, sizeof(input_buffer), &last_bytes_read);
  60. if(status.ok()) {
  61. //Writing file chunks to stdout
  62. fwrite(input_buffer, last_bytes_read, 1, stdout);
  63. total_bytes_read += last_bytes_read;
  64. } else {
  65. if(status.is_invalid_offset()){
  66. //Reached the end of the file
  67. break;
  68. } else {
  69. std::cerr << "Error reading the file: " << status.ToString() << std::endl;
  70. exit(EXIT_FAILURE);
  71. }
  72. }
  73. } while (last_bytes_read > 0);
  74. // Clean up static data and prevent valgrind memory leaks
  75. google::protobuf::ShutdownProtobufLibrary();
  76. return 0;
  77. }