hdfs_test.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. int main(int argc, char **argv) {
  20. hdfsFS fs = hdfsConnect("default", 0);
  21. if(!fs) {
  22. fprintf(stderr, "Oops! Failed to connect to hdfs!\n");
  23. exit(-1);
  24. }
  25. hdfsFS lfs = hdfsConnect(NULL, 0);
  26. if(!fs) {
  27. fprintf(stderr, "Oops! Failed to connect to 'local' hdfs!\n");
  28. exit(-1);
  29. }
  30. {
  31. //Write tests
  32. const char* writePath = "/tmp/testfile.txt";
  33. hdfsFile writeFile = hdfsOpenFile(fs, writePath, O_WRONLY, 0, 0, 0);
  34. if(!writeFile) {
  35. fprintf(stderr, "Failed to open %s for writing!\n", writePath);
  36. exit(-1);
  37. }
  38. fprintf(stderr, "Opened %s for writing successfully...\n", writePath);
  39. char* buffer = "Hello, World!";
  40. tSize num_written_bytes = hdfsWrite(fs, writeFile, (void*)buffer, strlen(buffer)+1);
  41. fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);
  42. tOffset currentPos = -1;
  43. if ((currentPos = hdfsTell(fs, writeFile)) == -1) {
  44. fprintf(stderr,
  45. "Failed to get current file position correctly! Got %d!\n",
  46. currentPos);
  47. exit(-1);
  48. }
  49. fprintf(stderr, "Current position: %ld\n", currentPos);
  50. if (hdfsFlush(fs, writeFile)) {
  51. fprintf(stderr, "Failed to 'flush' %s\n", writePath);
  52. exit(-1);
  53. }
  54. fprintf(stderr, "Flushed %s successfully!\n", writePath);
  55. hdfsCloseFile(fs, writeFile);
  56. }
  57. {
  58. //Read tests
  59. const char* readPath = "/tmp/testfile.txt";
  60. hdfsFile readFile = hdfsOpenFile(fs, readPath, O_RDONLY, 0, 0, 0);
  61. if (!readFile) {
  62. fprintf(stderr, "Failed to open %s for reading!\n", readPath);
  63. exit(-1);
  64. }
  65. fprintf(stderr, "hdfsAvailable: %d\n", hdfsAvailable(fs, readFile));
  66. tOffset seekPos = 1;
  67. if(hdfsSeek(fs, readFile, seekPos)) {
  68. fprintf(stderr, "Failed to seek %s for reading!\n", readPath);
  69. exit(-1);
  70. }
  71. tOffset currentPos = -1;
  72. if((currentPos = hdfsTell(fs, readFile)) != seekPos) {
  73. fprintf(stderr,
  74. "Failed to get current file position correctly! Got %d!\n",
  75. currentPos);
  76. exit(-1);
  77. }
  78. fprintf(stderr, "Current position: %ld\n", currentPos);
  79. static char buffer[32];
  80. tSize num_read_bytes = hdfsRead(fs, readFile, (void*)buffer,
  81. sizeof(buffer));
  82. fprintf(stderr, "Read following %d bytes:\n%s\n",
  83. num_read_bytes, buffer);
  84. num_read_bytes = hdfsPread(fs, readFile, 0, (void*)buffer,
  85. sizeof(buffer));
  86. fprintf(stderr, "Read following %d bytes:\n%s\n",
  87. num_read_bytes, buffer);
  88. hdfsCloseFile(fs, readFile);
  89. }
  90. {
  91. //Generic file-system operations
  92. const char* srcPath = "/tmp/testfile.txt";
  93. const char* dstPath = "/tmp/testfile2.txt";
  94. fprintf(stderr, "hdfsCopy(remote-local): %s\n", (hdfsCopy(fs, srcPath, lfs, srcPath) ? "Failed!" : "Success!"));
  95. fprintf(stderr, "hdfsCopy(remote-remote): %s\n", (hdfsCopy(fs, srcPath, fs, dstPath) ? "Failed!" : "Success!"));
  96. fprintf(stderr, "hdfsMove(local-local): %s\n", (hdfsMove(lfs, srcPath, lfs, dstPath) ? "Failed!" : "Success!"));
  97. fprintf(stderr, "hdfsMove(remote-local): %s\n", (hdfsMove(fs, srcPath, lfs, srcPath) ? "Failed!" : "Success!"));
  98. fprintf(stderr, "hdfsRename: %s\n", (hdfsRename(fs, dstPath, srcPath) ? "Failed!" : "Success!"));
  99. fprintf(stderr, "hdfsLock: %s\n", (hdfsLock(fs, srcPath, 1) ? "Failed!" : "Success!"));
  100. fprintf(stderr, "hdfsReleaseLock: %s\n", (hdfsReleaseLock(fs, srcPath) ? "Failed!" : "Success!"));
  101. const char* slashTmp = "/tmp";
  102. const char* newDirectory = "/tmp/newdir";
  103. fprintf(stderr, "hdfsCreateDirectory: %s\n", (hdfsCreateDirectory(fs, newDirectory) ? "Failed!" : "Success!"));
  104. char buffer[256];
  105. fprintf(stderr, "hdfsGetWorkingDirectory: %s\n", (hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer)) ? buffer : "Failed!"));
  106. fprintf(stderr, "hdfsSetWorkingDirectory: %s\n", (hdfsSetWorkingDirectory(fs, slashTmp) ? "Failed!" : "Success!"));
  107. fprintf(stderr, "hdfsGetWorkingDirectory: %s\n", (hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer)) ? buffer : "Failed!"));
  108. fprintf(stderr, "hdfsGetDefaultBlockSize: %Ld\n", hdfsGetDefaultBlockSize(fs));
  109. fprintf(stderr, "hdfsGetCapacity: %Ld\n", hdfsGetCapacity(fs));
  110. fprintf(stderr, "hdfsGetUsed: %Ld\n", hdfsGetUsed(fs));
  111. hdfsFileInfo *fileInfo = NULL;
  112. if(fileInfo = hdfsGetPathInfo(fs, slashTmp)) {
  113. fprintf(stderr, "hdfsGetPathInfo - SUCCESS!\n");
  114. fprintf(stderr, "Name: %s,", fileInfo->mName);
  115. fprintf(stderr, "Type: %c,", (char)fileInfo->mKind);
  116. fprintf(stderr, "Size: %ld\n", fileInfo->mSize);
  117. hdfsFreeFileInfo(fileInfo, 1);
  118. } else {
  119. fprintf(stderr, "waah! hdfsGetPathInfo for %s - FAILED!\n", slashTmp);
  120. }
  121. hdfsFileInfo *fileList = 0;
  122. int numEntries = 0;
  123. if(fileList = hdfsListDirectory(fs, slashTmp, &numEntries)) {
  124. int i = 0;
  125. for(i=0; i < numEntries; ++i) {
  126. fprintf(stderr, "Name: %s,", fileList[i].mName);
  127. fprintf(stderr, "Type: %c,", (char)fileList[i].mKind);
  128. fprintf(stderr, "Size: %ld\n", fileList[i].mSize);
  129. }
  130. hdfsFreeFileInfo(fileList, numEntries);
  131. } else {
  132. if (errno) {
  133. fprintf(stderr, "waah! hdfsListDirectory - FAILED!\n");
  134. } else {
  135. fprintf(stderr, "Empty directory!\n");
  136. }
  137. }
  138. char*** hosts = hdfsGetHosts(fs, srcPath, 0, 1);
  139. if(hosts) {
  140. fprintf(stderr, "hdfsGetHosts - SUCCESS! ... \n");
  141. int i=0;
  142. while(hosts[i]) {
  143. int j = 0;
  144. while(hosts[i][j]) {
  145. fprintf(stderr,
  146. "\thosts[%d][%d] - %s\n", i, j, hosts[i][j]);
  147. ++j;
  148. }
  149. ++i;
  150. }
  151. } else {
  152. fprintf(stderr, "waah! hdfsGetHosts - FAILED!\n");
  153. }
  154. // Clean up
  155. fprintf(stderr, "hdfsDelete: %s\n", (hdfsDelete(fs, newDirectory) ? "Failed!" : "Success!"));
  156. fprintf(stderr, "hdfsDelete: %s\n", (hdfsDelete(fs, srcPath) ? "Failed!" : "Success!"));
  157. fprintf(stderr, "hdfsDelete: %s\n", (hdfsDelete(lfs, srcPath) ? "Failed!" : "Success!"));
  158. fprintf(stderr, "hdfsDelete: %s\n", (hdfsDelete(lfs, dstPath) ? "Failed!" : "Success!"));
  159. }
  160. return 0;
  161. }
  162. /**
  163. * vim: ts=4: sw=4: et:
  164. */