46-hadoop-hdfs-api:
HDFS-API-window环境准备
1、准备hadoop依赖
2、配置环境变量
双击winutils.exe(一闪而过)
创建maven项目,略过
添加依赖
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
</dependencies>
在项目的 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
创建目录测试
private FileSystem fileSystem;
@Before
public void init() throws URISyntaxException, IOException, InterruptedException {
//连接集群namenode地址
URI uri = new URI("hdfs://192.168.1.102:8020");
//配置文件
Configuration configuration = new Configuration();
//获取客户端对象
fileSystem = FileSystem.get(uri, configuration,"root");
}
@After
public void close() throws IOException {
//关闭资源
fileSystem.close();
}
@Test
public void testMkdir() throws URISyntaxException, IOException, InterruptedException {
//执行相关操作,创建一个文件夹
fileSystem.mkdirs(new Path("/xiyou/ huaguosan1"));
}
上传
/**参数优先级 hdfs-default.xml=>dfs-site.xml=>resource目录下的hdfs-site.xml =>//配置文件 Configuration configuration = new Configuration(); * configuration.set(“dfs.replication”,“2”); */
//上传api测试
@Test
public void testPut() throws IOException {
//参数,1是否删除元数据,2如果目标已经存在,是否覆盖 3.元数据路径 4.目标路径
fileSystem.copyFromLocalFile(false,false,
new Path("D:\\Study\\test-txt\\quninainaidetui.txt"),
new Path("hdfs://hadoop102/xiyou/huaguosan"));
}
下载
.crc是一种校验数据的方式(加密,校验。crc加密算法。对比)
//文件的下载
@Test
public void testGet() throws IOException {
//参数1:源文件是否删除 2:源文件路径 3:win目标路径 4:
fileSystem.copyToLocalFile(false,new Path("hdfs://hadoop102/xiyou/huaguosan"),
new Path("D:\\test"),false);
}
文件删除
//文件删除
@Test
public void testRm() throws IOException {
//1,删除路径 2:是否递归删除
//删除文件
fileSystem.delete(new Path("hdfs://hadoop102/xiyou/huaguosan/quninainaidetui.txt"),false);
//删除空目录
fileSystem.delete(new Path("hdfs://hadoop102/xiyou/huaguosan/"),false);
//删除非空目录
fileSystem.delete(new Path("hdfs://hadoop102/jinguo"),true);
}
文件名称的修改和移动
//文件的更名和移动
@Test
public void testMv() throws IOException {
//1:源文件路径 2:目标文件路径
//文件名称的修改
//fileSystem.rename(new Path("hdfs://hadoop102/jinguo/put-shell-hdfs.txt"),
new Path("hdfs://hadoop102/jinguo/xiugaihou-put-shell-hdfs.txt"));
//文件的移动及更名
fileSystem.rename(new Path("hdfs://hadoop102/jinguo/xiugaihou-put-shell-hdfs.txt"),
new Path("/移动及更名-put-shell-hdfs.txt"));
//目录的更名
fileSystem.rename(new Path("hdfs://hadoop102/jinguo"),
new Path("/jinguo02"));
}
查看文件名称、权限、长度、块信息
//获取文件详细信息
@Test
public void fileDetail() throws IOException {
//1:路径 2:是否迭代
//获取所有文件信息
RemoteIterator<LocatedFileStatus> listFiles = fileSystem.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));
}
// 关闭资源
fileSystem.close();
}
HDFS 文件和文件夹判断
@Test
public void testListStatus() throws IOException, InterruptedException,
URISyntaxException{
// 1 获取文件配置信息
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"),
configuration, "root");
// 2 判断是文件还是文件夹
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : listStatus) {
// 如果是文件
if (fileStatus.isFile()) {
System.out.println("是文件:"+fileStatus.getPath().getName());
}else {
System.out.println("是文件夹:"+fileStatus.getPath().getName());
}
}
// 3 关闭资源
fs.close();
}
学习路径:https://space.bilibili.com/302417610/,如有侵权,请联系q进行删除:3623472230