maven仓库
代码语言:javascript
复制<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
javaApi
代码语言:javascript
复制package com.nzqk.demo;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/**
* @Version 1.0
* @Author:zhaoJiaCai
* @Date:2020/12/10 星期四 07:59
*/
public class demo_03 {
/**
* 创建文件夹
*/
@Test
public void demo_01() {
Configuration conf = new Configuration();
boolean boo = false;
try {
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.10.3:8020"), conf);
boo = fs.createNewFile(new Path("/111.txt"));
} catch (Exception ex) {
}
if (boo) {
System.out.println("创建文件成功!");
} else {
System.out.println("创建文件失败!");
}
}
/**
* 修改
*/
@Test
public void demo_02() {
Configuration conf = new Configuration();
boolean boo = false;
try {
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.10.3:8020"), conf);
boo = fs.rename(new Path("/111.txt"), new Path("/222.txt"));
} catch (Exception ex) {
}
if (boo) {
System.out.println("修改文件成功!");
} else {
System.out.println("修改文件失败!");
}
}
/**
* 查看 所有文件
*/
@Test
public void demo_03() {
try {
//1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.10.3:8020"), configuration, "root");
// 2 获取文件详情
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
while (listFiles.hasNext()) {
LocatedFileStatus status = listFiles.next();
// 输出详情
// 文件名称
System.out.println(status.getPath().getName());
// 长度
System.out.println(status.getLen());
// 权限
System.out.println(status.getPermission());
// 分组
System.out.println(status.getGroup());
// 获取存储的块信息
BlockLocation[] blockLocations = status.getBlockLocations();
for (BlockLocation blockLocation : blockLocations) {
// 获取块存储的主机节点
String[] hosts = blockLocation.getHosts();
for (String host : hosts) {
System.out.println(host);
}
}
System.out.println("-----------分割线----------");
}
// 3 关闭资源
fs.close();
} catch (Exception ex) {
}
}
/**
* 查看 遍历根目录下的所有文件 文件夹
*/
@Test
public void testListStatus() throws IOException, InterruptedException, URISyntaxException {
// 1 获取文件配置信息
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.10.3:8020"), configuration, "root");
// 2 判断是文件还是文件夹
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : listStatus) {
System.out.println("============================");
// 如果是文件
if (fileStatus.isFile()) {
System.out.println("f:" fileStatus.getPath().getName());
} else {
System.out.println("d:" fileStatus.getPath().getName());
}
//获取路径
System.out.println(fileStatus.getPath().getParent().toString());
String str = fileStatus.getPath().getParent().toString();
String[] split = str.split("\/");
str = "";
for (int i = 3; i < split.length; i ) {
str = "/" split[i];
}
System.out.println(str);
// 长度
System.out.println(fileStatus.getLen());
// 权限
System.out.println(fileStatus.getPermission());
// 分组
System.out.println(fileStatus.getGroup());
System.out.println("文件的块大小: " (fileStatus.getBlockSize() / 1024 / 1024));
System.out.println("目录所有者: " fileStatus.getOwner());
System.out.println("目录备份数: " fileStatus.getReplication());
}
// 3 关闭资源
fs.close();
}
/**
* 删除
*/
@Test
public void testDelete() throws IOException, InterruptedException, URISyntaxException {
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.10.3:8020"), configuration, "root");
// 2 执行删除
boolean delete = fs.delete(new Path("/222.txt"), true);
// 3 关闭资源
fs.close();
}
/**
* 下载
*/
@Test
public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException {
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.10.3:8020"), configuration, "root");
// 2 执行下载操作
// boolean delSrc 指是否将原文件删除
// Path src 指要下载的文件路径
// Path dst 指将文件下载到的路径
// boolean useRawLocalFileSystem 是否开启文件校验
fs.copyToLocalFile(false, new Path("/demo_01/part-m-00000"), new Path("e:/1.txt"), true);
// 3 关闭资源
fs.close();
}
/**
* 上传
*/
@Test
public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
// 1 获取文件系统
Configuration configuration = new Configuration();
configuration.set("dfs.replication", "2");
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.10.3:8020"), configuration, "root");
// 2 上传文件
fs.copyFromLocalFile(new Path("e:/A.png"), new Path("/A.png"));
// 3 关闭资源
fs.close();
System.out.println("over");
}
}