hdfs_write.c 2.8 KB

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