1.HDFS的Shell操作
1.1 基本语法
语法一:
bin/hadoop fs 命令
语法二:
bin/hdfs dfs 命令
1.2 命令大全
[JL@hadoop101 hadoop-3.1.3]$ bin/hadoop fs
[-appendToFile <localsrc> ... <dst>]
[-cat [-ignoreCrc] <src> ...]
[-checksum <src> ...]
[-chgrp [-R] GROUP PATH...]
[-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...]
[-chown [-R] [OWNER][:[GROUP]] PATH...]
[-copyFromLocal [-f] [-p] <localsrc> ... <dst>]
[-copyToLocal [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
[-count [-q] <path> ...]
[-cp [-f] [-p] <src> ... <dst>]
[-createSnapshot <snapshotDir> [<snapshotName>]]
[-deleteSnapshot <snapshotDir> <snapshotName>]
[-df [-h] [<path> ...]]
[-du [-s] [-h] <path> ...]
[-expunge]
[-get [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
[-getfacl [-R] <path>]
[-getmerge [-nl] <src> <localdst>]
[-help [cmd ...]]
[-ls [-d] [-h] [-R] [<path> ...]]
[-mkdir [-p] <path> ...]
[-moveFromLocal <localsrc> ... <dst>]
[-moveToLocal <src> <localdst>]
[-mv <src> ... <dst>]
[-put [-f] [-p] <localsrc> ... <dst>]
[-renameSnapshot <snapshotDir> <oldName> <newName>]
[-rm [-f] [-r|-R] [-skipTrash] <src> ...]
[-rmdir [--ignore-fail-on-non-empty] <dir> ...]
[-setfacl [-R] [{-b|-k} {-m|-x <acl_spec>} <path>]|[--set <acl_spec> <path>]]
[-setrep [-R] [-w] <rep> <path> ...]
[-stat [format] <path> ...]
[-tail [-f] <file>]
[-test -[defsz] <path>]
[-text [-ignoreCrc] <src> ...]
[-touchz <path> ...]
[-usage [cmd ...]]
1.3 实际操作
先启动hadoop集群,方便后续操作
[JL@hadoop101 hadoop-3.1.3]$ sbin/start-dfs.sh
(1)-help:输出这个命令参数
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -help rm
(2)-ls: 显示目录信息
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -ls /
(3)-mkdir:在HDFS上创建目录
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -mkdir -p /sanguo/shuguo
(4)-moveFromLocal:从本地剪切粘贴到HDFS
[JL@hadoop101 hadoop-3.1.3]$ touch kongming.txt
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -moveFromLocal ./kongming.txt /sanguo/shuguo
(5)-appendToFile:追加一个文件到已经存在的文件末尾
[JL@hadoop101 hadoop-3.1.3]$ touch liubei.txt
[JL@hadoop101 hadoop-3.1.3]$ vi liubei.txt
输入
san gu mao lu
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -appendToFile liubei.txt /sanguo/shuguo/kongming.txt
(6)-cat:显示文件内容
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -cat /sanguo/shuguo/kongming.txt
(7)-chgrp 、-chmod、-chown:Linux文件系统中的用法一样,修改文件所属权限
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -chmod 666 /sanguo/shuguo/kongming.txt
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -chown JL:JL /sanguo/shuguo/kongming.txt
(8)-copyFromLocal:从本地文件系统中拷贝文件到HDFS路径去
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -copyFromLocal README.txt /
(9)-copyToLocal:从HDFS拷贝到本地
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -copyToLocal /sanguo/shuguo/kongming.txt ./
(10)-cp :从HDFS的一个路径拷贝到HDFS的另一个路径
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -cp /sanguo/shuguo/kongming.txt /zhuge.txt
(11)-mv:在HDFS目录中移动文件
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -mv /zhuge.txt /sanguo/shuguo/
(12)-get:等同于copyToLocal,就是从HDFS下载文件到本地
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -get /sanguo/shuguo/kongming.txt ./
(13)-getmerge:合并下载多个文件,比如HDFS的目录 /user/JL/test下有多个文件:log.1, log.2,log.3,…
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -getmerge /user/JL/test/* ./zaiyiqi.txt
(14)-put:等同于copyFromLocal
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -put ./zaiyiqi.txt /user/JL/test/
(15)-tail:显示一个文件的末尾
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -tail /sanguo/shuguo/kongming.txt
(16)-rm:删除文件或文件夹
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -rm /user/JL/test/jinlian2.txt
(17)-rmdir:删除空目录
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -mkdir /test
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -rmdir /test
(18)-du统计文件夹的大小信息
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -du -s -h /user/JL/test
2.7 K /user/JL/test
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -du -h /user/JL/test
1.3 K /user/JL/test/README.txt
15 K /user/JL/test/jinlian.txt
1.4 K /user/JL/test/zaiyiqi.txt
(19)-setrep:设置HDFS中文件的副本数量
[JL@hadoop101 hadoop-3.1.3]$ hadoop fs -setrep 5 /README.txt
这里设置的副本数只是记录在NameNode的元数据中,是否真的会有这么多副本,还得看DataNode的数量。因为目前只有3台设备,最多也就3个副本,只有节点数的增加到至少5台时,副本数才能达到5。
(20)解决web页面中操作没有权限问题
hadoop默认情况下开启了权限检查,且默认使用dir.who作为http访问的静态用户,因此可通过关闭权限检查或者配置http访问的静态用户为JL,二选一即可.
在core-site.xml中修改http访问的静态用户为JL
<property>
<name>hadoop.http.staticuser.user</name>
<value>JL</value>
</property>
在hdfs-site.xml中关闭权限检查
<property>
<name>dfs.permissions.enabled</name>
<value>false</value>
</property>
2.HDFS客户端操作
2.1 准备
(1)准备windows本地库(可以从github上获取)
(2)配置HADOOP_HOME和Path环境变量
如果配置了环境变量,还是找不到hadoop命令,就将hadoop.dll文件拷贝到c:/windows/system32目录下,然后重启系统。
(3)创建Maven工程
(4)导入依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.1.3</version>
</dependency>
</dependencies>
也可以用hadoop-client-api和hadoop-client-runtime代替hadoop-client依赖。
在项目的src/main /resources目录下,新建一个文件,命名“log4j2.xml”,在文件中填入
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error" strict="true" name="XMLConfig">
<Appenders>
<!-- 类型名为Console,名称为必须属性 -->
<Appender type="Console" name="STDOUT">
<!-- 布局为PatternLayout的方式,
输出样式为[INFO] [2018-01-22 17:34:01][org.test.Console]I'm here -->
<Layout type="PatternLayout"
pattern="[%p] [%d{yyyy-MM-dd HH:mm:ss}][%c{10}]%m%n" />
</Appender>
</Appenders>
<Loggers>
<!-- 可加性为false -->
<Logger name="test" level="info" additivity="false">
<AppenderRef ref="STDOUT" />
</Logger>
<!-- root loggerConfig设置 -->
<Root level="info">
<AppenderRef ref="STDOUT" />
</Root>
</Loggers>
</Configuration>
(5)创建HdfsClient类
public class HdfsClient{
@Test
public void testHdfsClient() throws IOException, InterruptedException {
//1. 创建HDFS客户端对象,传入uri, configuration , user
FileSystem fileSystem =
FileSystem.get(URI.create("hdfs://hadoop101:9820"), new Configuration(), "JL");
//2. 操作集群
// 例如:在集群的/目录下创建 testHDFS目录
fileSystem.mkdirs(new Path("/testHDFS"));
//3. 关闭资源
fileSystem.close();
}
}
测试程序。
2.2 HDFS的API操作
2.2.1 HDFS文件上传(测试参数优先级)
(1)编写源代码
@Test
public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
// 1 获取文件系统
Configuration configuration = new Configuration();
// 设置副本数为2个
configuration.set("dfs.replication", "2");
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:9820"), configuration, "JL");
// 2 上传文件
fs.copyFromLocalFile(new Path("e:/banzhang.txt"), new Path("/banzhang.txt"));
// 3 关闭资源
fs.close();
}
(2)在项目的resources中新建hdfs-site.xml文件,并将如下内容拷贝进去,再次测试
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
</configuration>
(3)参数优先级
参数优先级排序:客户端代码中设置的值 >ClassPath下的用户自定义配置文件 >然后是服务器的自定义配置(xxx-site.xml) >服务器的默认配置(xxx-default.xml)
2.2.2 HDFS文件下载
@Test
public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:9820"), configuration, "JL");
// 2 执行下载操作
// boolean delSrc 指是否将原文件删除
// Path src 指要下载的文件路径
// Path dst 指将文件下载到的路径
// boolean useRawLocalFileSystem 是否开启文件校验
fs.copyToLocalFile(false, new Path("/banzhang.txt"), new Path("e:/banhua.txt"), true);
// 3 关闭资源
fs.close();
}
2.2.3 HDFS文件夹删除
@Test
public void testDelete() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:9820"), configuration, "JL");
// 2 执行删除
fs.delete(new Path("/sanguo/"), true);
// 3 关闭资源
fs.close();
}
2.2.4 HDFS文件名更改/移动
@Test
public void testRename() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:9820"), configuration, "JL");
// 2 修改文件名称
fs.rename(new Path("/banzhang.txt"), new Path("/banhua.txt"));
// 3 关闭资源
fs.close();
}
2.2.5 HDFS文件详情查看
查看文件名称、权限、长度、块信息
@Test
public void testListFiles() throws IOException, InterruptedException, URISyntaxException{
// 1获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:9820"), configuration, "JL");
// 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();
}
2.2.6 HDFS文件和文件夹判断
@Test
public void testListStatus() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件配置信息
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:9820"), configuration, "JL");
// 2 判断是文件还是文件夹
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : listStatus) {
// 如果是文件
if (fileStatus.isFile()) {
System.out.println("f:"+fileStatus.getPath().getName());
}else {
System.out.println("d:"+fileStatus.getPath().getName());
}
}
// 3 关闭资源
fs.close();
}