千家信息网

hdfs文件操作操作的示例分析

发表于:2025-12-01 作者:千家信息网编辑
千家信息网最后更新 2025年12月01日,这篇文章主要为大家展示了"hdfs文件操作操作的示例分析",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"hdfs文件操作操作的示例分析"这篇文章吧。impo
千家信息网最后更新 2025年12月01日hdfs文件操作操作的示例分析

这篇文章主要为大家展示了"hdfs文件操作操作的示例分析",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"hdfs文件操作操作的示例分析"这篇文章吧。

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import java.io.File;
import java.io.IOException;
/**hdfs文件操作操作示例,包括上传文件到HDFS上、从HDFS上下载文件和删除HDFS上的文件*/
public class HadoopFile {
private Configuration conf =null;
public HadoopFile(){
conf =new Configuration();
conf.addResource(new Path("/hadoop/etc/hadoop/core-site.xml"));
}
public HadoopFile(Configuration conf){
this.conf =conf;
}
public boolean sendFile(String path,String localfile){
File file=new File(localfile);
if (!file.isFile()) {
System.out.println(file.getName());
return false;
}
try {
FileSystem localFS =FileSystem.getLocal(conf);
FileSystem hadoopFS =FileSystem.get(conf);
Path hadPath=new Path(path);
FSDataOutputStream fsOut=hadoopFS.create(new Path(path+"/"+file.getName()));
FSDataInputStream fsIn=localFS.open(new Path(localfile));
byte[] buf =new byte[1024];
int readbytes=0;
while ((readbytes=fsIn.read(buf))>0){
fsOut.write(buf,0,readbytes);
}
fsIn.close();
fsOut.close();
FileStatus[] hadfiles= hadoopFS.listStatus(hadPath);
for(FileStatus fs :hadfiles){
System.out.println(fs.toString());
}
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public boolean delFile(String hadfile){
try {
FileSystem hadoopFS =FileSystem.get(conf);
Path hadPath=new Path(hadfile);
Path p=hadPath.getParent();
boolean rtnval= hadoopFS.delete(hadPath, true);
FileStatus[] hadfiles= hadoopFS.listStatus(p);
for(FileStatus fs :hadfiles){
System.out.println(fs.toString());
}
return rtnval;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}


public boolean downloadFile(String hadfile,String localPath){
try {
FileSystem localFS =FileSystem.getLocal(conf);
FileSystem hadoopFS =FileSystem.get(conf);
Path hadPath=new Path(hadfile);
FSDataOutputStream fsOut=localFS.create(new Path(localPath+"/"+hadPath.getName()));
FSDataInputStream fsIn=hadoopFS.open(hadPath);
byte[] buf =new byte[1024];
int readbytes=0;
while ((readbytes=fsIn.read(buf))>0){
fsOut.write(buf,0,readbytes);
}
fsIn.close();
fsOut.close();
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}

以上是"hdfs文件操作操作的示例分析"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

文件 示例 分析 内容 篇文章 学习 帮助 上下 易懂 更多 条理 知识 编带 行业 资讯 资讯频道 频道 研究 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 pg数据库怎么右补空格 2012系统管理域服务器 清游网络技术有限公司 我的世界电脑版没有账号怎么玩服务器 成都学软件开发的职高排名 西城区网络技术信息哪个好 运营商网络技术论坛 咸阳开展网络安全宣传周 软件开发需要怎么配置电脑 2018网络安全测试答案 专注食品药品监管软件开发 黄色片下载软件开发 计算机网络技术发展的利大于弊 数据库技术数据挖掘 网络安全带来的经济损失 手机微信支付找不到服务器怎么办 大河报招聘软件开发 广东华为服务器维修多少钱 ever数据库怎么样 银川系统软件开发定制多少钱 个人信息管理网站建设数据库 计算机网络技术专业的理解 备份数据库覆盖了如何找回 我的世界怎么上传建筑到服务器 龙华区软件开发培训 测试网站后台数据库的信息 小米手机远程管理服务器 基因geo tcga数据库 星网络安全 洛阳三联网络技术
0