test_libhdfs_read.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "hdfs.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. int main(int argc, char **argv) {
  22. hdfsFS fs;
  23. const char *rfile = argv[1];
  24. tSize bufferSize = strtoul(argv[3], NULL, 10);
  25. hdfsFile readFile;
  26. char* buffer;
  27. tSize curSize;
  28. if (argc != 4) {
  29. fprintf(stderr, "Usage: hdfs_read <filename> <filesize> <buffersize>\n");
  30. exit(-1);
  31. }
  32. fs = hdfsConnect("default", 0);
  33. if (!fs) {
  34. fprintf(stderr, "Oops! Failed to connect to hdfs!\n");
  35. exit(-1);
  36. }
  37. readFile = hdfsOpenFile(fs, rfile, O_RDONLY, bufferSize, 0, 0);
  38. if (!readFile) {
  39. fprintf(stderr, "Failed to open %s for writing!\n", rfile);
  40. exit(-2);
  41. }
  42. // data to be written to the file
  43. buffer = malloc(sizeof(char) * bufferSize);
  44. if(buffer == NULL) {
  45. return -2;
  46. }
  47. // read from the file
  48. curSize = bufferSize;
  49. for (; curSize == bufferSize;) {
  50. curSize = hdfsRead(fs, readFile, (void*)buffer, curSize);
  51. }
  52. free(buffer);
  53. hdfsCloseFile(fs, readFile);
  54. hdfsDisconnect(fs);
  55. return 0;
  56. }
  57. /**
  58. * vim: ts=4: sw=4: et:
  59. */