package com.clq.hdfs;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
public class FileCopyWithProgress {
public static void main(String[] args) throws IOException {
String localSrc = args[0];
String dst = args[1];
InputStream in = new BufferedInputStream(new FileInputStream(localSrc));
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(dst), conf);
FSDataOutputStream out = fs.create(new Path(dst), new Progressable() {
@Override
public void progress() {
System.out.print("."); // 每傳輸一塊打印一個點
}
});
IOUtils.copyBytes(in, out, conf, true); // 最後參數 true 表示關閉流
}
}
# 打成 jar:mapreducer.jar
hadoop jar mapreducer.jar /local/path/file.jar hdfs://ubuntu:9000/target/path/
Exception: Cannot create /out06; already exists as a directory
目標路徑在 HDFS 已存在且為目錄,換一個不存在的檔案路徑即可。
幾十行代碼即可實現帶進度的 HDFS 上傳,適合遷移大檔案或離線導數據場景。
出處:bianceng.cn