HDFS之脱贫攻略

1 开场白

1.1 文件系统(File System)是啥

文件系统是操作系统中统一管理信息资源的一种软件,管理文件的存储,检索,更新,提供安全可靠的共享和保护手段,方便用户使用。通过统一的命名空间——目录树来定位管理文件。

1.2 常见的文件系统

Windows —— FAT16文件系统,FAT32文件系统,NTFS文件系统

......

2 HDFS(Hadoop Distributed File System)是啥

HDFS也是一个文件系统,不同于FAT,NTFS等常见文件系统,HDFS支持分布式用于存储超大文件,运行在商用硬件的集群上。

HDFS文件系统会给客户端提供一个统一的抽象目录树,客户端通过路径来访问文件,形如:hdfs://namenode:port/dir-a/dir-b/dir-c/file.data。

Hadoop comes with(自带) a distributed filesystem called HDFS, which stands for Hadoop Distributed Filesystem. 

When a dataset outgrows(过大而不适用于) the storage capacity of a single physical machine, it becomes necessary to partition(分割分布) it across a number of separate machines(大量独立的机器).

Filesystems that manage the storage across a network of machines are called distributed filesystems.

One of the biggest challenges is making the filesystem tolerate node failure without suffering data loss(能够容忍节点故障而不丢失数据).

3 HDFS的设计

3.1 适用于HDFS的应用

3.1.1 Very large files超大文件

“Very large” in this context means files that are hundreds of MB, GB, or TB in size, even PB.

3.1.2 Streaming data access流式数据访问

HDFS is built around the idea that the most efficient data processing pattern is a write-once, read-many-times(一次写入多出读取) pattern. 

(适合用于做数据分析)

3.1.3 Commodity hardware商用硬件(廉价)

Hadoop is designed to run on clusters of commodity hardware. for which the chance of node failure across the cluster is high(集群节点故障率高), at least for large clusters. HDFS is designed to carry on working(继续运行) without a noticeable interruption(不会有明显的中断) to the user in the face of such failure.

(HDFS面对节点故障时,被设计成可以继续运行而让用户察觉不到明显的中断)

3.2 不适用于HDFS的应用

3.2.1 Low-latency data access低延迟数据访问

HDFS is optimized for delivering a high throughput of data(高数据吞吐量), and this may be at the expense of latency(以延迟为代价). HBase is currently a better choice for low-latency access.

(HDFS不适用于需要低延迟访问数据在毫秒范围内的应用,如不适合做网盘应用,网络开销大)

3.2.2 Lots of small files大量的小文件

Because the namenode holds filesystem metadata(元数据) in memory.

 Each file, directory, and block takes about(约需要) 150 bytes.

If you had one million files, each taking one block, you would need at least 300 MB of memory. Although storing millions of files is feasible, billions is beyond the capability of current hardware.(存储百万个文件还可行,十亿或更多个文件就......)

(对于大量的小件,单是维护文件元数据所占用的内存就会超出硬件的能力了)

元数据(metadata)

下面是契诃夫的小说《套中人》中的一段,描写一个叫做瓦莲卡的女子:

(她)年纪已经不轻,三十岁上下,个子高挑,身材匀称,黑黑的眉毛,红红的脸蛋--一句话,不是姑娘,而是果冻,她那样活跃,吵吵嚷嚷,不停地哼着小俄罗斯的抒情歌曲,高声大笑,动不动就发出一连串响亮的笑声:哈,哈,哈!

这段话里提供了这样几个信息:年龄(三十岁上下)、身高(个子高挑)、相貌(身材匀称,黑黑的眉毛,红红的脸蛋)、性格(活跃,吵吵嚷嚷,不停地哼着小俄罗斯的抒情歌曲,高声大笑)。有了这些信息,我们就可以大致想像出瓦莲卡是个什么样的人。推而广之,只要提供这几类的信息,我们也可以推测出其他人的样子。

这个例子中的"年龄"、"身高"、"相貌"、"性格",就是元数据,因为它们是用来描述具体数据/信息的数据/信息。

