1、Code -> GitHub
https://github.com/liufengji/hadoop_hdfs.git
2、Code
@Test
public void getFileToHDFS() throws Exception{
// 1 创建配置信息对象
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://node1:9000"),configuration, "victor");
// 2 读取文件路径
String filename = "hdfs://node1:9000/user/victor/hello1.txt";
// 3 创建读取path
Path readPath = new Path(filename);
// 4 创建输入流
FSDataInputStream inStream = fs.open(readPath);
// 5 流对接输出到控制台
try{
IOUtils.copyBytes(inStream, System.out, 4096, false);
}catch(Exception e){
e.printStackTrace();
}finally{
IOUtils.closeStream(inStream);
}
}