Hadoop & HDFS Reference
Big data fundamentals, the Hadoop Distributed File System, the HDFS CLI, and Hive query tuning.
1. What is Big Data
Big data describes data whose volume, velocity and variety exceed what traditional databases and tools can capture, store and process. As internet, mobile and social usage grew, storing and managing that data became the central engineering challenge — not just analyzing it.
The 5 V's
| V | Meaning |
|---|---|
| Volume | The sheer scale of data generated — from megabytes per user to daily consumption at cloud scale. |
| Velocity | The speed data arrives and must be processed — streaming clickstreams, transactions, sensor feeds. |
| Variety | Structured, semi-structured and unstructured data mixed together. |
| Veracity | Trustworthiness and quality of the data. |
| Value | The business insight that makes collecting it worthwhile. |
Types of data
- Structured — fixed schema, fits neatly into tables (roughly 20% of data). Easy to store and query in relational databases.
- Semi-structured — no fixed schema but some structure, e.g. XML or JSON. Needs processing before it fits a table.
- Unstructured — no structure at all: images, video, free text (roughly 80% of data).
2. Apache Hadoop
Apache Hadoop (2006) is an open-source framework for distributed storage and processing of big data across clusters of commodity hardware. It assumes hardware failure is common and handles it automatically, making it highly scalable and fault tolerant. Hadoop splits files into large blocks, distributes them across cluster nodes, and processes them in parallel using a master–slave architecture.
3. HDFS Architecture
HDFS follows a master–slave design. The NameNode (master) holds filesystem metadata — the directory tree and which blocks live on which nodes — while DataNodes (slaves) store the actual block data and serve read/write requests. A Secondary NameNode periodically merges edit logs into the filesystem image; it is a checkpoint helper, not a hot standby.
- Block size — files are split into large blocks, default 128 MB, to keep seek overhead low relative to transfer time.
- Replication factor — each block is copied (default 3x) across DataNodes for fault tolerance.
- Rack awareness — replicas are spread across racks so a single rack failure doesn't lose all copies of a block.
Master nodes — run NameNode, ResourceManager and similar coordination services.
Worker nodes — run DataNode and NodeManager, storing data and running tasks.
4. Using the HDFS CLI
As a developer you typically connect to a gateway/client node over SSH, then interact with HDFS using hadoop fs or hdfs dfs — the two are aliases of each other for filesystem operations. hadoop has other subcommands beyond fs and covers HDFS/MapReduce interaction broadly; hdfs has other subcommands beyond dfs and covers administrative tasks (NameNode, Secondary NameNode, DataNode). As developers, day-to-day scope is limited to hdfs dfs / hadoop fs.
Anatomy of a command, using hdfs dfs -ls -l -S -r /public as an example:
hdfs → main command, manages all HDFS components
dfs → sub-command, manages files in HDFS
-ls → file system command: list files
-l -S -r → control arguments (long listing, sort by size, reverse)
/public → argument: the HDFS path to list
5. Command Reference
| Command | Purpose |
|---|---|
hdfs dfs -help [cmd] | Usage / help for a command |
hdfs dfs -ls -R /path | List files, recursively |
hdfs dfs -mkdir -p /path | Create directory (and parents) |
hdfs dfs -rm -r -skipTrash /path | Delete recursively, bypassing trash |
hdfs dfs -put local.txt /path | Copy local → HDFS |
hdfs dfs -get /path local.txt | Copy HDFS → local |
hdfs dfs -cp src dst | Copy within HDFS |
hdfs dfs -mv src dst | Move / rename within HDFS |
hdfs dfs -cat /path | Print file contents |
hdfs dfs -tail /path | Preview last kilobyte of a file |
hdfs dfs -du -h /path | Storage used, human-readable |
hdfs dfs -df -h | Filesystem free/used capacity |
hdfs dfs -stat "%b %r %n" /path | File metadata: size, replication, name |
hdfs dfs -setrep -w 2 /path | Change replication factor |
hdfs dfs -chmod 750 /path | Change file permissions |
hdfs dfs -chown user:group /path | Change file owner/group |
6. YARN Architecture
YARN separates resource management from job scheduling/monitoring, letting Hadoop run more than just MapReduce on one cluster.
- ResourceManager — one per cluster (with a standby for HA); tracks available resources and grants containers to applications.
- NodeManager — one per worker node; launches and monitors containers, reports node health and resource usage to the ResourceManager.
- ApplicationMaster — one per running application (a MapReduce job, a Spark app); negotiates containers from the ResourceManager and coordinates the application's own tasks inside them.
- Container — a bundle of resources (CPU, memory) on one node, the unit YARN actually allocates.
This is why Spark-on-YARN has a driver that runs as (or inside) the ApplicationMaster, and executors that run inside containers negotiated from the ResourceManager — the same YARN concepts underpin both classic MapReduce and modern Spark jobs.
7. MapReduce Phases
A MapReduce job processes data in four stages, each running in parallel across the cluster:
- Map — each mapper reads one input split and emits key-value pairs.
- Shuffle — the framework transfers each mapper's output to the reducer responsible for its key.
- Sort — keys arriving at a reducer are sorted, so a reducer sees all values for a key together and in order.
- Reduce — each reducer aggregates the values for its keys and writes final output, typically back to HDFS.
Shuffle is usually the most expensive phase — it moves data across the network — which is exactly why Spark's equivalent shuffle step is the first thing to tune (see §9 of the Spark reference doc).
8. HDFS Read/Write Path
Writing a file: the client asks the NameNode for a list of DataNodes to host the first block; it streams data to the first DataNode, which forwards it to the second, which forwards it to the third (a replication pipeline) — each DataNode acknowledges back up the pipeline once its copy is durable.
Reading a file: the client asks the NameNode for the block locations of a file; the NameNode returns DataNodes ordered by network proximity to the client, and the client reads each block directly from the nearest available replica — the NameNode itself never serves file data.
Safe mode — on startup, the NameNode enters safe mode: read-only, waiting for DataNodes to report in their block inventories before it considers the filesystem's replication state healthy enough to allow writes.
9. Hadoop Ecosystem
| Tool | Role |
|---|---|
| Hive | SQL-on-Hadoop; translates HiveQL into MapReduce/Tez/Spark jobs over HDFS tables. |
| Pig | Scripting language (Pig Latin) for data flows, compiled down to MapReduce. |
| Sqoop | Bulk transfers between HDFS/Hive and relational databases. |
| Oozie | Workflow scheduler that chains Hadoop jobs (Hive, Pig, MapReduce, Spark) together. |
| ZooKeeper | Distributed coordination service — leader election and configuration for HDFS HA, HBase, Kafka and more. |
10. Hive Query Tuning
Limit optimization
A bare LIMIT still runs the entire query before truncating results — wasteful for ad-hoc CLI exploration. Enable the optimizer so Hive samples input instead of scanning it all:
SET hive.limit.optimize.enable = true; SET hive.limit.row.max.size = 100000; SET hive.limit.optimize.limit.file = 10;
Trade-off: with sampling enabled, a query that also needs a full scan (a JOIN or GROUP BY) risks never seeing all the relevant input.
Join strategies
- Left semi join — returns rows from the left table when a match exists on the right; cheaper than an inner join. Hive has no right semi join.
- Map-side join — broadcasts a small table to every mapper, avoiding a shuffle:
SET hive.auto.convert.join=true;with the thresholdhive.mapjoin.smalltable.filesize(default 25MB). - Bucket map join — when both tables are bucketed on the join key and one bucket count is a multiple of the other, Hive joins matching buckets directly without fetching whole tables:
SET hive.optimize.bucketmapjoin=true; - Sort-merge bucket join — fastest, when bucketed tables share bucket count and are sorted on the join key: adds
SET hive.optimize.bucketmapjoin.sortedmerge=true; - Hive does not support non-equi joins, nor optimized right/full-outer joins.
Ordering data
| Clause | Behavior |
|---|---|
ORDER BY | Total ordering — forces all data through one reducer. Slow at scale. |
SORT BY | Orders within each reducer only — output overlaps between reducers. |
DISTRIBUTE BY col | Controls which reducer rows land on, pair with SORT BY. |
CLUSTER BY col | Shorthand for DISTRIBUTE BY + SORT BY on the same column. |
Other levers
- Local mode —
hive.exec.mode.local.auto=trueruns small jobs on a single machine, skipping cluster overhead. - Parallel execution —
hive.exec.parallel=trueruns independent stages of a query concurrently. - Vectorization — processes 1024 rows per batch instead of one at a time:
hive.vectorized.execution.enabled=true. - JVM reuse —
mapred.job.reuse.jvm.num.tasksamortizes JVM startup cost across many short tasks. - Tez engine —
hive.execution.engine=tezcan be 100–300% faster than classic MapReduce. - Single reducer for multi-GROUP BY —
hive.multigroupby.singlereducer=truemerges multiple GROUP BY operations into one job when their keys overlap. - Dynamic partitioning —
hive.exec.dynamic.partition.mode=strictrequires at least one static partition to guard against accidentally overwriting everything;hive.exec.max.dynamic.partitionsand its per-node variant cap how many partitions a job may create.
11. Replication & Fault Tolerance
HDFS splits files into blocks (128 MB by default, often 256 MB) and stores each block on several DataNodes. The NameNode keeps only metadata in memory: the directory tree, file → blocks, and block → DataNodes.
- Block size, not file size: a 10 MB file uses 10 MB of disk, but every file and block costs about 150 bytes of NameNode heap. That is the real reason small files hurt.
- Spark's default input split (
spark.sql.files.maxPartitionBytes) is also 128 MB, so one block ≈ one task.
Replication factor and rack awareness
The default replication factor is 3 (dfs.replication, or per path with hdfs dfs -setrep). Default placement: replica 1 on the writer's node, replica 2 on a node in a different rack, replica 3 on another node in that same remote rack. Losing any single node or any single rack loses no data, at the cost of 3× storage. Hadoop 3 erasure coding (e.g. RS-6-3) gives similar durability at about 1.5× storage, mainly for cold data.
How each failure is handled
| Failure | Protection |
|---|---|
| DataNode | No heartbeat (every 3 s) for ~10.5 min → marked dead → its blocks are re-replicated from surviving copies |
| Corrupt block | CRC32C checksums verified on read and by a background scanner → read another replica, replace the bad copy |
| Disk | DataNode keeps running with failed volumes tolerated; blocks re-replicated |
| Rack | Rack-aware placement keeps a copy on another rack |
| NameNode | HA: Active + Standby, edits shared via JournalNodes, ZooKeeper failover controllers, fencing |
| Task | YARN reschedules it; Spark recomputes lost partitions from lineage |
hdfs dfs -setrep -w 3 /data/txns # change replication, wait hdfs dfs -stat "%o %r" /data/txns/part-0000 # block size, replication hdfs fsck /data/txns -files -blocks -locations # blocks and replica locations hdfs dfsadmin -report # live / dead DataNodes
12. How Hive Reads Files
Hive puts SQL on top of files. A table is just metadata in the Metastore (schema, location, format, partitions) laid over existing files: schema-on-read. Rows that do not fit the schema come back as NULLs instead of failing.
beeline / JDBC ──► HiveServer2 ──► Metastore (schema, location, format, partitions)
│
└──► Tez / Spark / MapReduce tasks ──► read HDFS blocks locally
CREATE EXTERNAL TABLE transactions (
txn_id STRING, account_id STRING, channel STRING,
amount DOUBLE, event_time TIMESTAMP)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
STORED AS TEXTFILE
LOCATION '/data/txns/'
TBLPROPERTIES ("skip.header.line.count"="1");
SELECT channel, COUNT(*), SUM(amount) FROM transactions GROUP BY channel;
- Query flow: parse → Metastore lookup → block locations from the NameNode → plan (map, shuffle, reduce) → tasks run where the blocks are → a SerDe turns bytes into rows.
- Formats: text/CSV (slow), ORC (Hive native), Parquet (the standard), Avro (row-based with embedded schema).
- Spark uses the same tables via
SparkSession.builder.enableHiveSupport().
Then vs now
| On-prem Hadoop | Cloud today |
|---|---|
| HDFS | S3 / ADLS / GCS |
| YARN | Kubernetes, EMR, Databricks |
| MapReduce / Hive on MR | Spark, Flink, Trino |
| Hive Metastore | Still used; Glue Catalog, Unity Catalog |
| Hive tables | Delta / Iceberg / Hudi |
13. Hive's Problems & How They Were Solved
| Issue | Root cause | Fixed inside Hive | Modern solution |
|---|---|---|---|
| Slow queries (minutes) | MapReduce: JVM per job, HDFS writes between stages, row-at-a-time | Tez, LLAP, vectorization, CBO (Calcite) | Spark SQL, Trino, Snowflake, BigQuery |
| No UPDATE / DELETE / MERGE | Write-once files, no transaction log | Hive ACID (ORC delta files + compaction) | Delta, Iceberg, Hudi |
| No isolation | A table is just a directory | ACID transactions and locks | Snapshot isolation, optimistic concurrency |
| Small files | Micro-batches, many reducers, over-partitioning | hive.merge.*, CONCATENATE | Delta OPTIMIZE, Iceberg rewrite_data_files |
| Slow partition listing; partitions out of sync | Metastore + directory listing (worst on S3) | MSCK REPAIR, ADD PARTITION | File lists in table metadata; Iceberg hidden partitioning |
| Silent NULLs from bad data | Schema-on-read | Self-describing formats | Schema enforcement; DQ tests (Great Expectations, Deequ, dbt) |
| Schema changes break reads | Positional formats (CSV) | Parquet/ORC match by name | Iceberg column IDs; Delta column mapping |
| Slow joins, skew | Large shuffles, hot keys | Map join, bucket map join, skew join | Spark AQE |
| No real-time or point lookups | Batch scan design | — | Kafka + Spark/Flink; HBase, Redis, MongoDB |
| Weak security | Direct file access bypasses Hive | Kerberos, Ranger/Sentry | Unity Catalog, Lake Formation |
2010 Hive on MapReduce 2013 Stinger: Tez, ORC, vectorization 2016 Hive 2 + LLAP 2018 Hive 3: ACID by default 2019+ Lakehouse table formats queried by Spark / Trino Today the Hive Metastore lives on as a catalog; the Hive engine is mostly legacy.