HDFs principle
HDFS (Hadoop Distributed File System) is a distributed filesystem and is a GFS cottage version of Google. It is highly fault-tolerant and provides high-throughput data access, ideal for applications on large-scale datasets, providing a highly tolerant and high-throughput mass data storage solution.
- High-throughput access: Each block of HDFS is distributed across different rack, and HDFs calculates the most recent and least visited servers for users when they access it. Because blocks are backed up on different rack, they are no longer single-data access, so speed and efficiency are very fast. In addition, HDFs can read and write in parallel from the server cluster, increasing the access bandwidth of the file read and write.
- High fault tolerance: system failure is inevitable, how to do after the failure of data recovery and fault-tolerant processing is critical. HDFS ensures the reliability of data in many ways, multiple copies and distributed to different servers in the physical location, the data verification function, the continuous self-test data consistency function in the background provides the possibility of high fault tolerance.
- Linear extension: Because the block information of HDFs is stored on the Namenode, the file block is distributed to Datanode, when the expansion is only added Datanode quantity, the system can be expanded without stopping the service, and no manual intervention is required.
2.1 HDFs Architecture
As shown in HDFs is the master and slave structure, divided into Namenode, secondary namenode and datanode three roles.
- NameNode: There is only one master node in hadoop1.x, which manages the namespace and block mapping information of HDFS, configures the replica policy, and processes client requests;
- Secondary NameNode: Auxiliary NameNode, share NameNode work, regularly merge fsimage and fsedits and push to NameNode, in case of emergency can assist recovery NameNode;
- Datanode:slave node, the actual storage of data, the execution of data block read and write and report storage information to Namenode;
2.2 HDFs Read operation
- The client opens the file that it wants to read by invoking the Open () method of the FileSystem object, which, for HDFs, distributes an instance of the file system;
- Distributedfilesystem by using RPC to call Namenode to determine the location of the file's starting block, the same block will return multiple locations according to the number of repetitions, sorted by the Hadoop cluster topology, near the front of the client;
- The first two steps return a Fsdatainputstream object, which is encapsulated as a Dfsinputstream object, Dfsinputstream can easily manage datanode and namenode data streams, and the client calls read on this input stream. () method;
- The Dfsinputstream that stores the Datanode address of the starting block of the file is connected to the nearest datanode, and the data can be transferred from Datanode to the client by repeatedly invoking the read () method on the data stream;
- When the end of the block is reached, Dfsinputstream closes the connection to the Datanode and then looks for the best datanode of the next block, which is transparent to the client, and the client's point of view is simply to read a continuous stream;
- Once the client finishes reading, the Fsdatainputstream call Close () method closes the file read.
2.3 HDFs Write operations
- The client creates a new file by calling the Create () method of Distributedfilesystem;
- Distributedfilesystem through RPC call Namenode to create a new file without blocks association, before the creation of Namenode will do a variety of checks, such as whether the file exists, the client has no permissions to create and so on. If the validation passes, Namenode will record a record for creating a new file, or an IO exception will be thrown;
- The Fsdataoutputstream object is returned after the first two steps, similar to when the file was read, Fsdataoutputstream encapsulated as Dfsoutputstream, Dfsoutputstream can coordinate Namenode and datanode. The client begins to write data to Dfsoutputstream,dfsoutputstream and cuts the data into small packets and writes to the internal queue called the "Data queue";
- Datastreamer will handle accepting data Queue, it first inquires Namenode this new block is most suitable for storage in which several datanode, such as repeat number is 3, then find 3 most suitable datanode, Lined them up in a pipeline. Datastreamer packet the queue output to the first datanode of the pipeline, the first Datanode packet output to the second Datanode, and so on;
- Dfsoutputstream also has a pair of column called Ack Quene, also has packet composition, waits for Datanode to receive the response, when all pipeline in Datanode all indicate has received, this time AKC Quene to remove the corresponding packet bag;
- The client finishes writing the data and calls the close () method to close the write stream;
- Datastreamer the rest of the bags to the pipeline and waits for the ACK message, after receiving the last Ack, notify Namenode to mark the file as completed.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The principle of HDFS and its operation