大数据全系列 教程
1869个小节阅读:468k
408考研
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
准备工作:
xxxxxxxxxx
#在node1通过脚本生成一个文本文件hh.txt
[root@node1 ~]# for i in `seq 100000`; do echo "hello itbaizhan $i" >> hh.txt; done
# 安装lrzsz包
[root@node1 ~]# yum install lrzsz -y
#将生产的文件下载到F:
[root@node1 ~]# sz hh.txt
方式一:编写Java基础代码(选学):
xxxxxxxxxx
public void uploadFile() throws Exception {
//1.创建配置文件对象
Configuration configuration = new Configuration();
//2.创建文件系统对象,指定URI,conf,userName
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node1:9820"),
configuration,"root");
//3.创建本地输入流对象
FileInputStream fileInputStream = new FileInputStream("F:\\hh.txt");
//4.创建HDFS文件系统的输出流对象
FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path("/api/show/hh.txt"));
//5.创建字节数组,临时存储中间缓存数据
byte[] data = new byte[1024];
int len = -1;
while((len = fileInputStream.read(data))!=-1){
fsDataOutputStream.write(data,0,len);
}
//6.关闭输入流对象
fileInputStream.close();
//7.刷新输出流中的数据
fsDataOutputStream.flush();
//8.关闭输出流
fsDataOutputStream.close();
//9.断开和HDFS之间的连接
fileSystem.close();
}
方式二:调用HDFS高级API
xxxxxxxxxx
//使用HDFS高级API
public void copyFromLocalFile() throws Exception {
//1.创建文件系统对象
Configuration configuration = new Configuration();
//2.设置block大小为1MB,上传时候使用,仅对当前方法有效
configuration.set("dfs.blocksize","1048576");
//3.设置副本数
//configuration.set("dfs.replication","2");
//4.创建文件系统对象
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node1:9820"), configuration, "root");
//5.上传文件
fileSystem.copyFromLocalFile(new Path("F:\\hh.txt"),new Path("/api/show/hh3.txt"));
//6.关闭资源
fileSystem.close();
System.out.println("上传操作完成。。。");
}
参数优先级总结:
参考原则是“就近一致”(约近优先级约高)
客户端代码中的参数设置的值 > 自定义配置文件中的参数的值 > 服务器默认xxx-default.xml
xxxxxxxxxx
public void rename() throws Exception {
//1.创建配置文件对象
Configuration configuration = new Configuration();
//2.生成文件系统对象
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node1:9820"), configuration, "root");
//3.修改名称:相同路径下
//fileSystem.rename(new Path("/api/show/hh.txt"),new Path("/api/show/hh_new.txt"));
//3.移动文件:不同路径下
fileSystem.rename(new Path("/api/show/hh_new.txt"),new Path("/api/hh_new.txt"));
//4.关闭
fileSystem.close();
}
##7.6 下载文件
xxxxxxxxxx
public void copyToLocalFile() throws Exception {
//1.创建配置文件对象
Configuration configuration = new Configuration();
//2.生成文件系统对象
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node1:9820"),configuration,"root");
/**3.调用下载API方法
* delSrc – whether to delete the src 是否原文件,false不删除,true表示删除原文件
* src – path:被下载文件的全路径名对应的Path类的对象
* dst – path:文件下载到的目标全路径名对应的Path类的对象
* useRawLocalFileSystem – 是否开启文件校验.
*/
fileSystem.copyToLocalFile(false,new Path("/api/hh_new.txt"),
new Path("F:/hh_new.txt"),true);
//4.关闭资源
fileSystem.close();
}
xxxxxxxxxx
public void delete() throws Exception {
//1.创建配置文件对象
Configuration configuration = new Configuration();
//2.获取文件系统对象
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node1:9820"),configuration,"root");
/**3.调用删除方法
*Path f :被删除的文件或目录对应的Path类的对象
*boolean recursive:
* false: 删除目录如果是一个非空的目录,抛出以下异常:
* PathIsNotEmptyDirectoryException: ``/api is non empty': Directory is not empty
* 删除时一个文件,或一个空目录,则直接删除。
* true:表示递归删除子文件或目录
*/
//fileSystem.delete(new Path("/api"),false);
//fileSystem.delete(new Path("/api/hh_new.txt"),false);
//fileSystem.delete(new Path("/api"),true);
fileSystem.delete(new Path("/hdfsapi/show"),false);
//4.关闭资源
fileSystem.close();
}
xxxxxxxxxx
public void getFileInfo() throws Exception {
//1.创建配置文件对象
Configuration configuration = new Configuration();
//2.获取文件系统对象
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node1:9820"),configuration,"root");
//3.获取指定文件的详细信息的对象
FileStatus fileStatus = fileSystem.getFileStatus(new Path("/hello.txt"));
//4.从fileStatus获取文件的信息
System.out.println("文件名称:"+fileStatus.getPath().getName());
System.out.println("文件所有者:"+fileStatus.getOwner());
System.out.println("文件所属用户组:"+fileStatus.getGroup());
System.out.println("文件块的大小:"+fileStatus.getBlockSize());
System.out.println("文件总大小:"+fileStatus.getLen());
System.out.println("文件的权限:"+fileStatus.getPermission());
System.out.println("副本数量:"+fileStatus.getReplication());
//5.关闭资源
fileSystem.close();
}
运行后的结果如下:
xxxxxxxxxx
文件名称:hello.txt
文件所有者:root
文件所属用户组:supergroup
文件块的大小:1048576
文件总大小:1788895
文件的权限:rw-r--r--
副本数量:2
xxxxxxxxxx
public void fileOrDir() throws Exception {
//1.创建配置文件对象
Configuration configuration = new Configuration();
//2.获取文件系统对象
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node1:9820"),configuration,"root");
//3.获取指定目录下的子文件或子目录的信息
FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/"));
//4.遍历数组
for(FileStatus fileStatus:fileStatuses){
if(fileStatus.isFile()){
System.out.println(fileStatus.getPath().getName()+" is file");
}else if(fileStatus.isDirectory()){
System.out.println(fileStatus.getPath().getName()+" is directory");
}else{
System.out.println(fileStatus.getPath().getName()+" is other..");
}
}
//5.关闭资源
fileSystem.close();
}
运行程序结果如下:
xxxxxxxxxx
hdfsapi is directory
hello.txt is file
user is directory
xxxxxxxxxx
public void listFiles() throws Exception {
//1.创建配置文件对象
Configuration configuration = new Configuration();
//2.获取文件系统对象
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node1:9820"),configuration,"root");
/**3.获取指定目录下文件列表信息
*Path f:指定目录对应的Path类的对象
*boolean recursive:
* true表示迭代“后代级别的文件“
* false表示仅仅获取指定目录下的文件
*/
RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/user"), true);
//4.遍历迭代器
while(listFiles.hasNext()){
LocatedFileStatus fileStatus = listFiles.next();
System.out.println("文件名称:"+fileStatus.getPath().getName());
System.out.println("文件的长度:"+fileStatus.getLen());
System.out.println("文件所属用户:"+fileStatus.getOwner());
System.out.println("文件所属的用户组:"+fileStatus.getGroup());
System.out.println("文件的权限:"+fileStatus.getPermission());
//5.获取文件块s信息
//BlockLocation[] blockLocations = fileSystem.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
//6.遍历文件块
for(BlockLocation blockLocation:blockLocations){
//获取
System.out.println("偏移量:"+blockLocation.getOffset());
String[] hosts = blockLocation.getHosts();
System.out.println("=============host start print==========");
for(String host:hosts){
System.out.println(host);
}
System.out.println("=============host end print==========");
String[] names = blockLocation.getNames();
System.out.println("names:"+names.toString());
}
System.out.println("====================================");
}
}