zoukankan      html  css  js  c++  java
  • 从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
  • 相关阅读:
    CSS里面position:relative与position:absolute 区别
    JSP页面规格化
    iframe自适应高度
    getElementByTagName的使用
    div左右居中
    @注解与普通web.xml的关系
    显示几秒内跳转
    js如何获取某id的子标签
    common upload乱码
    预览上传图片
  • 原文地址:https://www.cnblogs.com/MiraculousB/p/13848710.html
Copyright © 2011-2022 走看看