环境说明
1、使用IDEA作为开发工具
2、hadoop版本为CDH5.15.2
新建项目
略
为项目添加JAR包
使用maven管理项目,添加如下pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>hdfs</artifactId>
<version>0.10.0.RELEASE</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<hadoop.version>2.6.0-cdh5.15.2</hadoop.version>
</properties>
<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
编写测试程序
新建java程序
编写代码,检测文件/doc/text.txt是否存在
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class HDFSFileIfExist {
public static void main(String[] args) {
try {
String fileName = "/doc/test.txt";
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://172.22.2.1:8020");
conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
FileSystem fs = FileSystem.get(conf);
if (fs.exists(new Path(fileName))) {
System.out.println("文件存在");
} else {
System.out.println("文件不存在");
}
}catch (Exception e){
e.printStackTrace();
}
}
}
应用程序部署
IDEA构建工件
配置构建
执行构建
将jar包复制到hadoop平台执行
java -jar hdfs.jar