import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class Copyfile2hdfs {
public static void main(String[] args) throws IOException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path src = new Path("D:/demo.txt");
Path dst = new Path("/usr");
fs.copyFromLocalFile(src, dst);
System.err.println("文件上传成功至"+conf.get("fs.default.name"));
//列出服务器上目前拥有的文件
FileStatus[] fileStatus = fs.listStatus(dst);
for(FileStatus f:fileStatus){
System.err.println(f.getPath());
}
}
}
结果:
文件上传成功至hdfs://192.168.1.109:9000/
hdfs://192.168.1.109:9000/usr/demo.txt
hdfs://192.168.1.109:9000/usr/hadoop