本文实例为大家分享了Java代码对HDFS进行增删改查操作的具体代码,供大家参考,具体内容如下
进口java.io.File; 进口java.io.FileOutputStream; 进口java.io.IOException; 进口java.net.URI; 进口org.apache.commons.compress.utils.IOUtils; 进口org.apache.hadoop.conf.Configuration; 进口org.apache.hadoop.fs.BlockLocation; 进口org.apache.hadoop.fs.FSDataInputStream; 进口org.apache.hadoop.fs.FSDataOutputStream; 进口org.apache.hadoop.fs.FileStatus; 进口org.apache.hadoop.fs.FileSystem; 进口org.apache.hadoop.fs.Path; 公开课FileOpreation { 公共静态void main (String [] args)抛出IOException {//它仅仅();//DeleteFile ();//CopyFileToHDFS ();//mkdir ();//DelDirs (); ListDirectory (); 下载(); } 公共静态空它仅仅()抛出IOException { 字符串uri=" hdfs://艾尔维:9000”; 配置配置=new配置(); 文件系统fSystem=FileSystem.get (URI.create (uri),配置); hadoop byte [] file_content_buff="你好世界,测试写文件! \ n”.getBytes (); 路径dfs=new路径(“/home/用法”); FSDataOutputStream outputStream=fSystem.create (dfs); outputStream.write (file_content_buff.length); } 公共FileOpreation () {//TODO自动生成构造函数存根 }公共静态孔隙DeleteFile()抛出IOException { 字符串uri=" hdfs://艾尔维:9000”; 配置配置=new配置(); 文件系统fSystem=FileSystem.get (URI.create (uri),配置); 路径deletf=new路径(“/home/用法”); 布尔delResult=fSystem.delete (deletf,真实); System.out.println (delResult==真正的# 63;“删除成功”:“删除失败”); } 公共静态孔隙CopyFileToHDFS()抛出IOException { 字符串uri=" hdfs://艾尔维:9000”; 配置配置=new配置(); 文件系统fSystem=FileSystem.get (URI.create (uri),配置); 路径src=https://www.yisu.com/zixun/new路径(“E: \ \ SerializationTest \ \ APITest.txt”); 路径dest_src=https://www.yisu.com/zixun/new路径(/home); fSystem。dest_src copyFromLocalFile (src); } 公共静态孔隙mkdir()抛出IOException { 字符串uri=" hdfs://艾尔维:9000”; 配置配置=new配置(); 文件系统fSystem=FileSystem.get (URI.create (uri),配置); 路径src=https://www.yisu.com/zixun/new路径(/测试); fSystem.mkdirs (src); } 公共静态孔隙DelDirs()抛出IOException { 字符串uri=" hdfs://艾尔维:9000”; 配置配置=new配置(); 文件系统fSystem=FileSystem.get (URI.create (uri),配置); 路径src=https://www.yisu.com/zixun/new路径(/测试); fSystem.delete (src); } 公共静态孔隙ListDirectory()抛出IOException { 字符串uri=" hdfs://艾尔维:9000”; 配置配置=new配置(); 文件系统fSystem=FileSystem.get (URI.create (uri),配置); FileStatus [] fStatus=fSystem。listStatus(新路径(“输出”)); (FileStatus状态:fStatus) 如果(status.isFile ()) { System.out.println(“文件路径:“+ status.getPath () .toString ()); system . out。println(“文件路径getReplication:“+ status.getReplication ()); system . out。println(“文件路径getBlockSize:“+ status.getBlockSize ()); BlockLocation [] blockLocations=fSystem。getFileBlockLocations(状态,0,status.getBlockSize ()); (BlockLocation地点:blockLocations) { System.out.println(“主机名:“+ location.getHosts () [0]); System.out.println(“主机名:“+ location.getNames () [0]); } } 其他{ System.out.println(“目录:“+ status.getPath () .toString ()); } } 公共静态无效下载()抛出IOException { 配置配置=new配置(); configuration.set (“fs.defaultFS”、“hdfs://艾尔维:9000”); 文件系统fSystem=FileSystem.get(配置); FSDataInputStream inputStream=fSystem。打开(新路径("/输入/wc.jar ")); FileOutputStream outputStream=new FileOutputStream(新文件(“E: \ \ LearnLife \ \ \ \ wc.jar下载")); IOUtils。复制(inputStream, outputStream); System.out.println(“下载成功!”); } } >之前思想:
一、定义虚拟机接口
二,先拿到HDFS远程调用接口对象配置
三、定义分布式文件系统文件系统对象获取对象
四,给定路径
五,用文件系统对象调用操作
以上所述是小编给大家介绍的Java代码对HDFS进行增删改查操作详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留的言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
编写Java代码对HDFS进行增删改查操作代码实例