Hadoop给我们提供了使用c语言访问hdfs的API,下面进行简要介绍:
环境:ubuntu14.04 hadoop1.0.1 jdk1.7.0_51
访问hdfs的函数主要定义在hdfs.h文件中,该文件位于hadoop-1.0.1/src/c++/libhdfs/文件夹下,而相应的库文件是位于hadoop-1.0.1/c++/Linux-amd64-64/lib/目录下的libhdfs.so,另外要访问hdfs还需要依赖jdk的相关API,头文件目录包括jdk1.7.0_51/include/和jdk1.7.0_51/include/linux/,库文件为jdk1.7.0_51/jre/lib/amd64/server/目录下的libjvm.so,这些库和包含目录都要在编译连接时给出。下面是一段简单的源程序main.c:
1 #include <stdio.h> 2 3 #include <stdlib.h> 4 5 #include <string.h> 6 7 #include "hdfs.h" 8 9 10 11 int main(int argc, char **argv) 12 13 { 14 15 /* 16 17 * Connection to hdfs. 18 19 */ 20 21 hdfsFS fs = hdfsConnect("127.0.0.1", 9000); 22 23 if(!fs) 24 25 { 26 27 fprintf(stderr, "Failed to connect to hdfs. "); 28 29 exit(-1); 30 31 } 32 33 /* 34 35 * Create and open a file in hdfs. 36 37 */ 38 39 const char* writePath = "/user/root/output/testfile.txt"; 40 41 hdfsFile writeFile = hdfsOpenFile(fs, writePath, O_WRONLY|O_CREAT, 0, 0, 0); 42 43 if(!writeFile) 44 45 { 46 47 fprintf(stderr, "Failed to open %s for writing! ", writePath); 48 49 exit(-1); 50 51 } 52 53 /* 54 55 * Write data to the file. 56 57 */ 58 59 const char* buffer = "Hello, World!"; 60 61 tSize num_written_bytes = hdfsWrite(fs, writeFile, (void*)buffer, strlen(buffer)+1); 62 63 64 65 /* 66 67 * Flush buffer. 68 69 */ 70 71 if (hdfsFlush(fs, writeFile)) 72 73 { 74 75 fprintf(stderr, "Failed to 'flush' %s ", writePath); 76 77 exit(-1); 78 79 } 80 81 82 83 /* 84 85 * Close the file. 86 87 */ 88 89 hdfsCloseFile(fs, writeFile); 90 91 92 93 unsigned bufferSize=1024; 94 95 const char* readPath = "/user/root/output/testfile.txt"; 96 97 hdfsFile readFile = hdfsOpenFile(fs, readPath, O_RDONLY, bufferSize, 0, 0); 98 99 if (!readFile) { 100 101 fprintf(stderr,"couldn't open file %s for reading ",readPath); 102 103 exit(-2); 104 105 } 106 107 // data to be written to the file 108 109 char* rbuffer = (char*)malloc(sizeof(char) * (bufferSize+1)); 110 111 if(rbuffer == NULL) { 112 113 return -2; 114 115 } 116 117 118 119 // read from the file 120 121 tSize curSize = bufferSize; 122 123 for (; curSize == bufferSize;) { 124 125 curSize = hdfsRead(fs, readFile, (void*)rbuffer, curSize); 126 127 rbuffer[curSize]='