《小白兔到大黑牛》第十三篇maven中Hadoop中类的练习
pom.xml配置如下
[html] view plain copy<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>storage</artifactId> <groupId>platform.edu.storage</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>explorer-impl</artifactId> <name>${project.artifactId}</name> <packaging>jar</packaging> <description>Hadoop HDFS explorer实现</description> <dependencies> <!-- hadoop base before --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>2.2.0</version> </dependency> <!-- hadood base end --> </dependencies> HDFSServer.Java代码如下-----------------------------------------------------------
[java] view plain copypackage platform.edu.explorer.hdfs.server; import java.io.IOException; import java.net.URI; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.mapred.JobConf; /** * HDFS服务器 * * @author hjn * @version 1.0 2013-11-20 */ public class HDFSServer { private static Configuration configuration; private static FileSystem fileSystem; private static final String HDFS_URL = "hdfs://192.168.1.210:8020"; /** * HDFS服务器读取初始化 */ private static void init() { try { configuration= new JobConf(HDFSServer.class); fileSystem = FileSystem.get(URI.create(HDFS_URL), configuration); } catch (IOException e) { System.out.println("读取服务器失败"); e.printStackTrace(); } } public static FileSystem getFileSystem(){ if(fileSystem==null){ init(); } return fileSystem; } } -------------------------------------------------------------------------------------------------------------------------------------------------- HDFS.java代码如下 -------------------------- package platform.edu.explorer.hdfs; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import org.apache.commons.compress.utils.IOUtils; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import platform.edu.explorer.HadoopFileSystem; import platform.edu.explorer.hdfs.server.HDFSServer; /** * HDFS(分布式文件存储系统操作类). * * @author hjn * @version 1.0 2013-11-22 */ public class HDFS implements HadoopFileSystem { /** * 文件系统类 */ private FileSystem fileSystem; /** * 无参数构造方法 */ public HDFS() { init(); } /** * 初始化 */ private void init() { fileSystem = HDFSServer.getFileSystem(); } /** * 获取HDFS指定目录下文件状态列表 * * @param dirPath指定目录路径 * @return fileStatusList * @throws FileNotFoundException * @throws IOException */ public FileStatus[] getFileStatus(Path path) throws FileNotFoundException, IOException { FileStatus[] fileStatusList = fileSystem.listStatus(path); return fileStatusList; } /** * 获取指定目录列表路径 * * @param dirPath */ public List<String> dir(String dirPath) throws IOException { List<String> fileList = null; Path path = new Path(dirPath); if (fileSystem.exists(path)) { fileList = new ArrayList<String>(); FileStatus[] list = this.getFileStatus(path); for (FileStatus fileStatus : list) { fileList.add(fileStatus.getPath().toString()); } } else { System.out.println("目录不存在"); } return fileList; } /** * 获取文件 * * @param filePath * @return * @throws IOException */ public InputStream getFile(String filePath) throws IOException { Path path = new Path(filePath); return fileSystem.open(path); } /** * 更改HDSF文件名称 * * @param fileOldName * @param fileNewName * @return boolean:是否更名字成功 * @throws IOException */ public boolean rename(String src, String dst) throws IOException { Path srcPath = new Path(src); if (fileSystem.exists(srcPath)) { Path dstPath = new Path(dst); return fileSystem.rename(srcPath, dstPath); } System.out.println("原文件不存在"); return false; } /** * 创建HDFS目录 * * @param dir */ public boolean createDir(String dir) throws IOException { Path path = new Path(dir); if (fileSystem.exists(path)) { System.out.println("此目录已经存在不需要再创建"); return true; } return fileSystem.mkdirs(path); } /** * 上传本地文件到HDFS(注意是服务器本地硬盘,非客户端硬盘)) * * @return * * @throws IOException */ @Override public void uploadLocalFile(String localFileSrc, String HDFSFileDst) throws IOException { Path src = new Path(localFileSrc); Path dst = new Path(HDFSFileDst); fileSystem.copyFromLocalFile(src, dst); } /** * 批量上传本地文件到HDFS * @param localFileSrcs本地文件列表 * @param HDFSFileDst * @throws IOException */ public void uploadLocalFile(String[] localFileSrcs,String HDFSFileDst) throws IOException{ Path dstPath=new Path(HDFSFileDst); Path[] paths=new Path[localFileSrcs.length]; for (int i=0;i<localFileSrcs.length;i++) { paths[i]=new Path(localFileSrcs[i]); } fileSystem.copyFromLocalFile(false, false, paths,dstPath); } /** * 从HDFS下载文件到本地(注意是服务器本地硬盘,非浏览器客户端硬盘) * * @param HDFSFilePath * @param localFilePath * @throws IOException */ public void downFileToLocal(String HDFSFilePath, String localFilePath) throws IOException { Path dstPath = new Path(HDFSFilePath); FSDataInputStream in = fileSystem.open(dstPath); OutputStream out = new FileOutputStream(new File(localFilePath)); IOUtils.copy(in, out); } /** * HDFS文件是否存在 * * @param filePath * @return boolean:是否存在 * @throws IOException */ public boolean exists(String filePath) throws IOException { Path path = new Path(filePath); return fileSystem.exists(path); } /** * 根据路径删除文件或文件夹 * * @param filePath * @return * @throws IOException */ @SuppressWarnings("deprecation") public boolean deleteFile(String filePath) throws IOException { if (this.exists(filePath)) { Path path = new Path(filePath); fileSystem.delete(path); return true; } System.out.println("文件不存在"); return false; } /** * 剪切本地文件到HDFS(注意为服务器本地文件); * * @param src本地路径 * @param dst分布式存储路径 * @throws IOException */ public void moveFromLocalFile(String localSrc, String HDFSDst) throws IOException { Path srcPath = new Path(localSrc); Path dstPath = new Path(HDFSDst); fileSystem.moveFromLocalFile(srcPath, dstPath); } /** * HDFS文件之间的复制 * @param src源文件路径 * @param dst要复制后复制文件的路径 * @throws IOException */ public void copyHDFSFile(String src,String dst) throws IOException{ Path srcPath=new Path(src); Path dstPath=new Path(dst); InputStream in=fileSystem.open(srcPath); OutputStream out=fileSystem.create(dstPath); IOUtils.copy(in, out); } /** * HDFS中移动文件 * @param src源文件路径 * @param dst要移动后的路径 * @throws IOException */ @SuppressWarnings("deprecation") public void moveHDFSFile(String src,String dst) throws IOException{ Path srcPath=new Path(src); Path dstPath=new Path(dst); InputStream in=fileSystem.open(srcPath); OutputStream out=fileSystem.create(dstPath); IOUtils.copy(in, out); fileSystem.delete(srcPath); } /** * 剪切HDFS文件到本地 * @param HDFSSrc * @param localDst * @throws IOException */ public void moveToLocalFile(String HDFSSrc,String localDst) throws IOException{ Path srcPath = new Path(HDFSSrc); Path dstPath = new Path(localDst); fileSystem.moveToLocalFile(srcPath, dstPath); } /** * HDFS创建文件 * * @param in输入流 * @param dst分布式存储路径 * @throws IOException */ public void create(InputStream in, String dst) throws IOException { Path dstPath = new Path(dst); FSDataOutputStream out = fileSystem.create(dstPath); IOUtils.copy(in, out); } /** * 在HDFS创建文件 * * @param file * @param dst分布式存储路径 * @throws IOException */ public void create(File file, String dst) throws IOException { InputStream in = new FileInputStream(file); this.create(in, dst); } /** * 在HDFS创建文件 * * @param src本地文件路径 * @param dst分布式存储路径 * @throws IOException */ public void create(String src, String dst) throws IOException { File file = new File(src); this.create(file, dst); } /** * 获取FileSystem对象 * * @return */ public FileSystem getFileSystem() { return fileSystem; } /** * 关闭HDFS * * @throws IOException */ public void close() throws IOException { fileSystem.close(); } public boolean isDir(String src) throws IOException { Path path = new Path(src); return fileSystem.isDirectory(path); } public static void main(String[] args) throws IOException { HDFS hdfs = new HDFS(); // hdfs.uploadLocalFile("D:/picture", "/test"); // System. out.println(hdfs.dir("/")); // hdfs.create("D:/picture/mypicture/POP海报2590.jpg","/test/picture/mypicture/POP海报2590.jpg"); System. out.println(hdfs.dir("/test/picture/mypicture")); //hdfs.uploadLocalFile(new String[]{"E:/input","E:/output"}, "/"); //hdfs.rename("/input", "/debug_in"); System. out.println(hdfs.dir("/debug_out")); //hdfs.deleteFile("/output"); // hdfs.moveFromLocalFile("E:/test.jpg", "/test/picture/POP海报2590.jpg"); /* * System.out.println(hdfs.dir("/test")); * System.out.println(hdfs.exists("/test/picture/mypicture")); // * hdfs.delete("/test/picture/mypicture"); * System.out.println(hdfs.dir("/test/picture")); */ hdfs.close(); } }
《小白兔到大黑牛》第十三篇maven中Hadoop中类的练习相关推荐
- 《小白兔到大黑牛》第十四篇Hadoop中五个进程作用
问题导读: 1.job的本质是什么? 2.任务的本质是什么? 3.文件系统的Namespace由谁来管理,Namespace的作用是什么? 4.Namespace 镜像文件(Namespace ima ...
- 《小白兔到大黑牛》第十一篇yum命令的总结
yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器.基於RPM包管理,能够从指定的服务器自动下载RP ...
- Shell从小白牛到大黑牛你只差这一步
目录 1.题目: 1.2 代码编写 1.3 测试 2.题目: 2.2 代码编写 2.3 测试 3.题目: 3.1 代码编写 3.2 测试 4.题目: 4.1 编写代码 4.2 测试 4.2.1 切换用 ...
- 第十三篇 人生中的第一桶金
不知不觉,时间过的飞快,又到了星期五了,那么离星期六和星期天 的日子还远吗...... 昨晚的聚餐又被同一个借口给取消了,哎,今天是无论如何 都要他们请我吃大餐 呵呵.... 好开心 现在来说一下 ...
- 第十三篇 VUE中使用头像插件 multiavatar,超级有趣的头像生成插件,提升开发的乐趣
最近在开发中,看到一个很有趣的插件-multiavatar,查看了一下官方,功能很强大,能够依据输入的内容,随机进行生成一个头像,觉得特别的有乐趣,特在这分享一下. 官方地址:multiavatar ...
- 运维工具大宝典之商用软件篇
在前一篇<运维工具大宝典之开源工具篇>中,云智慧对比分析了国内流行开源运维监控软件的优劣.在文末我们提到了开源产品在服务和安全等方面的短板,而正因为有这些问题,所以国内企业,特别是中大型行 ...
- 《孙子兵法》十三篇注译(15--结束语)
结束语 (读<孙子兵法>,悟管理.营销之道) 中国自古为思想精英荟萃之地,也是的兵学昌盛之国,素有"兵法之国"之称.产生于2500年前的不朽名著<孙子兵法&g ...
- python学习[第十三篇] 条件和循环
python学习[第十三篇] 条件和循环 if语句 单一if 语句 if语句有三个部分构成,关键字if本身,判断结果真假的条件表达式,以及表达式为真或非0是执行的代码 if expression: e ...
- CCNA-第十三篇-NAT-上
CCNA-第十三篇-NAT-上 NAT- netword address translation 网络地址转换 NAT不仅仅是用于共享地址上网,NAT是一个很大的东西 核心思想是转换地址,以及端口号 ...
最新文章
- 使用JDK自带jvisualvm监控tomcat
- 在Windows环境下搭建Android开发环境
- 准备您的虚拟桌面--WinXp模板
- 每日一皮:唯一的编码思想!
- Android Fragment 监听返回键
- 145_Power BI Report Server自定义Form登录
- CDN的安全防护功能
- 制衣软件测试自学,服装检验作业指导书.doc
- MySQL基础知识思维导图大纲
- matlab专业代做hslogic,matlab代做hslogic
- 设置excel中数据有效性不被复制黏贴破坏
- c语言泊松分酒编码,泊松分酒原理
- AIML标签中srai不起作用的原因
- 小米6手机投屏到wins 8.1电脑上的软件——scrcpy的安装与使用教程
- 一份不太简短的LaTeX2e介绍最新版地址2019 The Not So Short Introduction To LATEX (Chinese Edition)
- HTML 语法规范及其基本结构标签
- 药品名自动归类机器人
- Excel 2010 如何将筛选后的数据复制粘贴到另一个工作表筛选后的表格里
- 远程管理软件(xshell)介绍和系统连接
- 数据分析行业出路太窄?那是你根本不懂什么是数据分析
热门文章
- Python--递归初步
- 微型计算机的alu部件包含在什么中,【单选题】微型计算机的ALU部件包括在( )之中。A. 存储器 B. I/O接口 C. O/I接口 D. CPU...
- Nginx反向代理服务器高速缓存数据结构的部分实现
- OSI的7层模型和TCP/IP四层模型
- OpenCV对矩阵进行padding操作
- 【可达性分析中的增量更新和原始快照】
- MATLAB之多项式插值
- 联发科(MTK)MT6765 核心板 安卓主板
- 如何删除outlook中的重复邮件
- vue 2.0使用tinymce-vue富文本