1、将hadoop集群中任一节点下的core-site.xml和hdfs-site.xml添加到IDEA工程的resources目录下
2、修改本机hosts文件:C:\Windows\System32\drivers\etc\hosts,设置hadoop域名和ip地址的映射。
3、pom.xml文件添加如下依赖:hadoop-common、hadoop-hdfs、hadoop-mapreduce-client-core
4、编写测试代码:
- import java.net.URI;
- 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 org.apache.hadoop.io.IOUtils;
- public class Test {
- public static void main(String[] args) throws Exception {
- String uri="hdfs://IP/test/test.txt";
- Configuration configuration=new Configuration();
- FileSystem fileSystem=FileSystem.get(URI.create(uri), configuration);
- FSDataInputStream in=null;
- in=fileSystem.open(new Path(uri));
- IOUtils.copyBytes(in, System.out, 4096, false);
- IOUtils.closeStream(in);
- }
- }