1
0

hdfs_test.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. hdfsCloseFile(fs, readFile);
  83. }
  84. {
  85. //Generic file-system operations
  86. const char* srcPath = "/tmp/testfile.txt";
  87. const char* dstPath = "/tmp/testfile2.txt";
  88. fprintf(stderr, "hdfsCopy(remote-local): %s\n", (hdfsCopy(fs, srcPath, lfs, srcPath) ? "Failed!" : "Success!"));
  89. fprintf(stderr, "hdfsCopy(remote-remote): %s\n", (hdfsCopy(fs, srcPath, fs, dstPath) ? "Failed!" : "Success!"));
  90. fprintf(stderr, "hdfsMove(local-local): %s\n", (hdfsMove(lfs, srcPath, lfs, dstPath) ? "Failed!" : "Success!"));
  91. fprintf(stderr, "hdfsMove(remote-local): %s\n", (hdfsMove(fs, srcPath, lfs, srcPath) ? "Failed!" : "Success!"));
  92. fprintf(stderr, "hdfsRename: %s\n", (hdfsRename(fs, dstPath, srcPath) ? "Failed!" : "Success!"));
  93. fprintf(stderr, "hdfsLock: %s\n", (hdfsLock(fs, srcPath, 1) ? "Failed!" : "Success!"));
  94. fprintf(stderr, "hdfsReleaseLock: %s\n", (hdfsReleaseLock(fs, srcPath) ? "Failed!" : "Success!"));
  95. const char* slashTmp = "/tmp";
  96. const char* newDirectory = "/tmp/newdir";
  97. fprintf(stderr, "hdfsCreateDirectory: %s\n", (hdfsCreateDirectory(fs, newDirectory) ? "Failed!" : "Success!"));
  98. char buffer[256];
  99. fprintf(stderr, "hdfsGetWorkingDirectory: %s\n", (hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer)) ? buffer : "Failed!"));
  100. fprintf(stderr, "hdfsSetWorkingDirectory: %s\n", (hdfsSetWorkingDirectory(fs, slashTmp) ? "Failed!" : "Success!"));
  101. fprintf(stderr, "hdfsGetWorkingDirectory: %s\n", (hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer)) ? buffer : "Failed!"));
  102. fprintf(stderr, "hdfsGetDefaultBlockSize: %Ld\n", hdfsGetDefaultBlockSize(fs));
  103. fprintf(stderr, "hdfsGetCapacity: %Ld\n", hdfsGetCapacity(fs));
  104. fprintf(stderr, "hdfsGetUsed: %Ld\n", hdfsGetUsed(fs));
  105. hdfsFileInfo *fileInfo = NULL;
  106. if(fileInfo = hdfsGetPathInfo(fs, slashTmp)) {
  107. fprintf(stderr, "Yaay! hdfsGetPathInfo - SUCCESS!\n");
  108. fprintf(stderr, "Name: %s,", fileInfo->mName);
  109. fprintf(stderr, "Type: %c,", (char)fileInfo->mKind);
  110. fprintf(stderr, "Size: %ld\n", fileInfo->mSize);
  111. hdfsFreeFileInfo(fileInfo, 1);
  112. } else {
  113. fprintf(stderr, "waah! hdfsGetPathInfo for %s - FAILED!\n", slashTmp);
  114. }
  115. hdfsFileInfo *fileList = 0;
  116. int numEntries = 0;
  117. if(fileList = hdfsListDirectory(fs, slashTmp, &numEntries)) {
  118. int i = 0;
  119. for(i=0; i < numEntries; ++i) {
  120. fprintf(stderr, "Name: %s,", fileList[i].mName);
  121. fprintf(stderr, "Type: %c,", (char)fileList[i].mKind);
  122. fprintf(stderr, "Size: %ld\n", fileList[i].mSize);
  123. }
  124. hdfsFreeFileInfo(fileList, numEntries);
  125. } else {
  126. if (errno) {
  127. fprintf(stderr, "waah! hdfsListDirectory - FAILED!\n");
  128. } else {
  129. fprintf(stderr, "Empty directory!\n");
  130. }
  131. }
  132. char*** hosts = hdfsGetHosts(fs, srcPath, 0, 1);
  133. if(hosts) {
  134. fprintf(stderr, "hdfsGetHosts - SUCCESS! ... \n");
  135. int i=0;
  136. while(hosts[i]) {
  137. int j = 0;
  138. while(hosts[i][j]) {
  139. fprintf(stderr,
  140. "\thosts[%d][%d] - %s\n", i, j, hosts[i][j]);
  141. ++j;
  142. }
  143. ++i;
  144. }
  145. } else {
  146. fprintf(stderr, "waah! hdfsGetHosts - FAILED!\n");
  147. }
  148. // Clean up
  149. fprintf(stderr, "hdfsDelete: %s\n", (hdfsDelete(fs, newDirectory) ? "Failed!" : "Success!"));
  150. fprintf(stderr, "hdfsDelete: %s\n", (hdfsDelete(fs, srcPath) ? "Failed!" : "Success!"));
  151. fprintf(stderr, "hdfsDelete: %s\n", (hdfsDelete(lfs, srcPath) ? "Failed!" : "Success!"));
  152. fprintf(stderr, "hdfsDelete: %s\n", (hdfsDelete(lfs, dstPath) ? "Failed!" : "Success!"));
  153. }
  154. return 0;
  155. }
  156. /**
  157. * vim: ts=4: sw=4: et:
  158. */