(引用自阮一峰的博文:元数据(MetaData)

(3)Multiple writers, arbitrary(任意的) file modifications多用户写入,任意修改文件

Files in HDFS may be written to by a single writer(只有一个写入者). Writes are always made at the end of the file, in append-only fashion(总在文件末尾进行写操作). There is no support for multiple writers or for modifications at arbitrary offsets in the file. (不支持多个写入者,不支持在文件任意位置修改)

4 HDFS涉及的概念

4.1 块(Blocks)

磁盘会被划分为一个个大小相等的块,并且统一编号,磁盘块大小一般为512B

块是最基本的存储单位。

HDFS默认将文件分割成block,默认一块Block大小为128MB(2.x以上版本)

一个256MB的文件,占用156/128=2个Block

不同于普通文件系统的是,HDFS中,如果一个文件小于一个数据块的大小,并不占用整个数据块存储空间

dfs.blocksize = 128M

block按键值对存储在HDFS上,键值对的映射存到内存中。如果小文件太多,那内存的负担会很重。

为毛HDFS中的一个块那么大?

文件的分块可以存储在集群上的任意一个磁盘,每个block都会在其他分散的机器进行复制,默认为3份。如果一个块损坏了,系统会在其他地方读取另一个副本。

dfs.replication = 3

4.2 Namenodes and Datanodes

HDFS采用Master-Slave的结构

NameNode:Master节点,管理文件系统的命名空间,目录树和文件的元数据;管理数据块映射;记录着每个文件每个块所在的数据节点;处理Client的读写请求。

EditLog :操作日志文件。存储在Linux文件系统中。

NameNode uses a transaction log called the EditLog to persistently record every change that occurs to file system metadata. For example, creating a new file in HDFS causes the NameNode to insert a record into the EditLog.

FsImage:元数据镜像文件。存储某一时段NameNode内存元数据信息。存储在Linux文件系统中。

The entire file system namespace, including the mapping of blocks to files and file system properties, is stored in a file called the FsImage.

DataNode:Slave节点。存储文件的数据;执行数据块的读写操作;提供Client的读写请求。 

On startup, the NameNode enters a special state called Safemode(安全模式). Replication of data blocks does not occur when the NameNode is in the Safemode state.

热备份:b是a的热备份,如果a坏掉。那么b马上运行代替a的工作。    

冷备份:b是a的冷备份,如果a坏掉。那么b不能马上代替a工作。但是b上存储a的一些信息,减少a坏掉之后的损失。  

5 HDFS命令行接口

可以通过命令行与HDFS交互,通过fs.default.name来为Hadoop设置默认文件系统。

    <property>
        <name>fs.default.name</name>
        <value>hdfs://hadoop:9000</value>
        <description>change your own hostname</description>
    </property>

5.1 基本文件系统操作

5.1.1 查看HDFS中的根目录信息

hadoop fs -ls /

5.1.2 创建文件夹

hadoop fs -mkdir /test

5.1.3 移动文件或重命名

hadoop fs -mkdir /hdfs路径1 /hdfs路径2

hadoop fs -mv /file1 /file2

5.1.4 上传文件到HDFS

hadoop fs -put /本地文件 /hdfs路径

5.1.5 从HDFS下载文件到本地

hadoop fs -get /hdfs路径   /本地路径
hadoop fs -copyToLocal /hdfs路径 /本地路径
hadoop fs -moveToLocal /hdfs路径  /本地路径

5.1.6 删除hdfs中的文件或文件夹

 hadoop fs -rm -r /test

5.1.7 查看hdfs中的文本文件内容

hadoop fs -cat /file
hadoop fs -tail -f /file

5.1.8 追加内容到已存在的文件

hadoop fs -appendToFile /本地文件   /hdfs中的文件

5.1.9 复制hdfs中的文件到hdfs的另一个目录

 hadoop fs -cp /hdfs路径1  /hdfs路径2

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章