每日日报--Hadoop的学习(6)

完成内容:

1.实操HDSFS命令

package com.leiyu.hdfs;

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 HdfsClient {

public FileSystem fs;

@Before
public void init() throws URISyntaxException, IOException, InterruptedException {
//集群地址
URI uri = new URI("hdfs://centos01:8020");
//创建一个配置文件
//副本配置(优先级最高):configuration.set("dfs.replication","2");
Configuration configuration = new Configuration();
//用户
String user = "hadoop";
//获取客户端对象
fs = FileSystem.get(uri, configuration, user);
}

@After
public void close() throws IOException {
//关闭资源
fs.close();
}

@Test
public void testMkdirs() throws URISyntaxException, IOException, InterruptedException {
//创建文件夹
fs.mkdirs(new Path("/xiyou/huaguoshan"));
}

/*
参数优先级
hdfs-default.xml < hdfs-site.xml < 在项目资源目录下的配置文件 < 代码中的配置
*/

@Test
public void testPut() throws IOException {
//文件上传
//参数解读:参数1:是否删除源文件;参数2:是否允许覆盖;参数3:源路径;参数4:目的路径
fs.copyFromLocalFile(false, false, new Path("D:\\sunwukong.txt"), new Path("hdfs://centos01:8020/xiyou/huaguoshan"));
}

@Test
public void testGet() throws IOException {
//文件下载
//参数解读:参数1:是否删除源文件;参数2:源路径;参数3:目的路径;参数4:是否使用CRC校验文件完整性 false:检验 true:不检验
fs.copyToLocalFile(false, new Path("hdfs://centos01:8020/user/hadoop/test/.bashrc"), new Path("D:\\"), false);
}

@Test
public void testRm() throws IOException {
//文件删除
//参数解读:参数1:删除路径;参数2:是否递归删除
//空文件不递归 非空文件递归
fs.delete(new Path("hdfs://centos01:8020/xiyou/huaguoshan"), true);
}

@Test
public void testMv() throws IOException {
//文件的移动和重命名
//参数解读:参数1:原文件路径;参数2:目标文件路径
fs.rename(new Path("hdfs://centos01:8020/xiyou"), new Path("hdfs://centos01:8020/leilei/leiyu"));
}

@Test
public void fileDetail() throws IOException {
//获取文件详细信息
//1.获取所有文件信息
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), 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());

//获取块信息
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
System.out.println(Arrays.toString(blockLocations));
}
}
@Test
public void testfile() throws IOException {
//判断是文件夹还是文件
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for (FileStatus status : listStatus) {
if (status.isFile()) {
System.out.println("文件:"+status.getPath().getName());
}
else{
System.out.println("目录:"+status.getPath().getName());
}
}
}

}
遇到问题:
 无 程序正常运行
目标:继续学习hadoop
posted @ 2021-09-13 11:18  1905-1雷宇  阅读(50)  评论(0编辑  收藏  举报