hadoop与hdfs需要自己安装,确保文件存在于hdfs
只有一个main方法
Test.java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Test {
public static void main(String[] args) throws IOException {
//创建configuration对象
Configuration conf = new Configuration();
//配置在etc/hadoop/core-site.xml fs.defaultFS
conf.set("fs.defaultFS","hdfs://192.168.163.132:8020");
//创建FileSystem对象
//查看hdfs集群服务器/user/passwd.txt的内容
FileSystem fs = FileSystem.get(conf);
FSDataInputStream is = fs.open(new Path("hdfs://192.168.163.132:8020/user/passwd.txt"));
File file = new File("a.txt");
System.out.println(file.getAbsolutePath());
OutputStream os = new FileOutputStream(file);
byte[] buff = new byte[1024];
int length = 0;
while ((length = is.read(buff)) != -1) {
System.out.println(new String(buff, 0, length));
os.write(buff, 0, length);
os.flush();
}
System.out.println(fs.getClass().getName());
}
}