1.shell(服务器)操作
hadoop fs 或者 hdfs dfs(一遍使用后者)
前者可以对各类fs进行操作,后者针对hdfs。
(1)上传
1)-moveFromLocal:剪切
从本地剪切到HDFS
hadoop fs -moveFromLocal D:/a.txt /input
2)-put(copyFromLocal):拷贝
从本地拷贝到HDFS
hadoop fs -put D:/b.txt /
3)-appendToFile:追加
追加一个文件到一个已存在文件的末尾
hadoop fs -appendToFile a.txt /b.txt
(2)下载
1)-get(copyToLocal):拷贝
从HDFS拷贝到本地
hadoop fs -get /a.txt ./
2)-getmerge:合并下载
hadoop fs -getmerge /input/* ./together.txt
(3)其他操作
-ls,-mkdir,-cat,-chgrp,-chown,-chmod,-cp
-mv,-tail,-rm,-rmdir等shell命令可以使用,具体看hadoop fs help。
1)-du:统计文件夹大小
-s:文件大小相加; -h:以最大单位显示
hadoop fs -du -s -h /test
2)-setrep:设置副本数量
hadoop fs -setrep 3
2.客户端操作
2.1环境配置
(1)拷贝依赖
(2)配置HADOOP_HOME环境变量
(3)将bin目录下hadoop.dll和winutils.exe放到C:/windows/system32目录下,重启。
(4)创建Maven工程
(5)导入依赖坐标+日志添加(具体操作看doc文档)
2.2API操作
(1)获取客户端对象
FileSystem.get(uri,conf.user)
//1.创建客户端(文件系统)对象
/*
get(final URI uri, final Configuration conf,
final String user)
uri:NN的(HDFS)地址
conf:配置文件(该对象可以设置各种需要配置的参数,如副本数量)
user:操作HDFS的用户名
*/
URI uri = new URI("hdfs://hadoop102:9820");
Configuration conf = new Configuration();
String user = "atguigu";
fs = FileSystem.get(uri, conf, user);
(2)具体操作
1)上传
copyFromLocalFile()
delSrc:是否删除源文件
overwrite:如果hdfs上已存在是否覆盖
src:源文件路径(本地)
dst:目标路径(本地)
2)下载
copyToLocalFile()
delSrc : 是否删除源文件(HDFS)
src : 源文件(HDFS)
dst : 目标路径(本地)
useRawLocalFileSystem : 是否使用RawLocalFileSystem
true : 不会生成crc文件
false : 会生成crc文件
3)删除
delete()
f : 文件或目录的路径
recursive : 如果是文件true和false都可以。如果是目录必须为true否则报错。
注意:如果是空目录true和false也都可能。
4)修改或改名
rename()
src : 源文件
dst : 目标文件或路径
5)检查或查询
listFiles()
f : 目标路径
recursive : 是否递归
该函数获得一个迭代器,使用迭代器循环获得信息。
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
while(listFiles.hasNext()){
LocatedFileStatus file = listFiles.next();
System.out.println("===="+file.getPath().getName()+"====");
System.out.println("拥有者:"+file.getOwner());
System.out.println("副本数:"+file.getReplication());
BlockLocation[] blockLocations = file.getBlockLocations();
System.out.println(Arrays.toString(blockLocations));
}
}
6)判断
listFile()
该函数返回一个对象数组,使用增强for和isDirectory()和isFile()函数进行判断。
(3)关闭资源
fs.close();
2.3API流操作
(1)上传
fs.create(new Path())
IOUtils.copyBytes()
in : 输入流
out : 输出 流
buffSize : 缓冲区大小
close : 是否关流
//1.从本地读
FileInputStream fis = new FileInputStream("E:\\BigDataLearn\\05-其他\\06-hadoop\\3.代码");
//2.写到HDFS
FSDataOutputStream fos = fs.create(new Path("/c.txt"));
//3.一边读一边写
IOUtils.copyBytes(fis, fos, 1024,false);
//4.关闭资源
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
(2)下载
fs.open(new Path())
IOUtils.copyBytes()
//1.读(HDFS)
FSDataInputStream fis = fs.open(new Path("/longge.txt"));
//2.写(本地)
FileOutputStream fos = new FileOutputStream("D:\\io\\hdfs\\longge.txt");
//3.文件对拷
IOUtils.copyBytes(fis,fos,1024,true);
3.补充
(1)获得客户端对象的另一种方法
/*
没有指定用户名时报的错:
Permission denied: user=wangfeilong, access=WRITE, inode="/":atguigu:supergroup:drwxr-xr-x
解决方案 :
1.指定用户名
Edit Configurations --> VM Options --> -DHADOOP_USER_NAME=atguigu
2.修改/目录的权限
*/
/*
参数的配置:
客户端: 1.代码 2.配置文件
服务器: 1.xxx-site.xml 2.xxx-default.xml
参数的优先级:
客户端代码 > 客户端配置文件 > 服务器xxx-site.xml > 服务器xxx-default.xml
*/
@Test
public void test() throws IOException {
//1.创建客户端对象
Configuration conf = new Configuration();
//设置副本的数量
conf.set("dfs.replication","6");
//配置HDFS的路径(NameNode的地址)
conf.set("fs.defaultFS","hdfs://hadoop102:9820");
FileSystem fs = FileSystem.get(conf);
//2.具体操作
fs.copyFromLocalFile(false,true,
new Path("D:\\io\\hdfs\\f.txt"),
new Path("/"));
//3.关闭资源
fs.close();
}
}
(2)参数的优先级
参数的配置:
客户端: 1.代码 2.配置文件
服务器: 1.xxx-site.xml 2.xxx-default.xml
参数的优先级:
客户端代码 > 客户端配置文件 > 服务器xxx-site.xml >
4.故障处理
NN故障后的处理,后期使用HA高可用,使用概率不高,具体操作看hfds的文档,主要是使用2NN的数据复制到NN上,会有一定数据损失。