Hadoop-2.6.0上的C的API访问HDFS

在使用Hadoop-2.6.0的C API访问HDFS时,遇到编译和运行问题。通过查找资料和尝试,最终解决了动态链接库缺失、环境变量设置等问题。通过添加库路径到`/etc/ld.so.conf`,更新共享库,以及将所需jar文件添加到classpath,成功编译并运行了示例代码。运行时的异常提示如SLF4J绑定和native-hadoop库加载失败,但最终成功创建了HDFS文件。

在通过Hadoop-2.6.0的C的API访问HDFS的时候,编译和运行出现了不少问题,花费了几天的时间,上网查了好多的资料,终于还是把问题给解决了

 参考文献:http://m.blog.csdn.net/blog/Aquester/25242215

系统:CentOS 6.6,hadoop-2.6.0, 在hadoop集群的datanode机器上进行

样例代码来源官方文档中的CAPI libhdfs:

#include"hdfs.h"

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

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

 

       hdfsFS fs =hdfsConnect("10.25.100.130", 9000); //在这里做了一点修改

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

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

        if(!writeFile) {

              fprintf(stderr, "Failed toopen %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);

    }

接下来就是编译,按照官网上给出的:

How To Link With The Library

See the CMake filefor test_libhdfs_ops.c in the libhdfssource directory (hadoop-hdfs-project/hadoop-hdfs/src/CMakeLists.txt) or something like: gcc above_sample.c -I$HADOOP_HDFS_HOME/include -L$HADOOP_HDFS_HOME/lib/native-lhdfs -o above_sample

试用第二种:

[root@node04 ~]# gcc above_sample.c -I/home/hadoop/hadoop-2.6.0/include/ -L /home/hadoop/hadoop-2.6.0/lib/native/-lhdfs -o above_sample

可以通过,查了好多资料,很少有人使用这一种,如何使用这一种有错误,也可以换用另外一种。


我使用的是这一种编译方式:

[root@node04 ~]# gcc above_sample.c -I/home/hadoop/hadoop-2.6.0/include/ -L /home/hadoop/hadoop-2.6.0/lib/native/-lhdfs /usr/java/jdk1.7.0_75/jre/lib/amd64/server/libjvm.so -o above_sample

这两种方法都可以生成一个可执行的文件above_sample

编译通过,可以在运行的时候出现以下错误:

[root@node04 ~]# ./above_sample

./above_sample: error while loading sharedlibraries: libjvm.so: cannot open shared object file: No such file or directory

发生这种报错的原因是,编译的程序运行期间需要依赖某个共享库,比如上面,write可执行程序需要依赖一个叫“libxxxx.so”的共享库。(动态链接库与静态链接库的区别,请百度相关文档)

/etc/ld.so.conf中添加路径,然后重新加载共享库:

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值