- 创建maven工程,并导入相关依赖(可以加上日志)
org.apache.hadoop hadoop-client3.1.3 junit junit4.12 org.slf4j slf4j-log4j121.7.30
添加日志需在项目的src/main/resources目录下,新建一个文件,命名为“log4j.properties”,在文件中填入一下内容
log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.logfile=org.apache.log4j.FileAppender log4j.appender.logfile.File=target/spring.log log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
2.连接hdfs,将此操作提取出来设置成测试的开始前执行,需将文件系统变量提取成全局变量供全局使用。
private FileSystem fs;
@Before
public void start() throws IOException, URISyntaxException, InterruptedException {
// 创建连接对象
Configuration conf = new Configuration();
// 加载文件系统信息
// uri为你的hdfs路径,conf为连接对象,"root"为你的操作权限者,这根据你的hadoop文件创建者权限,不一致会导致使用时无权限,当然root权限最高
fs = FileSystem.get(new URI("hdfs://192.168.243.131:8020"),conf,"root");
}
3.当然有开始就需要结束,这里一样使用测试将结束的提取出来,像这样的连接服务都需要在结束时释放资源,不然会占用资源,因此可以在结束测试时将资源释放。
@After
public void close() throws IOException {
// 关闭资源
fs.close();
}
4.下面开始编写操作方法
创建新目录,直接就调用mkdirs方法,传入创建路径就可
@Test
public void testMkdir() throws IOException {
// 创建新目录
fs.mkdirs(new Path("/input"));
System.out.println("mkdir ok");
} 上传文件(参数第一个路径设置本地系统路径,第二个设置hdfs路径)
@Test
public void testPut() throws IOException {
// 上传文件(参数第一个路径设置本地系统路径,第二个设置hdfs路径)
fs.copyFromLocalFile(new Path("D:\out5"),new Path("/put"));
System.out.println("put over");
} 获取hdfs文件(参数:boolean delSrc 指是否将原文件删除;Path src 指要下载的文件路径;Path dst 指将文件下载到的路径;boolean useRawLocalFileSystem 是否开启文件校验)
@Test
public void testGet() throws IOException {
// 获取hdfs文件
// boolean delSrc 指是否将原文件删除
// Path src 指要下载的文件路径
// Path dst 指将文件下载到的路径
// boolean useRawLocalFileSystem 是否开启文件校验
fs.copyToLocalFile(new Path("/put"), new Path("D:/out9"));
System.out.println("get over");
} 移动或者更名文件
@Test
public void testMv() throws IOException {
// 移动或者更名文件
fs.rename(new Path(""), new Path(""));
System.out.println(" mv over");
} 删除文件
@Test
public void testDel() throws IOException {
// 删除文件
fs.delete(new Path("/put"),true);
System.out.println("delete over");
} 判断是否为文件 @Test
public void testFiles() throws IOException {
FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
for (FileStatus f:fileStatuses
) {
if (f.isDirectory()){
System.out.println(f.getPath().getName()+"是目录。");
}else System.out.println(f.getPath().getName()+"是文件。");
}
} 获取文件详情 @Test
public void testListFiles() throws IOException {
// 获取文件详情
// 这里它使用的是自己的迭代器
RemoteIterator listFiles = fs.listFiles(new Path("/input"), true);
while (listFiles.hasNext()){
LocatedFileStatus fileStatus = listFiles.next();
System.out.println("========" + fileStatus.getPath() + "=========");
System.out.println("权限:"+fileStatus.getPermission());
System.out.println("管理者:"+fileStatus.getOwner());
System.out.println("管理组:"+fileStatus.getGroup());
System.out.println("文件长度:"+fileStatus.getLen());
System.out.println("时间(毫秒数):"+fileStatus.getModificationTime());
System.out.println("副本数:"+fileStatus.getReplication());
System.out.println("块大小:"+fileStatus.getBlockSize());
System.out.println("文件名:"+fileStatus.getPath().getName());
// 获取块信息
System.out.println("块信息:"+Arrays.toString(fileStatus.getBlockLocations()));
}
}
整个代码
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
public class HDFSApi {
private FileSystem fs;
@Before
public void start() throws IOException, URISyntaxException, InterruptedException {
// 创建连接对象
Configuration conf = new Configuration();
// 加载文件系统信息
fs = FileSystem.get(new URI("hdfs://192.168.243.131:8020"),conf,"root");
}
@After
public void close() throws IOException {
// 关闭资源
fs.close();
}
@Test
public void testMkdir() throws IOException {
// 创建新目录
fs.mkdirs(new Path("/input"));
System.out.println("mkdir ok");
}
@Test
public void testPut() throws IOException {
// 上传文件(参数第一个路径设置本地系统路径,第二个设置hdfs路径)
fs.copyFromLocalFile(new Path("D:\out5"),new Path("/put"));
System.out.println("put over");
}
@Test
public void testGet() throws IOException {
// 获取hdfs文件
// boolean delSrc 指是否将原文件删除
// Path src 指要下载的文件路径
// Path dst 指将文件下载到的路径
// boolean useRawLocalFileSystem 是否开启文件校验
fs.copyToLocalFile(new Path("/put"), new Path("D:/out9"));
System.out.println("get over");
}
@Test
public void testDel() throws IOException {
// 删除文件
fs.delete(new Path("/put"),true);
System.out.println("delete over");
}
@Test
public void testMv() throws IOException {
// 移动或者更名文件
fs.rename(new Path(""), new Path(""));
System.out.println(" mv over");
}
@Test
public void testListFiles() throws IOException {
// 获取文件详情
RemoteIterator listFiles = fs.listFiles(new Path("/input"), true);
while (listFiles.hasNext()){
LocatedFileStatus fileStatus = listFiles.next();
System.out.println("========" + fileStatus.getPath() + "=========");
System.out.println("权限:"+fileStatus.getPermission());
System.out.println("管理者:"+fileStatus.getOwner());
System.out.println("管理组:"+fileStatus.getGroup());
System.out.println("文件长度:"+fileStatus.getLen());
System.out.println("时间(毫秒数):"+fileStatus.getModificationTime());
System.out.println("副本数:"+fileStatus.getReplication());
System.out.println("块大小:"+fileStatus.getBlockSize());
System.out.println("文件名:"+fileStatus.getPath().getName());
// 获取块信息
System.out.println("块信息:"+Arrays.toString(fileStatus.getBlockLocations()));
}
}
@Test
public void testFiles() throws IOException {
FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
for (FileStatus f:fileStatuses
) {
if (f.isDirectory()){
System.out.println(f.getPath().getName()+"是目录。");
}else System.out.println(f.getPath().getName()+"是文件。");
}
}
}
总结一下,API的学习就是看文档、看源码,知道它有哪些方法就可以直接使用 。



