苏泽湛 发表于 2017-12-18 09:51:22

Java API操作HA方式下的Hadoop

/**  * 上传文件到HDFS上去
*/  private static void uploadToHdfs() throws IOException {
  String localSrc = "E:\\test\\article01.txt";
  String dst = "/user/test/article04.txt";
  FileSystem fs = FileSystem.get(URI.create(HADOOP_URL), conf);
  long start = new Date().getTime();
  

  /* InputStream in = new FileInputStream(localSrc);
  InputStreamReader isr = new InputStreamReader(in, "GBK");
  OutputStream out = fs.create(new Path(HADOOP_URL+dst), true);
  IOUtils.copy(isr, out, "UTF8");*/
  //该方法更快
  FSDataOutputStream outputStream=fs.create(new Path(dst));
  String fileContent = FileUtils.readFileToString(new File(localSrc), "GBK");
  outputStream.write(fileContent.getBytes());
  outputStream.close();
  long end = new Date().getTime();
  System.out.println("use:"+(end-start));
  }
页: [1]
查看完整版本: Java API操作HA方式下的Hadoop