HDFS(Hadoop distributed File System),它是一个文件系统,用于存储文件,通过目录树来定位文件;其次,它是分布式的,由很多服务器联合起来实现其功能,集群中的服务器有各自的角色。
使用场景:适合一次写入,多次读写的场景,且不支持文件的修改。适合用来做数据分析,并不适合用来做网盘应用。
HDFS中的文件在物理上是分块存储(Block),块的大小可以通过配置参数(dfs.blocksize)来规定,默认大小在Hadoop2.x版本中是128M,老版本中是64M。
【问题:】为什么块的大小不能设置太小,也不能设置太大?
(1)HDFS的块设置太小,会增加寻址时间,程序一直在找块的开始位置;
(2)如果块设置的太大,从磁盘传输数据的时间会明显大于定位这个块开始所需的时间。导致程序在处理这块数据时,会非常慢。
总结:HDFS块的大小设置主要取决于磁盘传输速率。
bin/hadoop fs 具体命令
或者 bin/hdfs dfs 具体命令
dfs 是 fs 的实现类。
[fseast@hadoop102 hadoop-2.7.2]$ sbin/ start-dfs.sh
[fseast@hadoop103 hadoop-2.7.2]$ sbin/start-yarn.sh
(1)-help:输出这个命令参数
[fseast@hadoop102 hadoop-2.7.2]$ hdfs dfs -help rm
(2)-ls:显示目录信息
[fseast@hadoop102 hadoop-2.7.2]$ hdfs dfs -ls /
(3)-mkdir:在HDFS上创建目录
[fseast@hadoop102 hadoop-2.7.2]$ hdfs dfs -mkdir -p /user/fseast/
(4)-moveFromLocal:从本地剪切粘贴到HDFS
[fseast@hadoop102 hadoop-2.7.2]$ vim testmoveFrom.txt
[fseast@hadoop102 hadoop-2.7.2]$ hdfs dfs -moveFromLocal ./testmoveFrom.txt /user/fseast/
(5)-appendToFile:追加一个文件到已经存在的文件末尾
[fseast@hadoop102 hadoop-2.7.2]$ hdfs dfs -appendToFile NOTICE.txt /user/fseast/testmoveFrom.txt
(6)-cat:显示文件内容
[fseast@hadoop102 hadoop-2.7.2]$ hdfs dfs -cat /user/fseast/testmoveFrom.txt
(7)-chgrp 、-chmod、-chown:Linux文件系统中的用法一样,修改文件所属权限
(8)-copyFromLocal:从本地文件系统中拷贝文件到HDFS路径去
[fseast@hadoop102 hadoop-2.7.2]$ hdfs dfs -copyFromLocal test001.txt /
(9)-copyToLocal:从HDFS拷贝到本地
[fseast@hadoop102 hadoop-2.7.2]$ rm -rf test001.txt
[fseast@hadoop102 hadoop-2.7.2]$ hdfs dfs -copyToLocal /test001.txt
(10)-cp :从HDFS的一个路径拷贝到HDFS的另一个路径
(11)-mv:在HDFS目录中移动文件
(12)-get:等同于copyToLocal,就是从HDFS下载文件到本地
(13)-getmerge:合并下载多个文件,比如HDFS的目录/user/fseast/test 下有多个文件:log.1 , log.2 , log.3 …
(14)-put:等同于copyFromLocal
(15)-tail:显示一个文件的末尾
(16)-rm:删除文件或文件夹
(17)-rmdir:删除空目录
(18)-du统计文件夹的大小信息
(19)-setrep:设置HDFS中文件的副本数量
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>RELEASEversion>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.8.2version>
dependency>
<dependency>
<groupId>org.apache.hadoopgroupId>
<artifactId>hadoop-commonartifactId>
<version>2.7.2version>
dependency>
<dependency>
<groupId>org.apache.hadoopgroupId>
<artifactId>hadoop-clientartifactId>
<version>2.7.2version>
dependency>
<dependency>
<groupId>org.apache.hadoopgroupId>
<artifactId>hadoop-hdfsartifactId>
<version>2.7.2version>
dependency>
<dependency>
<groupId>jdk.toolsgroupId>
<artifactId>jdk.toolsartifactId>
<version>1.8version>
<scope>systemscope>
<systemPath>${JAVA_HOME}/lib/tools.jarsystemPath>
dependency>
dependencies>
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
/**
* 连通客户端与HDFS的连接
*
* @throws Exception
*/
@Test
public void testClientConnectHDFS() throws Exception {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), conf, "fseast");
//在HDFS上创建一个目录
fs.mkdirs(new Path("/HdfsDemo02/test01"));
//关闭资源
fs.close();
}
/**
* 测试1:上传文件到HDFS
*/
@Test
public void testCopyFromLocal() throws Exception {
//1.获取FileSystem对象
Configuration configuration = new Configuration();
//可以设置多少个副本,优先级最高
configuration.set("dfs.replication", "2");
FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "fseast");
//2.操作
fileSystem.copyFromLocalFile(new Path("e:/file/test/fsdong.txt"), new Path("/HdfsDemo02/test01"));
//3. 关闭资源
fileSystem.close();
}
可以将hdfs-site.xml拷贝到项目的根目录下。
参数优先级:
参数优先级排序:
(1)客户端代码中设置的值 > (2)ClassPath 下的用户自定义配置文件 >(3)然后是服务器的默认配置
@Test
public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "fseast");
// 2 执行下载操作
// boolean delSrc 指是否将原文件删除
// Path src 指要下载的文件路径
// Path dst 指将文件下载到的路径
// boolean useRawLocalFileSystem 是否开启文件校验
fs.copyToLocalFile(false, new Path("/HdfsDemo02/test01/wc.input"), new Path("e:/file/test/"), true);
// 3 关闭资源
fs.close();
}
@Test
public void testDelete() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "fseast");
// 2 执行删除
fs.delete(new Path("/HdfsDemo01/"), true);
// 3 关闭资源
fs.close();
}
@Test
public void testRename() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "fseast");
// 2 修改文件名称
fs.rename(new Path("/HdfsDemo02/test01/fsdong.txt"), new Path("/fseast.txt"));
// 3 关闭资源
fs.close();
}
@Test
public void testListFiles() throws IOException, InterruptedException, URISyntaxException{
// 1获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "fseast");
// 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);
}
}
}
// 3 关闭资源
fs.close();
}
@Test
public void testListStatus() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件配置信息
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "fseast");
// 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();
}
传入一个路径,递归将该路径下的所有的文件还有目录打印到控制台
@Test
public void testListStatus() throws Exception {
conf = new Configuration();
fs = FileSystem.get(new URI(uri), conf,user);
printFileOrDir("/", fs);
}
/**
* 传入一个路径 ,递归将该路径下的所有的文件还有目录打印到控制台
*/
public void printFileOrDir(String path , FileSystem fs) throws Exception {
FileStatus[] listStatus = fs.listStatus(new Path(path));
for (FileStatus fileStatus : listStatus) {
//判断是文件还是目录
if(fileStatus.isFile()) {
System.out.println("File:" + path + "/" + fileStatus.getPath().getName());
}else {
// path: hdfs://hadoop102:9000/0508
String currentPath = fileStatus.getPath().toString().substring("hdfs://hadoop102:9000".length());
//打印当前的目录
System.out.println("Dir:" + currentPath);
//继续迭代当前目录下的子目录及文件
printFileOrDir(currentPath, fs);
fs.close();
}
}
}
1. HDFS文件上传
需求:把本地E盘上 test02.txt 文件上传到HDFS根目录
@Test
public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException {
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "fseast");
// 2 创建输入流
FileInputStream fis = new FileInputStream(new File("e:/test02.txt"));
// 3 获取输出流
FSDataOutputStream fos = fs.create(new Path("/test02.txt"));
// 4 流对拷
IOUtils.copyBytes(fis, fos, configuration);
// 5 关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close();
}
2. HDFS文件下载
需求:从HDFS上下载test02.txt文件到本地e盘上
// 文件下载
@Test
public void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "fseast");
// 2 获取输入流
FSDataInputStream fis = fs.open(new Path("/text02.txt"));
// 3 获取输出流
FileOutputStream fos = new FileOutputStream(new File("e:/test02..txt"));
// 4 流的对拷
IOUtils.copyBytes(fis, fos, configuration);
// 5 关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close();
}
3. 定位文件读取
需求:分块读取HDFS上的大文件,比如根目录下的/hadoop-2.7.2.tar.gz(188M,大于128M所以上传到集群默认分成两块进行存储)
(1)下载第一块:
@Test
public void readFileSeek1() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "fseast");
// 2 获取输入流
FSDataInputStream fis = fs.open(new Path("/hadoop-2.7.2.tar.gz"));
// 3 创建输出流
FileOutputStream fos = new FileOutputStream(new File("e:/hadoop-2.7.2.tar.gz.part1"));
// 4 流的拷贝
byte[] buf = new byte[1024];
for(int i =0 ; i < 1024 * 128; i++){
fis.read(buf);
fos.write(buf);
}
// 5关闭资源
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
fs.close();
}
(2)下载第二块
@Test
public void readFileSeek2() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "fseast");
// 2 打开输入流
FSDataInputStream fis = fs.open(new Path("/hadoop-2.7.2.tar.gz"));
// 3 定位输入数据位置
fis.seek(1024*1024*128);
// 4 创建输出流
FileOutputStream fos = new FileOutputStream(new File("e:/hadoop-2.7.2.tar.gz.part2"));
// 5 流的对拷
IOUtils.copyBytes(fis, fos, configuration);
// 6 关闭资源
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
}
(3)将下载到的两块文件合并查看是否完整 在Windows命令窗口中进入到目录E:\,然后执行如下命令,对数据进行合并:
type hadoop-2.7.2.tar.gz.part2 >> hadoop-2.7.2.tar.gz.part1
合并完成后,将hadoop-2.7.2.tar.gz.part1 重新命名为hadoop-2.7.2.tar.gz。解压发现该包是完整的。
(1)客户端通过 Distributed FileSystem 模块向NameNode请求上传文件,NameNode检查目标文件是否已存在,父目录是否存在。
(2)NameNode返回是否可以上传。
(3)客户端请求第一个 Block 上传到那几个 DataNode 服务器上。
(4)NameNode 返回3个 DataNode节点(默认备份3份的情况下),分别为dn1,dn2,dn3,。
(5)客户端通过 FSDataOutputStream 模块请求dn1上传数据,dn1 收到请求会继续调用dn2,然后dn2调用dn3,将这个通信管道建立完成。
(6)通道建立完成后,dn3应答dn2,dn2会答dn1,最后dn1应答客户端。
(7)客户端开始往dn1 上传第一个Block(先从磁盘读取数据放到一个本地内存缓存),以Packet为单位,dn1收到一个Packet就会传给dn2,dn2传给dn3;dn1每传一个packet会放入一个应答队列等待应答。(DataNode收到Packet会先存到本地磁盘,再把缓存中的Packet传到下一个DataNode)。全部存完从dn3开始往回应答。
(8)当一个Block传输完成之后,客户端再次请求NameNode上传第二个Block到服务器。重复执行3-7的步骤。
(9)传输完成以后,客户端会告诉NameNode数据传输完成。
在HDFS写数据的过程中,NameNode会选择距离带上传数据最近距离的DataNode接受数据。
最近距离如何计算
节点距离:两个节点到达最近的共同祖先的距离总和。看图更清晰:
假设有数据中心d1机架r1中的节点n1.该节点可以表示为/d1/r1/n1。利用这种标记,这里给出四种距离描述。
默认三个副本情况下的副本节点选择:
第一个副本在Client所处的节点上。如果客户端在集群外,随机选一个。
第二个副本和第一个副本位于相同机架,随机节点。
第三个副本位于不同机架,随机节点。
步骤分析:
(1)客户端通过 Distributed FileSystem 向 NameNode 请求下载文件,NameNode 通过查找元数据,找到文件块所在的DataNode地址。
(2)挑选一台DataNode(就近原则,然后随机)服务器,请求读取数据。
(3)DataNode开始传输数据给客户端(从磁盘读取数据输入流,以Packet)为单位来做校验。
(4)客户端以Packet为单位接收,先在本地缓存,然后写入目标文件。
(5)然后接着读取 第二个Block。
下一篇:(二)HDFS——节点分析及新特性