从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名。

 1 import org.apache.hadoop.conf.Configuration;
 2 import org.apache.hadoop.fs.*;
 3 import org.apache.hadoop.fs.FileSystem;
 4 import java.io.*;
 5 
 6 public class B_DownLoadFile {
 7     public static void copyToLocal(FileSystem fs, Path p_LocalPath,
 8             Path p_RemotePath, String s_LocalPath, String s_RemotePath) {
 9         try {
10             File f = new File(s_LocalPath);
11             Integer i = Integer.valueOf(0);
12             while (f.exists()) {
13                 i++;
14                 Integer lasti=Integer.valueOf(i-1);
15                 String[] split_path = s_LocalPath.split("/");
16                 String[] filename = split_path[4].split("\\.");
17 
18                 if(i>1){
19                     filename[0]=filename[0].replace("("+lasti+")", "");
20                     System.out.println(filename[0] + "("
21                             + lasti.toString() + ")" +"."+ filename[1] + " existed.");
22                 }
23                 else
24                     System.out.println(filename[0] +"."+ filename[1] + " existed.");
25                 s_LocalPath = "/"+split_path[1] + "/" + split_path[2] + "/"
26                         + split_path[3] + "/" +filename[0] + "("
27                         + i.toString() + ")" +"."+ filename[1];
28                 f = new File(s_LocalPath);
29             }
30             System.out.println("Download to " + s_LocalPath);
31             p_LocalPath = new Path(s_LocalPath);
32             fs.copyToLocalFile(p_RemotePath, p_LocalPath);
33         } catch (IOException e) {
34             e.printStackTrace();
35         }
36     }
37 
38     public static void main(String[] args) {
39         Var_init var = new Var_init();
40         try {
41             B_DownLoadFile.copyToLocal(var.fs, var.p_localFilePath,
42                     var.p_remoteFilePath, var.s_localFilePath,
43                     var.s_remoteFilePath);
44         } catch (Exception e) {
45             e.printStackTrace();
46         }
47     }
48 }
View Code
posted @ 2020-10-20 20:29  Miraculous_B  阅读(1426)  评论(0编辑  收藏  举报