编写Java代码对HDFS进行增删改查操作代码实例
本文实例为大家分享了Java代码对HDFS进行增删改查操作的具体代码,供大家参考,具体内容如下
importjava.io.File; importjava.io.FileOutputStream; importjava.io.IOException; importjava.net.URI; importorg.apache.commons.compress.utils.IOUtils; importorg.apache.hadoop.conf.Configuration; importorg.apache.hadoop.fs.BlockLocation; importorg.apache.hadoop.fs.FSDataInputStream; importorg.apache.hadoop.fs.FSDataOutputStream; importorg.apache.hadoop.fs.FileStatus; importorg.apache.hadoop.fs.FileSystem; importorg.apache.hadoop.fs.Path; publicclassFileOpreation{ publicstaticvoidmain(String[]args)throwsIOException{ //CreateFile(); //DeleteFile(); //CopyFileToHDFS(); //MkDirs(); //DelDirs(); ListDirectory(); DownLoad(); } publicstaticvoidCreateFile()throwsIOException{ Stringuri="hdfs://Alvis:9000"; Configurationconfiguration=newConfiguration(); FileSystemfSystem=FileSystem.get(URI.create(uri),configuration); byte[]file_content_buff="hellohadoopworld,testwritefile!\n".getBytes(); Pathdfs=newPath("/home/test.txt"); FSDataOutputStreamoutputStream=fSystem.create(dfs); outputStream.write(file_content_buff.length); } publicFileOpreation(){ //TODOAuto-generatedconstructorstub }publicstaticvoidDeleteFile()throwsIOException{ Stringuri="hdfs://Alvis:9000"; Configurationconfiguration=newConfiguration(); FileSystemfSystem=FileSystem.get(URI.create(uri),configuration); Pathdeletf=newPath("/home/test.txt"); booleandelResult=fSystem.delete(deletf,true); System.out.println(delResult==true?"删除成功":"删除失败"); } publicstaticvoidCopyFileToHDFS()throwsIOException{ Stringuri="hdfs://Alvis:9000"; Configurationconfiguration=newConfiguration(); FileSystemfSystem=FileSystem.get(URI.create(uri),configuration); Pathsrc=newPath("E:\\SerializationTest\\APITest.txt"); Pathdest_src=newPath("/home"); fSystem.copyFromLocalFile(src,dest_src); } publicstaticvoidMkDirs()throwsIOException{ Stringuri="hdfs://Alvis:9000"; Configurationconfiguration=newConfiguration(); FileSystemfSystem=FileSystem.get(URI.create(uri),configuration); Pathsrc=newPath("/Test"); fSystem.mkdirs(src); } publicstaticvoidDelDirs()throwsIOException{ Stringuri="hdfs://Alvis:9000"; Configurationconfiguration=newConfiguration(); FileSystemfSystem=FileSystem.get(URI.create(uri),configuration); Pathsrc=newPath("/Test"); fSystem.delete(src); } publicstaticvoidListDirectory()throwsIOException{ Stringuri="hdfs://Alvis:9000"; Configurationconfiguration=newConfiguration(); FileSystemfSystem=FileSystem.get(URI.create(uri),configuration); FileStatus[]fStatus=fSystem.listStatus(newPath("/output")); for(FileStatusstatus:fStatus) if(status.isFile()){ System.out.println("文件路径:"+status.getPath().toString()); System.out.println("文件路径getReplication:"+status.getReplication()); System.out.println("文件路径getBlockSize:"+status.getBlockSize()); BlockLocation[]blockLocations=fSystem.getFileBlockLocations(status,0,status.getBlockSize()); for(BlockLocationlocation:blockLocations){ System.out.println("主机名:"+location.getHosts()[0]); System.out.println("主机名:"+location.getNames()[0]); } } else{ System.out.println("directory:"+status.getPath().toString()); } } publicstaticvoidDownLoad()throwsIOException{ Configurationconfiguration=newConfiguration(); configuration.set("fs.defaultFS","hdfs://Alvis:9000"); FileSystemfSystem=FileSystem.get(configuration); FSDataInputStreaminputStream=fSystem.open(newPath("/input/wc.jar")); FileOutputStreamoutputStream=newFileOutputStream(newFile("E:\\LearnLife\\DownLoad\\wc.jar")); IOUtils.copy(inputStream,outputStream); System.out.println("下载成功!"); } }
思想:
一、定义虚拟机接口
二、先拿到HDFS远程调用接口对象Configuration
三、定义分布式文件系统FileSystem对象获取对象
四、给定路径
五、用FileSystem对象调用操作
以上所述是小编给大家介绍的Java代码对HDFS进行增删改查操作详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!