hdfs_write.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. if (argc != 4) {
  21. fprintf(stderr, "Usage: hdfs_write <filename> <filesize> <buffersize>\n");
  22. exit(-1);
  23. }
  24. hdfsFS fs = hdfsConnect("default", 0);
  25. if (!fs) {
  26. fprintf(stderr, "Oops! Failed to connect to hdfs!\n");
  27. exit(-1);
  28. }
  29. const char* writeFileName = argv[1];
  30. tSize fileTotalSize = strtoul(argv[2], NULL, 10);
  31. tSize bufferSize = strtoul(argv[3], NULL, 10);
  32. hdfsFile writeFile = hdfsOpenFile(fs, writeFileName, O_WRONLY, bufferSize, 0, 0);
  33. if (!writeFile) {
  34. fprintf(stderr, "Failed to open %s for writing!\n", writeFileName);
  35. exit(-2);
  36. }
  37. // data to be written to the file
  38. char* buffer = malloc(sizeof(char) * bufferSize);
  39. if(buffer == NULL) {
  40. return -2;
  41. }
  42. int i = 0;
  43. for (i=0; i < bufferSize; ++i) {
  44. buffer[i] = 'a' + (i%26);
  45. }
  46. // write to the file
  47. tSize nrRemaining;
  48. for (nrRemaining = fileTotalSize; nrRemaining > 0; nrRemaining -= bufferSize ) {
  49. int curSize = ( bufferSize < nrRemaining ) ? bufferSize : (int)nrRemaining;
  50. hdfsWrite(fs, writeFile, (void*)buffer, curSize);
  51. }
  52. free(buffer);
  53. hdfsCloseFile(fs, writeFile);
  54. hdfsDisconnect(fs);
  55. return 0;
  56. }
  57. /**
  58. * vim: ts=4: sw=4: et:
  59. */