1 //查看文件信息 2 //cathdfs(""); 3 //查看文件内容 4 //readfile(""); 5 //创建空一个文件 6 //createfile(""); 7 //创建一个文件并写入文件内容 8 //cufile(""); 9 //创建一个文件夹 10 //mkdirfile(""); 11 //重名称文件或文件夹 12 //renameFile(""); 13 //删除文件或文件夹 14 //deletefile(""); 15 //上传HDFS 16 //uploadfile("",""); 17 //下载到本地 18 //downloadfile("","");
1 //查询文件信息 2 public static void cathdfs(String path){ 3 //创建连接uri 4 String uri="hdfs://192.168.152.131:9000/"; 5 //实例化配置文件对象 6 Configuration con=new Configuration(); 7 FileSystem fileSystem=null; 8 FileStatus fileStatus=null; 9 try { 10 //创建文件系统对象,指定文件的操作 11 fileSystem=FileSystem.get(URI.create(uri),con); 12 //获取单个文件信息系 13 fileStatus=fileSystem.getFileStatus(new Path(path)); 14 System.out.print(fileStatus); 15 16 System.out.print("========================================分割线"); 17 /** 18 * 查询所有与文件信息 19 */ 20 FileStatus[] listStatus= fileSystem.listStatus(new Path(path)); 21 for(FileStatus staus:listStatus){ 22 System.out.println(staus); 23 } 24 } catch (IOException e) { 25 26 e.printStackTrace(); 27 }finally{ 28 try { 29 fileSystem.close(); 30 } catch (IOException e) { 31 e.printStackTrace(); 32 } 33 } 34 35 }
1 //查询文件内容 2 public static void readfile(String fileName){ 3 //第一步创建url链接 4 String uri="hdfs://192.168.152.131:9000/"; 5 //第二步实例化配置文件对象 6 Configuration config=new Configuration(); 7 //第三步创建文件系统对象,实现对文件到的操作,创建出入流对象 8 FileSystem fileSystem=null; 9 FSDataInputStream input=null; 10 BufferedReader br=null; 11 try { 12 //第四步 使用 13 fileSystem=FileSystem.get(URI.create(uri),config); 14 input=fileSystem.open(new Path(fileName)); 15 br=new BufferedReader(new InputStreamReader(input)); 16 String line=""; 17 while((line=br.readLine())!=null){ 18 System.out.print(line); 19 } 20 } catch (IOException e) { 21 e.printStackTrace(); 22 }finally{ 23 try { 24 //第五步关闭资源 25 br.close(); 26 input.close(); 27 fileSystem.close(); 28 } catch (IOException e) { 29 30 e.printStackTrace(); 31 } 32 } 33 34 }
1 //创建一个空的文件 2 public static void createfile(String filename){ 3 //一 4 String uri="hdfs://192.168.152.131:9000/"; 5 //二 6 Configuration con=new Configuration(); 7 //三 8 FileSystem file=null; 9 try { 10 file=FileSystem.get(URI.create(uri),con); 11 boolean fase=file.createNewFile(new Path(filename)); 12 if(fase){ 13 System.out.print("成功"); 14 }else{ 15 System.out.print("失败"); 16 } 17 } catch (IOException e) { 18 19 e.printStackTrace(); 20 }finally{ 21 try { 22 //四 23 file.close(); 24 } catch (IOException e) { 25 e.printStackTrace(); 26 } 27 } 28 }
1 //创建一个空文件并写入文件 2 public static void cufile(String filename){ 3 //1 4 String uri="hdfs://192.168.152.131:9000/"; 5 //2 6 Configuration con=new Configuration(); 7 //3 8 FileSystem fiel=null; 9 FSDataOutputStream fs=null; 10 try { 11 fiel=FileSystem.get(URI.create(uri),con); 12 if(fiel.exists(new Path(filename))){ 13 //文件已经存在 14 System.out.println("文件已经存在"); 15 }else{ 16 //文件不存在 17 fs= fiel.create(new Path(filename)); 18 //文件内容 19 String str="呵呵"; 20 fs.write(str.getBytes()); 21 fs.flush(); 22 } 23 } catch (IOException e) { 24 25 e.printStackTrace(); 26 }finally{ 27 try { 28 //4 29 fs.close(); 30 fiel.close(); 31 } catch (IOException e) { 32 33 e.printStackTrace(); 34 } 35 } 36 }
1 //创建一个文件夹 2 public static void mkdirfile(String filename){ 3 //1 4 String uri="hdfs://192.168.152.131:9000/"; 5 //2 6 Configuration con=new Configuration(); 7 //3 8 FileSystem file=null; 9 try { 10 file=FileSystem.get(URI.create(uri),con); 11 boolean mkdirs = file.mkdirs(new Path(filename)); 12 if(mkdirs){ 13 System.out.print("成功"); 14 }else{ 15 System.out.print("失败"); 16 } 17 } catch (IOException e) { 18 19 e.printStackTrace(); 20 }finally{ 21 try { 22 //4 23 file.close(); 24 } catch (IOException e) { 25 26 e.printStackTrace(); 27 } 28 } 29 } 30
1 //重命名文件或文件夹 2 public static void renameFile(String oldname,String filename){ 3 //1 4 String uri="hdfs://192.168.152.131:9000/"; 5 //2 6 Configuration con=new Configuration(); 7 //3 8 FileSystem file=null; 9 try { 10 file=FileSystem.get(URI.create(uri),con); 11 //1.文件或文件夹原名称 2.修改文件或文件夹名称 12 boolean rename = file.rename(new Path(oldname), new Path(filename)); 13 if(rename){ 14 System.out.print("成功"); 15 }else{ 16 System.out.print("失败"); 17 } 18 } catch (IOException e) { 19 20 e.printStackTrace(); 21 }finally{ 22 try { 23 file.close(); 24 } catch (IOException e) { 25 26 e.printStackTrace(); 27 } 28 } 29 30 } 31
1 //删除文件或文件夹 2 public static void deletefile(String filename){ 3 //1 4 String uri="hdfs://192.168.152.131:9000/"; 5 //2 6 Configuration con=new Configuration(); 7 //3 8 FileSystem file=null; 9 try { 10 file=FileSystem.get(URI.create(uri),con); 11 boolean delete = file.delete(new Path(filename)); 12 if(delete){ 13 System.out.print("成功"); 14 }else{ 15 System.out.print("失败"); 16 } 17 } catch (IOException e) { 18 19 e.printStackTrace(); 20 }finally{ 21 try { 22 file.close(); 23 } catch (IOException e) { 24 25 e.printStackTrace(); 26 } 27 } 28 }
1 //本地文件上传到HDFS 2 public static void uploadfile(String loadfile,String desfile){ 3 //1 4 String uri="hdfs://192.168.152.131:9000/"; 5 //2 6 Configuration con=new Configuration(); 7 //3 8 FileSystem file=null; 9 try { 10 file=FileSystem.get(URI.create(uri),con); 11 //上传中 12 file.copyFromLocalFile(new Path(loadfile), new Path(desfile)); 13 } catch (IOException e) { 14 15 e.printStackTrace(); 16 }finally{ 17 try { 18 file.close(); 19 } catch (IOException e) { 20 21 e.printStackTrace(); 22 } 23 } 24 System.out.println("上传中---------"); 25 }
1 //HDFS下载文件到本地 2 public static void downloadfile(String desfile,String loadfile){ 3 //1 4 String uri=""; 5 //2 6 Configuration con=new Configuration(); 7 //3 8 FileSystem file=null; 9 try { 10 file=FileSystem.get(URI.create(uri),con); 11 //下载中 12 file.copyToLocalFile(new Path(desfile), new Path(loadfile)); 13 } catch (IOException e) { 14 15 e.printStackTrace(); 16 }finally{ 17 try { 18 file.close(); 19 } catch (IOException e) { 20 21 e.printStackTrace(); 22 } 23 } 24 System.out.println("下载中-----------"); 25 }
注:开启 hadoop 集群
工具:eclipse
工程:Maven