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

 

 

 

 

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