hdfs_moveToLocal.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_moveToLocal [OPTION] SRC_FILE DST_FILE"
  22. << std::endl
  23. << std::endl << "Move SRC_FILE from hdfs to DST_FILE on the local file system."
  24. << std::endl << "Moving is done by copying SRC_FILE to DST_FILE, and then"
  25. << std::endl << "deleting DST_FILE if copy succeeded."
  26. << std::endl
  27. << std::endl << " -h display this help and exit"
  28. << std::endl
  29. << std::endl << "Examples:"
  30. << std::endl << "hdfs_moveToLocal hdfs://localhost.localdomain:8020/dir/file /home/usr/myfile"
  31. << std::endl << "hdfs_moveToLocal /dir/file /home/usr/dir/file"
  32. << std::endl;
  33. }
  34. int main(int argc, char *argv[]) {
  35. if (argc > 4) {
  36. usage();
  37. exit(EXIT_FAILURE);
  38. }
  39. int input;
  40. //Using GetOpt to read in the values
  41. opterr = 0;
  42. while ((input = getopt(argc, argv, "h")) != -1) {
  43. switch (input)
  44. {
  45. case 'h':
  46. usage();
  47. exit(EXIT_SUCCESS);
  48. case '?':
  49. if (isprint(optopt))
  50. std::cerr << "Unknown option `-" << (char) optopt << "'." << std::endl;
  51. else
  52. std::cerr << "Unknown option character `" << (char) optopt << "'." << std::endl;
  53. usage();
  54. exit(EXIT_FAILURE);
  55. default:
  56. exit(EXIT_FAILURE);
  57. }
  58. }
  59. std::string uri_path = argv[optind];
  60. std::string dest = argv[optind+1];
  61. //Building a URI object from the given uri_path
  62. hdfs::URI uri = hdfs::parse_path_or_exit(uri_path);
  63. std::shared_ptr<hdfs::FileSystem> fs = hdfs::doConnect(uri, false);
  64. if (!fs) {
  65. std::cerr << "Could not connect the file system. " << std::endl;
  66. exit(EXIT_FAILURE);
  67. }
  68. std::FILE* dst_file = std::fopen(dest.c_str(), "wb");
  69. if(!dst_file){
  70. std::cerr << "Unable to open the destination file: " << dest << std::endl;
  71. exit(EXIT_FAILURE);
  72. }
  73. readFile(fs, uri.get_path(), 0, dst_file, true);
  74. std::fclose(dst_file);
  75. // Clean up static data and prevent valgrind memory leaks
  76. google::protobuf::ShutdownProtobufLibrary();
  77. return 0;
  78. }