zoukankan      html  css  js  c++  java
  • 向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,由用户指定是追加到原有文件末尾还是覆盖原有的文件

     1 import java.io.FileInputStream;
     2 import java.io.IOException;
     3 import java.util.Scanner;
     4 
     5 import org.apache.hadoop.conf.Configuration;
     6 import org.apache.hadoop.fs.FSDataOutputStream;
     7 import org.apache.hadoop.fs.FileSystem;
     8 import org.apache.hadoop.fs.Path;
     9 
    10 public class A_UpdatedFile {
    11 
    12 
    13     public static void appendToFile(FileSystem fs, String localFilePath,
    14             String remoteFilePath) {
    15         Path remotePath = new Path(remoteFilePath);
    16         try {
    17             FileInputStream in = new FileInputStream(localFilePath);
    18             FSDataOutputStream out = fs.append(remotePath);
    19             byte[] data = new byte[1024];
    20             int read = -1;
    21             while ((read = in.read(data)) > 0) {
    22                 out.write(data, 0, read);
    23             }
    24             out.close();
    25         } catch (IOException e) {
    26             e.printStackTrace();
    27         }
    28     }
    29 
    30     public static void main(String[] args) {
    31         Var_init var = new Var_init();
    32         try {
    33             boolean fileExists = var.fs.exists(var.p_remoteFilePath);
    34             if (fileExists) {
    35                 System.out.println(var.s_remoteFilePath + " 已存在.");
    36             } else {
    37                 System.out.println(var.s_remoteFilePath + " 不存在.");
    38             }
    39             if (!fileExists) {
    40                 var.fs.copyFromLocalFile(false, true, var.p_localFilePath, var.p_remoteFilePath);
    41                 System.out.println(var.s_localFilePath + " 已上传至 "
    42                         + var.s_remoteFilePath);
    43             } else {
    44                 Scanner choose = new Scanner(System.in);
    45                 System.out.println("input c to cover,input a to append");
    46                 String str = choose.next();
    47                 if(str.equals("c"))
    48                 {
    49                     var.fs.copyFromLocalFile(false, true, var.p_localFilePath, var.p_remoteFilePath);
    50                     System.out.println("cover successfully");
    51                 }
    52                 else if(str.equals("a"))
    53                 {
    54                     A_UpdatedFile.appendToFile(var.fs, var.s_localFilePath, var.s_remoteFilePath);
    55                     System.out.println("append successfully");
    56                 }
    57                 else
    58                 {
    59                     System.out.println("plz input right common");
    60                 }
    61             }
    62         } catch (Exception e) {
    63             e.printStackTrace();
    64         }
    65     }
    66 }
    View Code
  • 相关阅读:
    如何把textfield或者textview中长按出现的(全选,复制,粘贴)显示成中文
    免费真机调试 -- Xcode7
    Android性能测试工具 Emmagee
    iOS 开发 入门:使用Ad Hoc 进行用户测试
    栈与队列的区别
    iOS中ASI和AFN的区别
    iOS开发之监测网络状态
    xcode设置项目图标玻璃镜效果
    isEqual,isEqualTostring,==三者的区别
    iphone匹配邮箱的正则表达式
  • 原文地址:https://www.cnblogs.com/MiraculousB/p/13848701.html
Copyright © 2011-2022 走看看