创建目录
上传本地文件到HDFS
文件下载 :HDFS—》本地
删除rm
获取文件详细信息
API文件和文件夹判断
配置好API环境后,可以在idea内编写代码操作HDFS
创建目录
package com.atguigu.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/*
客户端代码常用套路
1.获取客户端对象
2.执行相关的操作命令
3.关闭资源
*/
public class HdfsClient {
public static FileSystem fs;
public static void main(String[] args) throws InterruptedException, IOException, URISyntaxException {
init();
testMkdir();
close();
}
public static void init() throws URISyntaxException, IOException, InterruptedException {
//连接集群的nn地址
URI uri = new URI("hdfs://hadoop102:8020");
//创建一个配置文件
Configuration configuration=new Configuration();
//用户
String user="atguigu";
//获取到客户端对象
fs=FileSystem.get(uri,configuration,user);
}
public static void close() throws IOException {
//关闭资源
fs.close();
}
public static void testMkdir() throws IOException {
//创建一个文件夹
fs.mkdirs(new Path("/xiyou/huaguoshan"));
}
}
上传本地文件到HDFS
//上传
public static void testPut() throws IOException {
//参数一:是否删除原数据 参数二:是否允许覆盖 参数三:原数据路径 参数四:目的地路径
fs.copyFromLocalFile(false,false,new Path("D:\\sunwukong.txt"),new Path("/xiyou/huaguoshan"));
}
文件下载:HDFS–》本地
public static void testGet() throws IOException {
//参数一:原文件是否删除 参数二:原文件的路径 参数三:目标地址路径 参数四:是否开启本地校验,一般为false,不用管
fs.copyToLocalFile(false,new Path("/xiyou/huaguoshan"),new Path("D:\\"),false);
}
删除rm
public static void testRm() throws IOException {
//删除文件
//参数1:要删除的路径 参数2:是否递归删除
fs.delete(new Path("/hu.txt"),false);
//删除空目录
fs.delete(new Path("/xiyou"),false);
//删除非空目录
fs.delete(new Path("/jinguo"),true);
}
文件更名和移动
public static void testmv() throws IOException {
//修改文件名称
//参数1:要修改的原文件路径 参数2:目标文件路径
fs.rename(new Path("/input/word.txt"),new Path("/input/ss.txt"));
//文件的移动和更名
fs.rename(new Path("/input/ss.txt"),new Path("/cls.txt"));
//目录的更名
fs.rename(new Path("/input"),new Path("/output"));
}
获取文件详细信息
//获取文件详细信息
public static void fileDetail() throws IOException {
//获取文件信息
RemoteIterator<LocatedFileStatus> listFiles=fs.listFiles(new Path("/"),true);
//遍历文件
while (listFiles.hasNext()){
LocatedFileStatus fileStatus=listFiles.next();
System.out.println("-----"+fileStatus.getPath()+"-----");
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));
}
}
API文件和文件夹判断
public static void testListStatus() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件配置信息
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "atguigu");
// 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();
}