在IDEA中创建一个Maven项目 用来编写HDFS的API
导入依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<!-- hadoop版本必须和window下的hadoop版本一致 不然可能会报错-->
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jdk.tools/jdk.tools -->
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<!-- <systemPath>C:/Program Files/Java/jdk1.8.0_101/lib/tools.jar</systemPath>-->
<!--这里会报红 但是不用管 不影响我们的操作 -->
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
</dependencies>
由于我们导入了log4j的依赖 所以我们需要编写一个log4j.properties
的配置文件,来自定义log4j的输出格式
#可以去网上搜一下 这个是我以前的格式
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
package com.jee.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.net.URI;
public class HDFSClient {
public static void main(String[] args) throws IOException, InterruptedException {
HDFSClient client = new HDFSClient();
client.myPut("D:/1.txt","/");
}
//配置文件
private Configuration configuration = new Configuration();
//Hadoop用户名(我的是jee)
private String userName = "jee";
//HDFS文件系统的实例化对象
private FileSystem fs = null;
//初始化文件系统对象
public void initFs() throws IOException, InterruptedException {
//默认配置是使用file:///协议的,不会和HDFS集群通信
// 我们需要把配置文件修改成为我们Hadoop集群中自己配置的值 (好像URI.create("hdfs://hadoop2:9000")可以代替)
//configuration.set("fs.defaultFS","hdfs://hadoop2:9000");
fs = FileSystem.get(URI.create("hdfs://hadoop2:9000"),configuration,userName);
}
//关闭文件系统对象
public void closeFs() throws IOException {
fs.close();
}
//上传文件
public void myPut(String localFilePath,String HDFSPath) throws IOException, InterruptedException {
//初始化文件系统对象
initFs();
//从本地上传到HDFS文件系统中 第一个参数是本地文件的路径 第二个参数是上传到HDFS中的路径
fs.copyFromLocalFile(new Path(localFilePath),new Path(HDFSPath));
//关闭文件系统对象 (HDFS不支持并发写入 不关闭的话 其他的就不能写入了 最好关闭)
closeFs();
}
}
然后我们编写一个Junit测试 看一下我们的环境是不是已经搭建好(注意 需要我们先将集群启动起来!)
public class MyTest {
FileSystem fileSystem = null;
@Before
public void before() throws IOException, InterruptedException {
fileSystem = FileSystem.get(URI.create("hdfs://hadoop2:9000"),new Configuration(),"jee");
System.out.println("fileSystem 初始化成功!");
}
@Test
public void fun1() throws IOException, InterruptedException {
FileSystem fileSystem = FileSystem.get(URI.create("hdfs://hadoop2:9000"),new Configuration(),"jee");
fileSystem.copyFromLocalFile(new Path("D:\\1.txt"),new Path("/"));
fileSystem.close();
}
@Test
//从HDFS文件系统拷贝1.txt文件到本地D盘下
public void get() throws IOException {
fileSystem.copyToLocalFile(new Path("/1.txt"),new Path("D:\\"));
}
@Test
//将HDFS中的1.txt改名成2.txt
public void rename() throws IOException {
fileSystem.rename(new Path("/1.txt"),new Path("/2.txt"));
}
@Test
//将本地D盘下的1.txt拷贝到HDFS中的根目录下
public void put() throws IOException {
fileSystem.copyFromLocalFile(new Path("D:\\1.txt"),new Path("/"));
}
@Test
//删除HDFS系统中的文件
public void delete() throws IOException {
//后面的参数 true 表示级联删除 默认是false
fileSystem.delete(new Path("/1.txt"),true);
}
@After
public void after() throws IOException {
fileSystem.close();
System.out.println("fileSystem 关闭!");
}
}
运行之后如果成功 进入HDFS目录中 查看是否已经操作成功即可。