WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Hadoop & Big Data · Deep Dive into HDFS · question 27 of 120

How is data read in HDFS?

📕 Buy this interview preparation book: 120 Hadoop & Big Data questions & answers — PDF + EPUB for $5

Hadoop Distributed File System (HDFS) is a distributed file system that is designed to store and process large volumes of data across clusters of commodity hardware. The architecture of HDFS ensures high availability, fault tolerance, and scalability. In this answer, I will explain how data is read in HDFS step by step.

1. **Assumptions**: Assume we have an HDFS cluster consisting of a NameNode, which is the master server responsible for managing the namespace and regulating access to data, and several DataNodes that store the actual data blocks. Let’s also assume that a file has been split into blocks (e.g., block1, block2, and block3) and distributed across multiple DataNodes.

2. **Client request**: A client (e.g., a MapReduce job) wants to read data from a file stored in HDFS. It initiates a read request specifying the file name.

3. **Metadata retrieval**: The client communicates with the NameNode to fetch the metadata of the requested file, including the locations of its blocks. The NameNode looks up the metadata in its memory and returns the list of blocks, their replicas’ locations, and their respective DataNodes.

4. **Data retrieval**: The client sorts the DataNodes’ locations based on their proximity (i.e., the closest DataNode first) and then establishes a connection to the DataNode where the first block replica is stored. It reads the block directly from the DataNode by using the local file system’s read API.

5. **Blockwise reading**: The client can read data from the file block by block in a sequential manner. Once the client finishes reading the first block, it proceeds to the next block in the list and repeats the process until all the blocks are read.

6. **Fault tolerance**: If a client encounters an issue while reading a block (e.g., connection failure or DataNode failure), it can access the same block from a different replica stored in another DataNode. This mechanism ensures fault tolerance in HDFS.

Here is a high-level overview of the data reading process in HDFS:

1. Client requests to read a file from HDFS.

2. NameNode provides block and replica locations for the requested file.

3. Client contacts the closest DataNode hosting the block.

4. Data is read from the DataNode’s local file system.

5. The process repeats for each block in the file sequentially.

Below is an illustration of the data reading process in HDFS:

Client -------> NameNode
         Metadata request for 'file.txt'
         Metadata Response:
         Block1: DataNode1, DataNode2
         Block2: DataNode3, DataNode1

Client -------> DataNode1
         Read Block1
               <------ Block1 data
Client -------> DataNode3
         Read Block2
               <------ Block2 data

To sum up, data read in HDFS involves communication between the clients, NameNode, and DataNodes. Clients get the metadata information from the NameNode and read the actual data directly from DataNodes in a blockwise and sequential manner, ensuring high throughput and fault tolerance.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Hadoop & Big Data interview — then scores it.
📞 Practice Hadoop & Big Data — free 15 min
📕 Buy this interview preparation book: 120 Hadoop & Big Data questions & answers — PDF + EPUB for $5

All 120 Hadoop & Big Data questions · All topics