HDFS文件系统
HDFS概念
概念
- HDFS是一个文件系统,用于存储文件,通过目录树来定位文件;其次,它还是分布式的,由很多服务器联合起来实现其功能,集群中的服务器有个字的角色。
- HDFS的设计适合一次写入,多次读取的场景,且不支持文件的修改。适合用来做数据分析,并不适合做网盘应用。
组成
- HDFS集群包括,NameNode和DataNode以及Secondary Namenode。
- NameNode负责管理整个文件系统的元数据,以及每一个路径(文件)所对应的数据块信息。
- DataNode 负责管理用户的文件数据块,每一个数据块都可以在多个datanode上存储多个副本。
- Secondary NameNode用来监控HDFS状态的辅助后台程序,每隔一段时间获取HDFS元数据的快照。
HDFS文件块大小
- HDFS中的文件在物理上是分块存储(block),块的大小可以通过配置参数( dfs.blocksize)来规定,默认大小在hadoop2.x版本中是128M,老版本中是64M
- HDFS的块比磁盘的块大,其目的是为了最小化寻址开销。如果块设置得足够大,从磁盘传输数据的时间会明显大于定位这个块开始位置所需的时间。因而,传输一个由多个块组成的文件的时间取决于磁盘传输速率。
- 如果寻址时间约为10ms,而传输速率为100MB/s,为了使寻址时间仅占传输时间的1%,我们要将块大小设置约为100MB。默认的块大小128MB。
- 块的大小:10ms 100 100M/s = 100M
HFDS命令行操作
基本语法
bin/hadoop fs 具体命令
常用命令
(1)-help:输出这个命令参数
(2)-ls: 显示目录信息
(3)-mkdir:在hdfs上创建目录
(4)-moveFromLocal从本地剪切粘贴到hdfs
(5)-moveToLocal:从hdfs剪切粘贴到本地(尚未实现)
(6)-appendToFile :追加一个文件到已经存在的文件末尾
(7)-cat :显示文件内容
(8)-tail:显示一个文件的末尾
(9)-chgrp 、-chmod、-chown:linux文件系统中的用法一样,修改文件所属权限
(10)-copyFromLocal:从本地文件系统中拷贝文件到hdfs路径去
(11)-copyToLocal:从hdfs拷贝到本地
(12)-cp :从hdfs的一个路径拷贝到hdfs的另一个路径
(13)-mv:在hdfs目录中移动文件
(14)-get:等同于copyToLocal,就是从hdfs下载文件到本地
(15)-getmerge :合并下载多个文件,比如hdfs的目录 /aaa/下有多个文件:log.1, log.2,log.3,...
(16)-put:等同于copyFromLocal (hadoop fs -put /aaa/jdk.tar.gz /bbb/jdk.tar.gz.2)
(17)-rm:删除文件或文件夹
(18)-rmdir:删除空目录
(19)-df :统计文件系统的可用空间信息,-h:格式化打印
(20)-du统计文件夹的大小信息:-s:总大小,去掉则表示分别列出文件夹大小
(21)-count:统计一个指定目录下的文件节点数量
[rot@hadoop102 hadoop-2.7.2]$ hadoop fs -count /user/atguigu/wcinput
1 2 197657784 /user/atguigu/wcinput
嵌套文件层级; 包含文件的总数
(22)-setrep:设置hdfs中文件的副本数量
注意:这里设置的副本数只是记录在namenode的元数据中,是否真的会有这么多副本,还得看datanode的数量。因为目前只有3台设备,最多也就3个副本,只有节点数的增加到10台时,副本数才能达到10。
HDFS客户端操作
eclipse环境准备
1)解压hadoop-2.7.2.tar.gz到非中文目录
2)进入share文件夹,查找所有jar包,并把jar包拷贝到_lib文件夹下
3)在全部jar包中查找sources.jar,并剪切到_source文件夹。
4)在全部jar包中查找tests.jar,并剪切到_test文件夹。
1)配置HADOOP_HOME环境变量
2)采用hadoop编译后的bin 、lib两个文件夹(如果不生效,重新启动eclipse)
3)创建第一个java工程
4)执行程序
配置用户名称 在配置界面写上运行参数(-DHADOOP_USER_NAME=root)
客户端去操作hdfs时,是有一个用户身份的。默认情况下,hdfs客户端api会从jvm中获取一个参数来作为自己的用户身份:-DHADOOP_USER_NAME=root,root为用户名称。
通过API操作HDFS
HDFS获取文件系统
@Test
public void initHDFS() throws Exception{
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(configuration);
System.out.println(fs.toString());
}
HDFS文件上传
@Test
public void putFileToHDFS() throws Exception{
Configuration configuration = new Configuration();
configuration.set("dfs.replication", "2");
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"),configuration, "root");
Path src = new Path("e:/hello.txt");
Path dst = new Path("hdfs://hadoop102:9000/user/hello.txt");
fs.copyFromLocalFile(src, dst);
fs.close();
}
将core-site.xml拷贝到项目的根目录下
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>fs.defaultFS</name>
<value>hdfs://hadoop102:9000</value>
</property>
</configuration>
将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>
HDFS文件下载
@Test
public void getFileFromHDFS() throws Exception{
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"),configuration, "atguigu");
fs.copyToLocalFile(false, new Path("hdfs://hadoop102:9000/user/hello.txt"), new Path("e:/hellocopy.txt"), true);
fs.close();
}
HDFS目录创建
@Test
public void mkdirAtHDFS() throws Exception{
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"),configuration, "root");
fs.mkdirs(new Path("hdfs://hadoop102:9000/user/output"));
}
HDFS文件夹删除
@Test
public void deleteAtHDFS() throws Exception{
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"),configuration, "root");
fs.delete(new Path("hdfs://hadoop102:9000/user/output"), true);
}
HDFS文件名更改
@Test
public void renameAtHDFS() throws Exception{
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"),configuration, "root");
fs.rename(new Path("hdfs://hadoop102:9000/user/hello.txt"), new Path("hdfs://hadoop102:9000/user/hellonihao.txt"));
}
HDFS文件详情查看
@Test
public void readListFiles() throws Exception {
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"),configuration, "root");
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
while (listFiles.hasNext()) {
LocatedFileStatus fileStatus = listFiles.next();
System.out.println(fileStatus.getPath().getName());
System.out.println(fileStatus.getBlockSize());
System.out.println(fileStatus.getPermission());
System.out.println(fileStatus.getLen());
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
for (BlockLocation bl : blockLocations) {
System.out.println("block-offset:" + bl.getOffset());
String[] hosts = bl.getHosts();
for (String host : hosts) {
System.out.println(host);
}
}
System.out.println("--------------李冰冰的分割线--------------");
}
}
HDFS文件和文件夹判断
@Test
public void findAtHDFS() throws Exception, IllegalArgumentException, IOException{
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"),configuration, "root");
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for (FileStatus status : listStatus) {
if (status.isFile()) {
System.out.println("f--" + status.getPath().getName());
} else {
System.out.println("d--" + status.getPath().getName());
}
}
}
通过IO流操作HDFS
HDFS文件上传
@Test
public void putFileToHDFS() throws Exception{
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"),configuration, "root");
FileInputStream inStream = new FileInputStream(new File("e:/hello.txt"));
String putFileName = "hdfs://hadoop102:9000/user/hello1.txt";
Path writePath = new Path(putFileName);
FSDataOutputStream outStream = fs.create(writePath);
try{
IOUtils.copyBytes(inStream, outStream, 4096, false);
}catch(Exception e){
e.printStackTrace();
}finally{
IOUtils.closeStream(inStream);
IOUtils.closeStream(outStream);
}
}
HDFS文件下载
@Test
public void getFileToHDFS() throws Exception{
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"),configuration, "root");
String filename = "hdfs://hadoop102:9000/user/atguigu/hello1.txt";
Path readPath = new Path(filename);
FSDataInputStream inStream = fs.open(readPath);
try{
IOUtils.copyBytes(inStream, System.out, 4096, false);
}catch(Exception e){
e.printStackTrace();
}finally{
IOUtils.closeStream(inStream);
}
}
定位文件读取
下载第一块
@Test
public void readFileSeek1() throws Exception {
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "root");
Path path = new Path("hdfs://hadoop102:9000/user/tmp/hadoop-2.7.2.tar.gz");
FSDataInputStream fis = fs.open(path);
FileOutputStream fos = new FileOutputStream("e:/hadoop-2.7.2.tar.gz.part1");
byte[] buf = new byte[1024];
for (int i = 0; i < 128 * 1024; i++) {
fis.read(buf);
fos.write(buf);
}
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
}
下载第二块
@Test
public void readFileSeek2() throws Exception{
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "root");
Path path = new Path("hdfs://hadoop102:9000/user/tmp/hadoop-2.7.2.tar.gz");
FSDataInputStream fis = fs.open(path);
FileOutputStream fos = new FileOutputStream("e:/hadoop-2.7.2.tar.gz.part2");
fis.seek(1024 * 1024 * 128);
IOUtils.copyBytes(fis, fos, 1024);
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
}
合并文件
在window命令窗口中执行 type hadoop-2.7.2.tar.gz.part2 >> hadoop-2.7.2.tar.gz.part1