C++读写HDFS代码
来源:互联网 发布:公安网络攻防测试 编辑:程序博客网 时间:2024/05/17 22:27
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 0
- C++读写HDFS代码
- HDFS读写文件的java代码
- HDFS API 文件读写代码演示
- hdfs java读写hdfs demo
- HDFS文件读写
- HDFS读写过程解析
- 使用hdfsAPI读写hdfs
- HDFS上读写
- HDFS读写过程解析
- HDFS读写数据流
- HDFS 读写流程
- HDFS简单读写分析
- HDFS读写数据过程
- HDFS 读写流程
- hdfs java 文件读写
- HDFS 读写流程概要
- hdfs读写流程
- java读写HDFS
- Ubuntu16.04安装inetsim模拟Internet
- Redis与Memcached区别
- mysql having的用法
- 有史以来在表格下方新增空行最快捷的方法(没有之一)
- Appach_Tomcat整合架设
- C++读写HDFS代码
- oracle创建数据库以及用户表空间
- poj 3254 Corn Fields(状压)
- Leetcode解题笔记 2.Add Two Numbers [Medium]
- 淘宝客运营推广技巧方法有哪些?
- 浅析python 中__name__ = '__main__' 的作用
- iOS 11 GM 披露更多有关新 iPhone Face ID、无线充电的消息
- 欢迎使用CSDN-markdown编辑器
- MVC模式和MVP模式