1.用haddop提供的C API to HDFS来实现文件写入到HDFS中。过程中主要是在配置环境花了点时间
参考官网:http://hadoop.apache.org/common/docs/r0.20.203.0/libhdfs.html
不用重新编译直接用$HADOOP_HOME/c++/Linux-Linux-amd64-64/lib即可,若要编译libhdfs,在hadoop顶层目录运行:ant compile-c++-libhdfs -Dislibhdfs=true
API主要可以去hadoop软件包解压目录中查看hdfs.h定义的一些已实现的函数
一门语言的初学入门例子,一般都是“hello,world”,下面看写hdfs文件代码,文件hello_hdfs.c
1 #include "hdfs.h" 2 3 int main(int argc, char **argv) { 4 5 hdfsFS fs = hdfsConnect("127.0.0.1", 9000); 6 const char* writePath = "/tmp/testfile.txt"; 7 hdfsFile writeFile = hdfsOpenFile(fs, writePath, O_WRONLY|O_CREAT, 0, 0, 0); 8 if(!writeFile) { 9 fprintf(stderr, "Failed to open %s for writing!\n", writePath); 10 exit(-1); 11 } 12 char* buffer = "Hello, World!"; 13 tSize num_written_bytes = hdfsWrite(fs, writeFile, (void*)buffer, strlen(buffer)+1); 14 if (hdfsFlush(fs, writeFile)) { 15 fprintf(stderr, "Failed to 'flush' %s\n", writePath); 16 exit(-1); 17 } 18 hdfsCloseFile(fs, writeFile); 19 return 0; 20 }
编译命令:
gcc hello_hdfs.c -I${HADOOP_HOME}/src/c++/libhdfs -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux -L${HADOOP_HOME}/c++/Linux-Linux-amd64-64/lib -lhdfs -L${JAVA_HOME}/jre/lib/amd64/server -ljvm -o hello_hdfs
运行的时候报:error while loading shared libraries: libjvm.so: cannot open shared object file: No such file or directory
这个是LD_LIBRARY_PATH 没有设置:
这个是我本机上面的配置: /etc/profile(Ubuntu有一个bug,在这里设置的所有环境变量都是all user wide,除了LD_LIBRARY_PATH,你必须重新source一遍才行)
export JAVA_HOME=/usr/lib/jvm/java-6-sun
export LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/amd64/server
export PATH=$PATH:$JAVA_HOME/bin
export HADOOP_HOME=/home/hadoop/hadoop-0.20.203.0
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$HADOOP_HOME/hadoop-core-0.20.203.0.jar:$HADOOP_HOME/lib/commons-logging-1.1.1.jar:$HADOOP_HOME/lib/commons-configuration-1.6.jar:$HADOOP_HOME/lib/commons-lang-2.4.jar
查看文件是否写入,bin/hadoop fs -ls /tmp,或直接拷到本地以便查看:bin/hadoop fs -get /tmp/testfile.txt /home/test
如果写入不成功,一定要检查启动hadoop的是否与运行这个程序的是同一个用户!