C++读写HDFS代码

1. C++读写HDFS工程HadoopClient代码

1.1 operate_hdfs.h:

#ifndef __OPERATE_HDFS_H
#define __OPERATE_HDFS_H
 
#include <hdfs.h>
 
class operatehdfs
{
public:
   operatehdfs();
    voidoperatehdfs_conn(char const *ip, int port);
    voidoperatehdfs_open(char *filename);
    int  operatehdfs_write(unsigned char *buff, intbufflen);
    voidoperatehdfs_close();
    voidoperatehdfs_rename(const char *oldPath, const char *newPath);
private:
    hdfsFSfs;
    hdfsFilewriteFile;
};
#endif

1.2 operate_hdfs.cpp

#include "operate_hdfs.h"
 
#include <stdio.h>
 
operatehdfs::operatehdfs()
{
 
}
 
voidoperatehdfs::operatehdfs_conn(charconst *ip,int port)
{
    fs = hdfsConnect(ip, port);
    if (!fs)
    {
        printf("connect hdfserror!\n");
    }
}
 
voidoperatehdfs::operatehdfs_open(char*filename)
{
    writeFile = hdfsOpenFile(fs, filename, O_WRONLY|O_CREAT,0,0,0);
    if (NULL== writeFile)
    {
        printf("failed fs:%p open%s for writing !\n", fs, filename);
    }
}
 
intoperatehdfs::operatehdfs_write(unsignedchar *buff,int bufflen)
{
    if (NULL== writeFile)
        return 0;
    int num_written_bytes = hdfsWrite(fs, writeFile,(void*)buff, bufflen);
    if (hdfsFlush(fs, writeFile))
       {
        printf("failed toflush\n");
        return 0;
    }
    return num_written_bytes;
}
 
voidoperatehdfs::operatehdfs_close()
{
       if (NULL!= writeFile)
              hdfsCloseFile(fs, writeFile);
}
 
voidoperatehdfs::operatehdfs_rename(constchar* oldPath,const char* newPath)
{
       int ret = 0;
       ret = hdfsRename(fs, oldPath, newPath);
}


1.3 HadoopClient.cpp

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "operate_hdfs.h"
 
using namespace std;
 
int main()
{
   operatehdfs ophd;
    charwritepath[] = "/input/testfile.txt";
    charwritechar[] = "this is test!";
   ophd.operatehdfs_conn("192.168.241.235", 9000); //ip为安装hdfs的服务器,连接hdfs使用的端口
   ophd.operatehdfs_open(writepath);
    int sul= ophd.operatehdfs_write((unsigned char *)writechar, strlen(writechar));
   cout<<"success:"<<sul<<endl;
   ophd.operatehdfs_close();
    return0;
}


2. C++读写HDFS工程HadoopClient2代码

2.1 HadoopClient2.c

#include "hdfs.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
 
int main(int argc, char **argv) {
 
    hdfsFSfs = hdfsConnect("192.168.241.235", 9000);
    constchar* writePath = "/tmp/testfile.txt";
    hdfsFilewriteFile = 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!";
    tSizenum_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);
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HDFS(Hadoop Distributed File System)是一个分布式文件系统,它的设计就是为了能够在通用硬件上运行,同时提供高吞吐量的数据访问。下面是HDFS文件读写的一些Java代码示例: 1. 读取文件 ```java // 获取Hadoop配置信息 Configuration conf = new Configuration(); // 构造一个HDFS文件系统对象 FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"), conf); // 构造一个输入流,用于读取文件内容 Path inFile = new Path("/user/hadoop/input/test.txt"); FSDataInputStream in = fs.open(inFile); // 读取文件内容 byte[] buffer = new byte[1024]; int bytesRead = in.read(buffer); while (bytesRead > 0) { System.out.println(new String(buffer, 0, bytesRead)); bytesRead = in.read(buffer); } // 关闭输入流 in.close(); ``` 2. 写入文件 ```java // 获取Hadoop配置信息 Configuration conf = new Configuration(); // 构造一个HDFS文件系统对象 FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"), conf); // 构造一个输出流,用于写入文件内容 Path outFile = new Path("/user/hadoop/output/test.txt"); FSDataOutputStream out = fs.create(outFile); // 写入文件内容 out.write("Hello, World!".getBytes()); // 关闭输出流 out.close(); ``` 3. 检查文件是否存在 ```java // 获取Hadoop配置信息 Configuration conf = new Configuration(); // 构造一个HDFS文件系统对象 FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"), conf); // 检查文件是否存在 Path path = new Path("/user/hadoop/input/test.txt"); boolean exists = fs.exists(path); System.out.println("File exists: " + exists); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值