一、配置环境变量
1、查看 CLASSPATH
echo $CLASSPATH
2、配置CLASSPATH
vi ~/.bashrc
export CLASSPATH=`hadoop classpath`:$CLASSPATH
source ~/.bashrc
sudo vi /etc/profile
export CLASS_PATH=$CLASS_PATH:`hadoop classpath`
source /etc/profile
3、验证CLASSPATH是否配置好
echo $CLASSPATH
4、配置 /etc/profile 文件
export JRE_HOME=$JAVA_HOME/jre
export CLASS_PATH=$CLASS_PATH:`hadoop classpath`
export PATH=$PATH:$JAVA_HOME/bin
export LD_LIBRARY_PATH=/export/server/hadoop-3.3.4/lib/native/:$LD_LIBRARY_PATH:$JAVA_HOME/jre/lib/amd64/server
二、编写 C 程序连接HDFS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hdfs.h"
int main(int argc, char **argv)
{
hdfsFS fs = hdfsConnect("192.168.48.1", 8020);
const char *writePath = "/data_test/x.txt";
hdfsFile writeFile = hdfsOpenFile(fs, writePath, O_WRONLY | O_CREAT, 131072, 1, 134217728);
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, &buffer, strlen(buffer) + 1);
if (hdfsFlush(fs, writeFile))
{
fprintf(stderr, "Failed to 'flush' %s\n", writePath);
exit(-1);
}
hdfsCloseFile(fs, writeFile);
return 0;
}
三、使用 gcc 命令编译 C 程序
gcc fs.c -I$HADOOP_HOME/include -L$HADOOP_HOME/lib/native -lhdfs -o sample
./sample