您的位置:首页 > 编程语言 > Java开发

hadoop之libhdfs配置及使用(hadoop1.2.1 + eclipse CDT 使用libhdfs)

2015-09-08 21:02 1631 查看
hadoop官方的二进制发布版本一直是32位平台编译的,对于java来说跨平台不影响使用,但是为了在c/c++程序中操作hdfs就做不到了,因为libhdfs.so是二进制不兼容的。

可能需要编译:$cd HADOOP_HOME<br>$ant compile-c++-libhdfs -Dislibhdfs=true(ant使用略)

一:文件目录:

我的hadoop和jdk安装路径HADOOP_HOME=/home/hadoop/hadoop-1.2.1,JAVA_HOME=/usr/lib/jvm/jdk1.8.0_45。

二:工程配置

1.库文件配置

使用libhdfs所需的.h和.c文件:hdfs.h,hdfs.c,hdfsJniHelper.h,hdfsJniHelper.c在${HADOOP_HOME}/src/c++/libhdfs/,将其加到工程src中。

而libhdfs是基于JNI的,所以JAVA的头文件也在哪也要告诉GCC。JAVA头文件在$JAVA_HOME/include和$JAVA_HOME/include/linux下。加到工程includes中

(可能需要修正相关文件中.h的路径,例如jni.h中#include "jni_md.h" -> #include "linux/jni_md.h"),

 Project->Properties->C/C++ Build->Settings->Tool Settings->GCC C++ compiler->includes->include path(-l)

2.动态链接库配置

libhdfs的动态链接库libhdfs.so在$HADOOP_HOME/c++/Linux-amd64-64/lib目录下

JAVA的动态链接库在$JAVA_HOME/jre/lib/amd64/server目录下,使用到libjvm.so

a)在eclipseCDT里配置动态库

Project->Properties->C/C++ Build->Settings->Tool Settings->GCC C++ Linker->Libraries

    添加Libraries (-l): hdfs,jvm

    添加Library search path (-L): {libhdfs.so,libjvm.so路径}

/*Project->Properties->C/C++ Build->Settings->Tool Settings->GCC C++ Linker->Miscellaneous

  other options    添加-R/libhdfs.so路径,-R/libjvm.so路径*/

此时可能设置并未生效,继续b)。

b)设置动态链接库的方法为:

/*

运行报错,提示error while loading shared libraries: libhdfs.so.0: cannot open shared object file: No such file or directory,由于hadoop的c++/Linux-i386-32/lib目录下libhdfs.so、libhdfs.so.0、libhdfs.so.0.0.0文件相同,先将libhdfs.so.0改为libhdfs.so.0.bak,再执行

    $ln -s ./libhdfs.so.0.0.0 ./libhdfs.so.0  #为某一个文件在另外一个位置建立一个同步的链接,软链接:ln -s 源文件 目标文件

*/

在/etc/ld.so.conf.d/目录下新建一个.conf文件,然后在文件里填入链接库所在目录。

$sudo gedit /etc/ld.so.conf.d/hdfs.conf

在此文件中填入目录(使用绝对路径)

$JAVA_HOME/jre/lib/amd64/server

$HADOOP_HOME/c++/Linux-amd64-64/lib

 保存,退出,然后执行命令使配置文件生效

$sudo ldconfig       (出现libhdfs.so.0 不是符号连接)

sudo /sbin/ldconfig -v(很重要!!!!)

/*

./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下就可以了。

*/

/*

3.jar包添加到CLASSPATH中

修改/etc/profile:

#为了将hadoop下所有.jar都加到CLASSPATH中

#find /home/hadoop/hadoop-1.2.1/ -name *.jar|awk '{ printf("export CLASSPATH=%s:$CLASSPATH\n", $0); }'

export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:${HADOOP_HOME}/lib/commons-lang-2.4.jar:${HADOOP_HOME}/hadoop-core-1.2.1.jar:${HADOOP_HOME}/lib/commons-logging-api-1.0.4.jar:${HADOOP_HOME}/lib/commons-configuration-1.6.jar:${HADOOP_HOME}/contrib/streaming/hadoop-streaming-1.2.1.jar:$CLASSPATH

保存修改:$source /etc/profile

*/

三:样例代码

/* testLibhdfs.c */<br><br>#include "hdfs.h"

 

int main(int argc, char **argv) {

 

    hdfsFS fs = hdfsConnect("default", 0);

    const char* writePath = "/tmp/testfile.txt";

    hdfsFile writeFile = hdfsOpenFile(fs, writePath, O_WRONLY|O_CREAT, 0, 0, 0);

    if(!writeFile) {

          fprintf(stderr, "Failed to open %s for writing!\n", writePath);

          exit(-1);

    }

    char* buffer = "Hello, World!";

    tSize num_written_bytes = hdfsWrite(fs, writeFile, (void*)buffer, strlen(buffer)+1);

    if (hdfsFlush(fs, writeFile)) {

           fprintf(stderr, "Failed to 'flush' %s\n", writePath);

          exit(-1);

    }

   hdfsCloseFile(fs, writeFile);

}

参考: http://blog.csdn.net/sprintfwater/article/details/8973089
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hadoop libhdfs CDT