首页 > 代码库 > Hadoop-2.6.0上调用C的API实现相似云盘的功能
Hadoop-2.6.0上调用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
以下開始我们的云盘功能:
首先我们在主函数中连接到我们的hadoopserver,并创建一个用户自己的目录
int main(int argc, char **argv)
{
char CreatDirName[30];
/*创建目录和路径*/
char DirNamePath[50];
int Create;
hdfsFS fs = hdfsConnect("10.25.100.130", 9000); //连接到hadoopserver
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");
}
}
}
上传文件到server:
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实现相似云盘的功能