很好-Hadoop通过C的API访问HDFS

在通过Hadoop的C的API 访问HDFS的时候,编译和运行出现了不少问题,在这边,做个总结吧:

系统:Ubuntu11.04,Hadoop-0.20.203.0

样例代码就是参考官方文档中提供到:

[cpp]  view plain  copy
  1. #include "hdfs.h"   
  2.   
  3. int main(int argc, char **argv) {  
  4.   
  5.     hdfsFS fs = hdfsConnect("default", 0);  
  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. }  

编译:官网这样描述

See the Makefile for hdfs_test.c in the libhdfs source directory (${HADOOP_HOME}/src/c++/libhdfs/Makefile) or something like:
gcc above_sample.c -I${HADOOP_HOME}/src/c++/libhdfs -L${HADOOP_HOME}/libhdfs -lhdfs -o above_sample

但是我两个方法都试了,不行,后面发现原来是要少了:

[plain]  view plain  copy
  1. LIB = -L$(HADOOP_INSTALL)/c++/Linux-i386-32/lib/  
  2. libjvm=/usr/lib/jvm/java-6-openjdk/jre/lib/i386/client/libjvm.so  

所以完整到makefile就是:

[plain]  view plain  copy
  1. HADOOP_INSTALL=/home/fzuir/hadoop-0.20.203.0  
  2. PLATFORM=Linux-i386-32  
  3. JAVA_HOME=/usr/lib/jvm/java-6-openjdk/  
  4. CPPFLAGS= -I$(HADOOP_INSTALL)/src/c++/libhdfs  
  5. LIB = -L$(HADOOP_INSTALL)/c++/Linux-i386-32/lib/  
  6. libjvm=/usr/lib/jvm/java-6-openjdk/jre/lib/i386/client/libjvm.so  
  7. LDFLAGS += -lhdfs  
  8.   
  9. testHdfs: testHdfs.c  
  10.     gcc testHdfs.c  $(CPPFLAGS) $(LIB) $(LDFLAGS) $(libjvm) -o testHdfs  
  11.   
  12. clean:  
  13.     rm testHdfs  

好了,编译通过,但是运行的时候出现以下错误信息:

1.

./testHdfs: error while loading shared libraries: xxx.so.0:cannot open shared object file: No such file or directory

解决方法:把xxx.so.0所在的目录添加到/etc/ld.so.conf中,然后/sbin/ldconfig –v下就可以了。

2.

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/configuration/Configuration

...

Call to org.apache.hadoop.fs.Filesystem::get(URI, Configuration) failed!
Exception in thread "main" java.lang.NullPointerException
Call to get configuration object from filesystem failed!

解决方法,修改/etc/profile,添加相应的CLASSPATH:

[plain]  view plain  copy
  1. HADOOP_HOME=/<span><span>home/fzuir/hadoop-0.20.203.0</span></span>  
  2. export PATH=$HADOOP_HOME/bin:$PATH  
  3. export CLASSPATH=.:$HADOOP_HOME/lib/commons-lang-2.4.jar:$HADOOP_HOME/hadoop-cor  
  4. e-1.0.1.jar:$HADOOP_HOME/lib/commons-logging-api-1.0.4.jar:$HADOOP_HOME/lib/comm  
  5. ons-configuration-1.6.jar:$JAVA_HOME/lib:$JRE_HOME/lib:$HADOOP_HOME/contrib/stre  
  6. aming/hadoop-streaming-1.0.1.jar:$CLASSPATH  

最后,恭喜你,问题解决了。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值