连接hdfs
根据hdfs的版本引用相关的hdfs依赖
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.6.5</version>
</dependency>
将hdfs中的core-site.xml
和hdfs-site.xml
放进项目的resources目录里
简单编码测试下
public class Connection {
public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
Configuration configuration=new Configuration();
System.setProperty("HADOOP_USER_NAME", "root");
FileSystem fs = FileSystem.get(configuration);
System.out.println(fs.getHomeDirectory());
//拿到FileSystem后可以简单操作下hdfs,若是运行成功则代表连接成功!
}
}