hdfs_count.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #include <google/protobuf/stubs/common.h>
  18. #include <unistd.h>
  19. #include "tools_common.h"
  20. void usage(){
  21. std::cout << "Usage: hdfs_count [OPTION] FILE"
  22. << std::endl
  23. << std::endl << "Count the number of directories, files and bytes under the path that match the specified FILE pattern."
  24. << std::endl << "The output columns with -count are: DIR_COUNT, FILE_COUNT, CONTENT_SIZE, PATHNAME"
  25. << std::endl
  26. << std::endl << " -q output additional columns before the rest: QUOTA, SPACE_QUOTA, SPACE_CONSUMED"
  27. << std::endl << " -h display this help and exit"
  28. << std::endl
  29. << std::endl << "Examples:"
  30. << std::endl << "hdfs_count hdfs://localhost.localdomain:8020/dir"
  31. << std::endl << "hdfs_count -q /dir1/dir2"
  32. << std::endl;
  33. }
  34. int main(int argc, char *argv[]) {
  35. //We should have at least 2 arguments
  36. if (argc < 2) {
  37. usage();
  38. exit(EXIT_FAILURE);
  39. }
  40. bool quota = false;
  41. int input;
  42. //Using GetOpt to read in the values
  43. opterr = 0;
  44. while ((input = getopt(argc, argv, "qh")) != -1) {
  45. switch (input)
  46. {
  47. case 'q':
  48. quota = true;
  49. break;
  50. case 'h':
  51. usage();
  52. exit(EXIT_SUCCESS);
  53. case '?':
  54. if (isprint(optopt))
  55. std::cerr << "Unknown option `-" << (char) optopt << "'." << std::endl;
  56. else
  57. std::cerr << "Unknown option character `" << (char) optopt << "'." << std::endl;
  58. usage();
  59. exit(EXIT_FAILURE);
  60. default:
  61. exit(EXIT_FAILURE);
  62. }
  63. }
  64. std::string uri_path = argv[optind];
  65. //Building a URI object from the given uri_path
  66. hdfs::URI uri = hdfs::parse_path_or_exit(uri_path);
  67. std::shared_ptr<hdfs::FileSystem> fs = hdfs::doConnect(uri, false);
  68. if (!fs) {
  69. std::cerr << "Could not connect the file system. " << std::endl;
  70. exit(EXIT_FAILURE);
  71. }
  72. hdfs::ContentSummary content_summary;
  73. hdfs::Status status = fs->GetContentSummary(uri.get_path(), content_summary);
  74. if (!status.ok()) {
  75. std::cerr << "Error: " << status.ToString() << std::endl;
  76. exit(EXIT_FAILURE);
  77. }
  78. std::cout << content_summary.str(quota) << std::endl;
  79. // Clean up static data and prevent valgrind memory leaks
  80. google::protobuf::ShutdownProtobufLibrary();
  81. return 0;
  82. }