test_libhdfs_write.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/hdfs.h"
  19. #include <limits.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <sys/types.h>
  23. int main(int argc, char **argv) {
  24. hdfsFS fs;
  25. const char *writeFileName = argv[1];
  26. off_t fileTotalSize = strtoul(argv[2], NULL, 10);
  27. long long tmpBufferSize = strtoul(argv[3], NULL, 10);
  28. tSize bufferSize;
  29. hdfsFile writeFile;
  30. char* buffer;
  31. int i;
  32. off_t nrRemaining;
  33. tSize curSize;
  34. tSize written;
  35. if (argc != 4) {
  36. fprintf(stderr, "Usage: hdfs_write <filename> <filesize> <buffersize>\n");
  37. exit(-1);
  38. }
  39. fs = hdfsConnect("default", 0);
  40. if (!fs) {
  41. fprintf(stderr, "Oops! Failed to connect to hdfs!\n");
  42. exit(-1);
  43. }
  44. // sanity check
  45. if(fileTotalSize == ULONG_MAX && errno == ERANGE) {
  46. fprintf(stderr, "invalid file size %s - must be <= %lu\n", argv[2], ULONG_MAX);
  47. exit(-3);
  48. }
  49. // currently libhdfs writes are of tSize which is int32
  50. if(tmpBufferSize > INT_MAX) {
  51. fprintf(stderr, "invalid buffer size libhdfs API write chunks must be <= %d\n",INT_MAX);
  52. exit(-3);
  53. }
  54. bufferSize = (tSize)tmpBufferSize;
  55. writeFile = hdfsOpenFile(fs, writeFileName, O_WRONLY, bufferSize, 0, 0);
  56. if (!writeFile) {
  57. fprintf(stderr, "Failed to open %s for writing!\n", writeFileName);
  58. exit(-2);
  59. }
  60. // data to be written to the file
  61. buffer = malloc(sizeof(char) * bufferSize);
  62. if(buffer == NULL) {
  63. fprintf(stderr, "Could not allocate buffer of size %d\n", bufferSize);
  64. return -2;
  65. }
  66. for (i=0; i < bufferSize; ++i) {
  67. buffer[i] = 'a' + (i%26);
  68. }
  69. // write to the file
  70. for (nrRemaining = fileTotalSize; nrRemaining > 0; nrRemaining -= bufferSize ) {
  71. curSize = ( bufferSize < nrRemaining ) ? bufferSize : (tSize)nrRemaining;
  72. if ((written = hdfsWrite(fs, writeFile, (void*)buffer, curSize)) != curSize) {
  73. fprintf(stderr, "ERROR: hdfsWrite returned an error on write: %d\n", written);
  74. exit(-3);
  75. }
  76. }
  77. free(buffer);
  78. hdfsCloseFile(fs, writeFile);
  79. hdfsDisconnect(fs);
  80. return 0;
  81. }
  82. /**
  83. * vim: ts=4: sw=4: et:
  84. */