码迷,mamicode.com
首页 > Windows程序 > 详细

Hadoop-2.6.0上调用C的API实现类似云盘的功能

时间:2015-08-16 18:22:28      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:centos   hadoop   hdfs   c   api   

Hadoop-2.6.0上调用C的API实现类似云盘的功能(上传,下载,删除,重命名)

测试系统:CentOS6.6, hadoop-2.6.0

本次测试是调用hadoop下的C的API来访问HDFS实现类似云盘的上传,下载,删除,重命名的功能,其它功能还请有意者自行添加,废话少说,开始进入正题。

首先我们要能在hadoop-2.6.0上的C的API访问HDFS

详情可以访问:http://blog.csdn.net/u013930856/article/details/47660937

下面开始我们的云盘功能:

首先我们在主函数中连接到我们的hadoop服务器,并创建一个用户自己的文件夹

int main(int argc, char **argv) 
{
char CreatDirName[30]; /*创建文件夹和路径*/
char DirNamePath[50];
int Create;

hdfsFS fs = hdfsConnect("10.25.100.130", 9000);     //连接到hadoop服务器

printf("请输入你想创建的文件夹和路径:\n");
scanf("%s", CreatDirName);


Create = hdfsCreateDirectory(fs, CreatDirName);

printf("Create = %d\n", Create);
if(Create == -1)
{
printf("创建失败!\n");
exit(1);
}

while(1)
{
int num;

HdfsChoseMenu_Function();
scanf("%d", &num);
switch(num)
{
case 1:HdfsSendFile_Function(fs, CreatDirName);     //Hdfs上传文件Function
break;
case 2:HdfsDownFile_Function(fs, CreatDirName);      //下载文件Function
break;
case 3:HdfsDelete_Function(fs); //HdfsDelete_Function
break;
case 4:HdfsRename_Function(fs); //HdfsRename_Function
break;
case 0:HdfsQuit_Function(fs);
break;
default:printf("PLease input Error!!!\n");
}
}
}

上传文件到服务器:

void HdfsSendFile_Function(hdfsFS fs, char CreatDirName[])     //Hdfs上传文件Function
{
char SendFileName[30]; //FileName
char SendFilePath[50]; //FilePath
char buffer[LENGTH]; //BufferFile

printf("请输入要上传的文件名: ");
scanf("%s", SendFileName);
sprintf(SendFilePath, "%s/%s", CreatDirName, SendFileName);
hdfsFile OpenFileName = hdfsOpenFile(fs, SendFilePath, O_WRONLY|O_CREAT, 0, 0, 0);


FILE *fp = fopen(SendFileName, "r");   


if(NULL == fp)      
{        
printf("File:%s Not Found\n", SendFileName);      
}    
else
{
bzero(buffer, LENGTH);
tSize length = 0;
while((length = fread(buffer, sizeof(char), LENGTH, fp)) > 0)        

printf("length = %d\n", length);
tSize num_written_bytes = hdfsWrite(fs, OpenFileName, buffer, length);
printf("num_written_bytes = %d\n", num_written_bytes);
if (hdfsFlush(fs, OpenFileName)) 
{
fprintf(stderr, "Failed to ‘flush‘ %s\n", SendFilePath);
exit(-1);
}
bzero(buffer, LENGTH);
}
fclose(fp);  
hdfsCloseFile(fs, OpenFileName);
printf("\n>>>上传文件成功!!!\n\n");
}
}

下载文件:

void HdfsDownFile_Function(hdfsFS fs, char CreatDirName[])      //下载文件Function
{
char DownFileName[30]; //DownFileName
char DownFilePath[50]; //DownFilePath
char buffer[LENGTH]; //BufferFile

printf("请输入要下载的文件名: ");
scanf("%s", DownFileName);

sprintf(DownFilePath, "%s/%s", CreatDirName, DownFileName);
hdfsFile DownOpenFile = hdfsOpenFile(fs, DownFilePath, O_RDONLY, 0, 0, 0);
if(NULL == DownOpenFile)
{
printf("打开文件失败!\n");
exit(1);
}
else
{
FILE *fp = fopen(DownFileName, "w");    
if(NULL == fp)    
{      
printf("File:\t%s Can Not Open To Write\n", DownFileName);      
exit(1);    
}  
else
{
tSize D_length = 0;
while((D_length = hdfsRead(fs, DownOpenFile, buffer, LENGTH)) > 0)
{
printf("D_length = %d\n", D_length);


if(fwrite(buffer, sizeof(char), D_length, fp) < D_length)      
{        
printf("File:\t%s Write Failed\n", DownFileName);        
break;      
}   
bzero(buffer, LENGTH);
}//sleep(1);
fclose(fp);  
hdfsCloseFile(fs, DownOpenFile);
printf("\n>>>下载文件成功!!!\n\n");
}
}
}

删除文件:

void HdfsDelete_Function(hdfsFS fs)       //HdfsDelete_Function
{
int num_Delete;
char delete_HdfsFilePath[50];

printf("请输入你要删除的文件的名字和路径: ");
scanf("%s", delete_HdfsFilePath);
num_Delete = hdfsDelete(fs, delete_HdfsFilePath, 0);
printf("num_Delete = %d\n", num_Delete);

}

文件重命名:

void HdfsRename_Function(hdfsFS fs) //HdfsRename_Function
{
int num_Rename;
char HdfsFilePath[30] = {0};
char oldHdfsFileName[30] = {0};
char newHdfsFileName[30] = {0};
char oldHdfsFilePath[50] = {0};
char newHdfsFilePath[50] = {0};

printf("请输入要更改的文件路径和文件名: ");  //中间用空格隔开  example:/xiaodai 1.jpg
scanf("%s%s", HdfsFilePath, oldHdfsFileName);
printf("请输入更改后的文件名: ");
scanf("%s", newHdfsFileName);

sprintf(oldHdfsFilePath, "%s/%s", HdfsFilePath, oldHdfsFileName);
sprintf(newHdfsFilePath, "%s/%s", HdfsFilePath, newHdfsFileName);

num_Rename = hdfsRename(fs, oldHdfsFilePath, newHdfsFilePath);
printf("num_Rename = %d\n", num_Rename);
}

这只是简单的实现其功能,如果想继续添加其更多更能,还请开发者继续努力

这只是实现其功能的函数核心代码,其完整代码和操作文档详见:

http://download.csdn.net/detail/u013930856/9012061

版权声明:本文为博主原创文章,未经博主允许不得转载。

Hadoop-2.6.0上调用C的API实现类似云盘的功能

标签:centos   hadoop   hdfs   c   api   

原文地址:http://blog.csdn.net/u013930856/article/details/47703805

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!