cfsky 发表于 2018-10-31 08:21:53

gcc编译和运行hadoop c api程序

$ cat hdfs_cpp_demo.c  
// Following is a libhdfs sample adapted from the src/c++/libhdfs/hdfs_write.c of the Hadoop distribution.
  
#include "hdfs.h"
  
int main(int argc, char **argv) {
  hdfsFS fs = hdfsConnect("master",9000);
  if (!fs) {
  fprintf(stderr, "Cannot connect to HDFS.\n");
  exit(-1);
  }
  char* fileName = "demo_c.txt";
  char* message = "Welcome to HDFS C API!!!";

  int>  int exists = hdfsExists(fs, fileName);
  if (exists > -1) {
  fprintf(stdout, "File %s exists!\n", fileName);
  }else{
  // Create and open file for writing
  hdfsFile outFile = hdfsOpenFile(fs, fileName, O_WRONLY|O_CREAT, 0, 0, 0);
  if (!outFile) {
  fprintf(stderr, "Failed to open %s for writing!\n", fileName);
  exit(-2);
  }
  // write to file

  hdfsWrite(fs, outFile, (void*)message,>  hdfsCloseFile(fs, outFile);
  }
  // Open file for reading
  hdfsFile inFile = hdfsOpenFile(fs, fileName, O_RDONLY, 0, 0, 0);
  if (!inFile) {
  fprintf(stderr, "Failed to open %s for reading!\n", fileName);
  exit(-2);
  }

  char* data = malloc(sizeof(char) *>  // Read from file.

  tSize readSize = hdfsRead(fs, inFile, (void*)data,>  fprintf(stdout, "%s\n", data);
  free(data);
  hdfsCloseFile(fs, inFile);
  hdfsDisconnect(fs);
  return 0;
  
}


页: [1]
查看完整版本: gcc编译和运行hadoop c api程序