简介
NameNode
负责响应客户端请求。
负责管理元数据(文件名、副本数、Block存放的DN)。
DataNode
存储数据。
向NameNode发送心跳,汇报本身及Block信息。
默认block为128mb。
Secondary NameNode
监控HDFS状态的辅助后台程序,合并fsimage与edits。
fsimage:元数据镜像文件,存储NameNode元数据信息(Secondary Namenode通过合并edits来更新内容)
edits:操作日志文件。
HDFS优缺点
优点
- 数据多份、硬件容错
- 适合存储大文件
- 一次写入多次读取
- 构建在廉价的机械上
缺点 - 不适合小文件存储
HDFS shell
~/app/hadoop-2.6.0-cdh5.15.1/sbin$ hadoop fs
Usage: hadoop fs [generic options]
[-appendToFile <localsrc> ... <dst>] : 将一个本地文件append到一个已存在的文件中
[-cat [-ignoreCrc] <src> ...]:查看文件内容
[-chgrp [-R] GROUP PATH...]:修改组
[-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...]:修改mode
[-chown [-R] [OWNER][:[GROUP]] PATH...]:修改所有者
[-copyFromLocal [-f] [-p] [-l] <localsrc> ... <dst>]:拷贝本地文件到HDFS
[-copyToLocal [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]:从HDFS拷贝到本地
[-count [-q] [-h] [-v] [-x] <path> ...]:查看数量
[-df [-h] [<path> ...]]:查看大小
[-du [-s] [-h] [-x] <path> ...]:查看大小
[-get [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]:获取文件
[-getmerge [-nl] <src> <localdst>]:获取文件然后合并
[-moveFromLocal <localsrc> ... <dst>]:从本地拷贝到HDFS然后删除本地文件
[-moveToLocal <src> <localdst>]:从HDFS拷贝到本地,然后删除HDFS内容
[-mv <src> ... <dst>]
[-put [-f] [-p] [-l] <localsrc> ... <dst>]
[-rm [-f] [-r|-R] [-skipTrash] <src> ...]
[-rmdir [--ignore-fail-on-non-empty] <dir> ...]
[-tail [-f] <file>]
[-test -[defsz] <path>]
[-text [-ignoreCrc] <src> ...]
hdfs dfs 也可以用hadoop fs
- -ls
ls [-R] <path> 显示当前目录下所有文件
示例:
hadoop fs -ls / [显示根目录文件
hadoop fs -ls -R / [递归展示
- -put
-put <localsrc> <dst> //本地文件上传到hdfs
示例:
hadoop fs -put hello.txt / [上传文件到hdfs
hadoop fs -put hello.dir / [上传一个文件夹,并且文件夹中有文件
hadoop fs -put ./salesFile ./wcFile / [上传多个文件
- -moveFromLocal
-put <localsrc> <dst> //本地文件上传到hdfs,上传完成之后,删除本地文件(或文件夹)
示例:
hadoop fs -moveFromLocal test/ /test2 [上传一个文件夹,之后删除本地的文件夹
- -copyFromLocal
-copyFromLocal <localsrc> <dst> //拷贝本地文件到hdfs
示例:
hadoop fs -copyFromLocal hello.txt / [上传文件到hdfs
hadoop fs -copyFromLocal hello.dir / [上传一个文件夹,并且文件夹中有文件
hadoop fs -copyFromLocal ./salesFile ./wcFile / [上传多个文件
- -get
-get [-ignoreCrc] <src> <localdst> //复制文件到本地,可以忽略crc校验
示例:
hadoop fs -get /hello.dir hello2.dir [从hdfs下载一个文件夹包括文件
hadoop fs -get /hello.txt ./hello2.txt [从hdfs下载一个文件
- -cat
-cat <src> //在终端显示文件内容
示例:
hadoop fs -cat /hello.txt [查看文件内容
hadoop fs -cat /hello.dir/hello2.txt
- -text
-text <src> //在终端显示文件内容
示例:
hadoop fs -text /hello.txt [查看文件内容
hadoop fs -text /hello.dir/hello2.txt
- -mkdir
-mkdir <path> //创建文件夹
示例:
hadoop fs -mkdir /hello3.dir [创建文件夹
- -touchz
-touchz <path> //创建一个空文件
示例:
hadoop fs -touchz /hello3.dir/hello3.txt [创建一个空文件
- -mv
-mv <src> <dst> //移动多个文件到目标目录
示例:
hadoop fs -mv /hello.txt /hello3.dir [移动文件
hadoop fs -mv /hello.dir /hello3.dir [移动一个目录包括子文件
- -cp
-cp <src> <dst> //复制多个文件到目标目录
注意这是拷贝hdfs中的文件,而不能用来上传。
示例:
hadoop fs -cp /hello3.dir/hello.txt /hello3.dir/hello2.txt [拷贝一个文件
hadoop fs -cp /hello3.dir /hello.dir [拷贝一整个目录
- -rm
-rm [-r] //删除文件(夹)
示例:
hadoop fs -rm /hello3.dir/hello.txt [删除文件
hadoop fs -rm -R /hello3.dir [删除文件夹
hadoop fs -rm -r /hello.dir [删除文件夹
- -getmerge
-getmerge <src> <localdst> [合并文件到本地文件
示例:
hadoop fs -rm /hdfs-texts ./merge.txt [合并/hdfs-texts目录下的文件到本地
HDFS JavaAPI
添加依赖
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.8.5</version>
</dependency>
上传文件
/**
* 上传文件
* @throws URISyntaxException
* @throws IOException
* @throws InterruptedException
*/
@Test
public void put() throws URISyntaxException, IOException, InterruptedException {
FileSystem fileSystem = FileSystem.get(
new URI("hdfs://host000:8020"),
new Configuration(), "user000");
// //写法1-----------------------------
// 使用copyFromLocalFile方法
// fileSystem.copyFromLocalFile(new Path("./file"), new Path("/file1"));
// //写法2-----------------------------
// 使用create创建输出流
// FileInputStream in = new FileInputStream("./file");
// FSDataOutputStream out = fileSystem.create(new Path("/file2"));
// IOUtils.copy(in, out);
// in.close();
// out.close();
// //---------------------------------
// 带有进度提示
FileInputStream in = new FileInputStream(
"/Users/baozi/dev/doc/bigdata/hadoop-2.6.0-cdh5.7.0.tar.gz");
FSDataOutputStream out = fileSystem.create(new Path("/file3"),
() -> System.out.print("."));//这是实现Progressable接口
IOUtils.copy(in, out);
in.close();
out.close();
}
下载文件
/**
* 下载文件
* @throws URISyntaxException
* @throws IOException
* @throws InterruptedException
*/
@Test
public void get() throws URISyntaxException, IOException, InterruptedException {
FileSystem fileSystem = FileSystem.get(
new URI("hdfs://host000:8020"),
new Configuration(), "user000");
// //写法1-----------------------------
// 使用copyToLocalFile方法
fileSystem.copyToLocalFile(new Path("/file1"), new Path("./file1"));
// //写法2-----------------------------
// 使用open创建输入流。(create和open都是相对hdfs的)
// FSDataInputStream in = fileSystem.open(new Path("/file2"));
// FileOutputStream out = new FileOutputStream("./file2");
// IOUtils.copy(in, out);
// out.close();
// in.close();
}
显示所有文件
/**
* 查看某个目录下的所有文件
* 这里会发现一个问题,我hadoop配置里副本数配置为1,但是我查看到的有些副本数是3
* 原因是:使用Java API上传的文件使用的是Hadoop默认的副本3。
* 但使用Hadoop命令上传的使用的是我们配置的,这里配置的是1。
* @throws URISyntaxException
* @throws IOException
* @throws InterruptedException
*/
@Test
public void listFiles() throws URISyntaxException, IOException, InterruptedException {
FileSystem fileSystem = FileSystem.get(
new URI("hdfs://host000:8020"),
new Configuration(), "user000");
FileStatus[] fileStatus = fileSystem.listStatus(new Path("/"));
for (FileStatus f : fileStatus) {
String fileType = f.isDirectory() ? "目录" : "文件";
String path = "Path:" + f.getPath();
String blockSize = "BlockSize:" + f.getBlockSize();
String len = "Len:" + f.getLen();
String replications = "副本数:" + f.getReplication();
System.out.printf("%s\t%s\t%s\t%s\t%s\n",fileType,path,blockSize,len,replications);
}
}
显示文件内容
其实就是下载
/**
* 查看HDFS文件的内容
* @throws URISyntaxException
* @throws IOException
* @throws InterruptedException
*/
@Test
public void cat() throws URISyntaxException, IOException, InterruptedException {
FileSystem fileSystem = FileSystem.get(
new URI("hdfs://host000:8020"),
new Configuration(), "user000");
//其实和下载一样,就是不输出到文件,输出到控制台
FSDataInputStream in = fileSystem.open(new Path("/file1"));
IOUtils.copy(in,System.out);
in.close();
}
创建文件夹
/**
* 创建HDFS目录
* @throws URISyntaxException
* @throws IOException
* @throws InterruptedException
*/
@Test
public void mkdirs() throws URISyntaxException, IOException, InterruptedException {
FileSystem fileSystem = FileSystem.get(
new URI("hdfs://host000:8020"),
new Configuration(), "user000");
fileSystem.mkdirs(new Path("/baozi/baozi2"));
}
创建文件
其实就是上传
/**
* 创建文件
* @throws URISyntaxException
* @throws IOException
* @throws InterruptedException
*/
@Test
public void touchz() throws URISyntaxException, IOException, InterruptedException {
FileSystem fileSystem = FileSystem.get(
new URI("hdfs://host000:8020"),
new Configuration(), "user000");
//就是创建一个输出流,写文件,其实和上传一样。
FSDataOutputStream out = fileSystem.create(new Path("/baozi/baozi2/newFile"));
out.write("baozi".getBytes());
out.flush();
out.close();
}
重命名文件
/**
* 重命名
* @throws URISyntaxException
* @throws IOException
* @throws InterruptedException
*/
@Test
public void rename() throws URISyntaxException, IOException, InterruptedException {
FileSystem fileSystem = FileSystem.get(
new URI("hdfs://host000:8020"),
new Configuration(), "user000");
fileSystem.rename(new Path("/baozi/baozi2/newFile"),new Path("/baozi/baozi2/oldFile"));
}
删除文件
/**
* 删除
* @throws URISyntaxException
* @throws IOException
* @throws InterruptedException
*/
@Test
public void rm() throws URISyntaxException, IOException, InterruptedException {
FileSystem fileSystem = FileSystem.get(
new URI("hdfs://host000:8020"),
new Configuration(), "user000");
//true递归删除
fileSystem.delete(new Path("/baozi"),true);
}