在通过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);
}
接下来就是编译,按照官网上给出的:
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中添加路径,然后重新加载共享库:

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

被折叠的 条评论
为什么被折叠?



