hdfs_test.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * Copyright 2005 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "hdfs.h"
  17. int main(int argc, char **argv) {
  18. hdfsFS fs = hdfsConnect("default", 0);
  19. if(!fs) {
  20. fprintf(stderr, "Oops! Failed to connect to hdfs!\n");
  21. exit(-1);
  22. }
  23. hdfsFS lfs = hdfsConnect(NULL, 0);
  24. if(!fs) {
  25. fprintf(stderr, "Oops! Failed to connect to 'local' hdfs!\n");
  26. exit(-1);
  27. }
  28. {
  29. //Write tests
  30. const char* writePath = "/tmp/testfile.txt";
  31. hdfsFile writeFile = hdfsOpenFile(fs, writePath, O_WRONLY, 0, 0, 0);
  32. if(!writeFile) {
  33. fprintf(stderr, "Failed to open %s for writing!\n", writePath);
  34. exit(-1);
  35. }
  36. fprintf(stderr, "Opened %s for writing successfully...\n", writePath);
  37. char* buffer = "Hello, World!";
  38. tSize num_written_bytes = hdfsWrite(fs, writeFile, (void*)buffer, strlen(buffer)+1);
  39. fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);
  40. tOffset currentPos = -1;
  41. if ((currentPos = hdfsTell(fs, writeFile)) == -1) {
  42. fprintf(stderr,
  43. "Failed to get current file position correctly! Got %d!\n",
  44. currentPos);
  45. exit(-1);
  46. }
  47. fprintf(stderr, "Current position: %ld\n", currentPos);
  48. if (hdfsFlush(fs, writeFile)) {
  49. fprintf(stderr, "Failed to 'flush' %s\n", writePath);
  50. exit(-1);
  51. }
  52. fprintf(stderr, "Flushed %s successfully!\n", writePath);
  53. hdfsCloseFile(fs, writeFile);
  54. }
  55. {
  56. //Read tests
  57. const char* readPath = "/tmp/testfile.txt";
  58. hdfsFile readFile = hdfsOpenFile(fs, readPath, O_RDONLY, 0, 0, 0);
  59. if (!readFile) {
  60. fprintf(stderr, "Failed to open %s for reading!\n", readPath);
  61. exit(-1);
  62. }
  63. fprintf(stderr, "hdfsAvailable: %d\n", hdfsAvailable(fs, readFile));
  64. tOffset seekPos = 1;
  65. if(hdfsSeek(fs, readFile, seekPos)) {
  66. fprintf(stderr, "Failed to seek %s for reading!\n", readPath);
  67. exit(-1);
  68. }
  69. tOffset currentPos = -1;
  70. if((currentPos = hdfsTell(fs, readFile)) != seekPos) {
  71. fprintf(stderr,
  72. "Failed to get current file position correctly! Got %d!\n",
  73. currentPos);
  74. exit(-1);
  75. }
  76. fprintf(stderr, "Current position: %ld\n", currentPos);
  77. static char buffer[32];
  78. tSize num_read_bytes = hdfsRead(fs, readFile, (void*)buffer,
  79. sizeof(buffer));
  80. fprintf(stderr, "Read following %d bytes:\n%s\n",
  81. num_read_bytes, buffer);
  82. num_read_bytes = hdfsPread(fs, readFile, 0, (void*)buffer,
  83. sizeof(buffer));
  84. fprintf(stderr, "Read following %d bytes:\n%s\n",
  85. num_read_bytes, buffer);
  86. hdfsCloseFile(fs, readFile);
  87. }
  88. {
  89. //Generic file-system operations
  90. const char* srcPath = "/tmp/testfile.txt";
  91. const char* dstPath = "/tmp/testfile2.txt";
  92. fprintf(stderr, "hdfsCopy(remote-local): %s\n", (hdfsCopy(fs, srcPath, lfs, srcPath) ? "Failed!" : "Success!"));
  93. fprintf(stderr, "hdfsCopy(remote-remote): %s\n", (hdfsCopy(fs, srcPath, fs, dstPath) ? "Failed!" : "Success!"));
  94. fprintf(stderr, "hdfsMove(local-local): %s\n", (hdfsMove(lfs, srcPath, lfs, dstPath) ? "Failed!" : "Success!"));
  95. fprintf(stderr, "hdfsMove(remote-local): %s\n", (hdfsMove(fs, srcPath, lfs, srcPath) ? "Failed!" : "Success!"));
  96. fprintf(stderr, "hdfsRename: %s\n", (hdfsRename(fs, dstPath, srcPath) ? "Failed!" : "Success!"));
  97. fprintf(stderr, "hdfsLock: %s\n", (hdfsLock(fs, srcPath, 1) ? "Failed!" : "Success!"));
  98. fprintf(stderr, "hdfsReleaseLock: %s\n", (hdfsReleaseLock(fs, srcPath) ? "Failed!" : "Success!"));
  99. const char* slashTmp = "/tmp";
  100. const char* newDirectory = "/tmp/newdir";
  101. fprintf(stderr, "hdfsCreateDirectory: %s\n", (hdfsCreateDirectory(fs, newDirectory) ? "Failed!" : "Success!"));
  102. char buffer[256];
  103. fprintf(stderr, "hdfsGetWorkingDirectory: %s\n", (hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer)) ? buffer : "Failed!"));
  104. fprintf(stderr, "hdfsSetWorkingDirectory: %s\n", (hdfsSetWorkingDirectory(fs, slashTmp) ? "Failed!" : "Success!"));
  105. fprintf(stderr, "hdfsGetWorkingDirectory: %s\n", (hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer)) ? buffer : "Failed!"));
  106. fprintf(stderr, "hdfsGetDefaultBlockSize: %Ld\n", hdfsGetDefaultBlockSize(fs));
  107. fprintf(stderr, "hdfsGetCapacity: %Ld\n", hdfsGetCapacity(fs));
  108. fprintf(stderr, "hdfsGetUsed: %Ld\n", hdfsGetUsed(fs));
  109. hdfsFileInfo *fileInfo = NULL;
  110. if(fileInfo = hdfsGetPathInfo(fs, slashTmp)) {
  111. fprintf(stderr, "hdfsGetPathInfo - SUCCESS!\n");
  112. fprintf(stderr, "Name: %s,", fileInfo->mName);
  113. fprintf(stderr, "Type: %c,", (char)fileInfo->mKind);
  114. fprintf(stderr, "Size: %ld\n", fileInfo->mSize);
  115. hdfsFreeFileInfo(fileInfo, 1);
  116. } else {
  117. fprintf(stderr, "waah! hdfsGetPathInfo for %s - FAILED!\n", slashTmp);
  118. }
  119. hdfsFileInfo *fileList = 0;
  120. int numEntries = 0;
  121. if(fileList = hdfsListDirectory(fs, slashTmp, &numEntries)) {
  122. int i = 0;
  123. for(i=0; i < numEntries; ++i) {
  124. fprintf(stderr, "Name: %s,", fileList[i].mName);
  125. fprintf(stderr, "Type: %c,", (char)fileList[i].mKind);
  126. fprintf(stderr, "Size: %ld\n", fileList[i].mSize);
  127. }
  128. hdfsFreeFileInfo(fileList, numEntries);
  129. } else {
  130. if (errno) {
  131. fprintf(stderr, "waah! hdfsListDirectory - FAILED!\n");
  132. } else {
  133. fprintf(stderr, "Empty directory!\n");
  134. }
  135. }
  136. char*** hosts = hdfsGetHosts(fs, srcPath, 0, 1);
  137. if(hosts) {
  138. fprintf(stderr, "hdfsGetHosts - SUCCESS! ... \n");
  139. int i=0;
  140. while(hosts[i]) {
  141. int j = 0;
  142. while(hosts[i][j]) {
  143. fprintf(stderr,
  144. "\thosts[%d][%d] - %s\n", i, j, hosts[i][j]);
  145. ++j;
  146. }
  147. ++i;
  148. }
  149. } else {
  150. fprintf(stderr, "waah! hdfsGetHosts - FAILED!\n");
  151. }
  152. // Clean up
  153. fprintf(stderr, "hdfsDelete: %s\n", (hdfsDelete(fs, newDirectory) ? "Failed!" : "Success!"));
  154. fprintf(stderr, "hdfsDelete: %s\n", (hdfsDelete(fs, srcPath) ? "Failed!" : "Success!"));
  155. fprintf(stderr, "hdfsDelete: %s\n", (hdfsDelete(lfs, srcPath) ? "Failed!" : "Success!"));
  156. fprintf(stderr, "hdfsDelete: %s\n", (hdfsDelete(lfs, dstPath) ? "Failed!" : "Success!"));
  157. }
  158. return 0;
  159. }
  160. /**
  161. * vim: ts=4: sw=4: et:
  162. */