C++读写HDFS代码

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

1.1 operate_hdfs.h:

#ifndef __OPERATE_HDFS_H
#define __OPERATE_HDFS_H
 
#include 
 
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 
 
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 
#include 
#include 
#include 
#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:"<


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);
}


你可能感兴趣的:(hadoop)