版权声明:本文为博主原创文章,未经博主允许欢迎转载,请注明原文链接。一起交流,共同进步。 https://blog.csdn.net/newbie_907486852/article/details/83058513
package com.yang.hdfs;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.Before;
import org.junit.Test;
public class HDFSTest {
private FileSystem fileSystem;
private Configuration configuration;
@Before
public void initHDFS() throws IOException, InterruptedException, URISyntaxException {
// 1 创建配置信息对象
configuration = new Configuration();
//设置副本数
//configuration.set("dfs.replication", "2");
/*参数优先级: 1、客户端代码中设置的值 2、classpath下的用户自定义配置文件 3、然后是服务器的默认配置*/
// 2 获取文件系统
fileSystem = FileSystem.get(new URI("hdfs://hadoop14:9000"),configuration, "admin");
// 3 打印文件系统
System.out.println("==========================="+fileSystem.toString());
}
/**
* 上传
* @throws Exception
*/
@Test
public void HDFSUpLoad() throws Exception{
//上传文件
fileSystem.copyFromLocalFile(new Path("G:/yang.jpg")
, new Path("/user/admin/mapreduce/wordcount/input/yang3.jpg"));
//关闭资源
fileSystem.close();
System.out.println("over==================================");
}
/**
* 下载
* @throws Exception
*/
@Test
public void getFileFromHDFS() throws Exception{
// boolean delSrc 指是否将原文件删除
// Path src 指要下载的文件路径
// Path dst 指将文件下载到的路径
// boolean useRawLocalFileSystem 是否开启文件效验
// 2 下载文件
fileSystem.copyToLocalFile(false, new Path("hdfs://hadoop14:9000//user/admin/mapreduce/wordcount/input/yang3.jpg"),
new Path("G:/yang3.jpg"), true);
fileSystem.close();
}
/**
* 创建目录
* @throws Exception
*/
@Test
public void mkdirAtHDFS() throws Exception{
//创建目录
fileSystem.mkdirs(new Path("hdfs://hadoop14:9000/admin"));
}
/**
* 删除文件夹
* @throws Exception
*/
@Test
public void deleteAtHDFS() throws Exception{
//删除文件夹 ,如果是非空文件夹,参数2是否递归删除,true递归
fileSystem.delete(new Path("hdfs://hadoop14:9000/admin"), true);
}
/**
* 重命名文件或文件夹
* @throws Exception
*/
@Test
public void renameAtHDFS() throws Exception{
//重命名文件或文件夹
fileSystem.rename(new Path("hdfs://hadoop14:9000/user/admin/mapreduce/wordcount/test/wc.input"),
new Path("hdfs://hadoop14:9000/user/admin/mapreduce/wordcount/test/wc1.input"));
}
/**
* 查看文件名称、权限、长度、块信息
* @throws Exception
*/
@Test
public void readListFiles() throws Exception {
// 思考:为什么返回迭代器,而不是List之类的容器:减少内存损耗
RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/"), true);
while (listFiles.hasNext()) {
LocatedFileStatus fileStatus = listFiles.next();
System.out.println(fileStatus.getPath().getName());
System.out.println(fileStatus.getBlockSize());
System.out.println(fileStatus.getPermission());
System.out.println(fileStatus.getLen());
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
for (BlockLocation bl : blockLocations) {
System.out.println("block-offset:" + bl.getOffset());
String[] hosts = bl.getHosts();
for (String host : hosts) {
System.out.println(host);
}
}
System.out.println("--------------李冰冰的分割线--------------");
}
}
/**
* 判断是文件还是文件夹
* @throws Exception
* @throws IllegalArgumentException
* @throws IOException
*/
@Test
public void findAtHDFS() throws Exception, IllegalArgumentException, IOException{
//获取查询路径下的文件状态信息
FileStatus[] listStatus = fileSystem.listStatus(new Path("/"));
//遍历所有文件状态
for (FileStatus status : listStatus) {
if (status.isFile()) {
System.out.println("f--" + status.getPath().getName());
} else {
System.out.println("d--" + status.getPath().getName());
}
}
}
}