๐Ÿ“Š Data Engineering

Hadoop

Apache Hadoop

A distributed storage/processing framework. Based on HDFS and MapReduce. The early standard for big data processing.

Detailed Explanation

Apache Hadoop is an open-source framework for distributed processing of large-scale data sets. Developed based on Google's GFS and MapReduce papers, it became a core technology that opened the big data era.

Core Components of Hadoop

  • HDFS (Hadoop Distributed File System): A file system that stores large files distributed across multiple nodes
  • YARN (Yet Another Resource Negotiator): Cluster resource management and job scheduling
  • MapReduce: A distributed data processing programming model
  • Hadoop Common: Common utilities and libraries

HDFS Architecture

ComponentRoleFeatures
NameNodeMetadata managementStores file locations and block information
DataNodeStores actual dataStores data in block units (128MB)
Secondary NameNodeCreates checkpointsNot a NameNode backup
BlockData storage unit3-way replication by default

YARN Architecture

ComponentRoleDescription
ResourceManagerGlobal resource managementAllocates resources across the entire cluster
NodeManagerPer-node resource managementRuns and monitors containers
ApplicationMasterApplication managementNegotiates resources per job
ContainerExecution environmentUnit of CPU and memory allocation

Code Example

HDFS Configuration (hdfs-site.xml)

<!-- hdfs-site.xml: HDFS ์„ค์ • -->
<configuration>
    <!-- ๋ธ”๋ก ๋ณต์ œ ๊ณ„์ˆ˜ -->
    <property>
        <name>dfs.replication</name>
        <value>3</value>
        <description>๊ธฐ๋ณธ ๋ธ”๋ก ๋ณต์ œ ์ˆ˜</description>
    </property>

    <!-- ๋ธ”๋ก ํฌ๊ธฐ (128MB) -->
    <property>
        <name>dfs.blocksize</name>
        <value>134217728</value>
    </property>

    <!-- NameNode ๋ฐ์ดํ„ฐ ๋””๋ ‰ํ† ๋ฆฌ -->
    <property>
        <name>dfs.namenode.name.dir</name>
        <value>file:///hadoop/hdfs/namenode</value>
    </property>

    <!-- DataNode ๋ฐ์ดํ„ฐ ๋””๋ ‰ํ† ๋ฆฌ -->
    <property>
        <name>dfs.datanode.data.dir</name>
        <value>file:///hadoop/hdfs/datanode</value>
    </property>

    <!-- NameNode HA ์„ค์ • (์„ ํƒ) -->
    <property>
        <name>dfs.nameservices</name>
        <value>mycluster</value>
    </property>

    <property>
        <name>dfs.ha.namenodes.mycluster</name>
        <value>nn1,nn2</value>
    </property>

    <property>
        <name>dfs.namenode.rpc-address.mycluster.nn1</name>
        <value>namenode1:8020</value>
    </property>

    <property>
        <name>dfs.namenode.rpc-address.mycluster.nn2</name>
        <value>namenode2:8020</value>
    </property>

    <!-- ์ž๋™ Failover ํ™œ์„ฑํ™” -->
    <property>
        <name>dfs.ha.automatic-failover.enabled</name>
        <value>true</value>
    </property>
</configuration>

YARN Configuration (yarn-site.xml)

<!-- yarn-site.xml: YARN ์„ค์ • -->
<configuration>
    <!-- ResourceManager ํ˜ธ์ŠคํŠธ -->
    <property>
        <name>yarn.resourcemanager.hostname</name>
        <value>resourcemanager</value>
    </property>

    <!-- NodeManager ๋ฉ”๋ชจ๋ฆฌ -->
    <property>
        <name>yarn.nodemanager.resource.memory-mb</name>
        <value>65536</value>
        <description>๋…ธ๋“œ๋‹น ํ• ๋‹น ๊ฐ€๋Šฅํ•œ ๋ฉ”๋ชจ๋ฆฌ (64GB)</description>
    </property>

    <!-- NodeManager CPU ์ฝ”์–ด -->
    <property>
        <name>yarn.nodemanager.resource.cpu-vcores</name>
        <value>16</value>
    </property>

    <!-- ์ตœ์†Œ ์ปจํ…Œ์ด๋„ˆ ๋ฉ”๋ชจ๋ฆฌ -->
    <property>
        <name>yarn.scheduler.minimum-allocation-mb</name>
        <value>1024</value>
    </property>

    <!-- ์ตœ๋Œ€ ์ปจํ…Œ์ด๋„ˆ ๋ฉ”๋ชจ๋ฆฌ -->
    <property>
        <name>yarn.scheduler.maximum-allocation-mb</name>
        <value>32768</value>
    </property>

    <!-- Shuffle ์„œ๋น„์Šค ํ™œ์„ฑํ™” (MapReduce/Spark์šฉ) -->
    <property>
        <name>yarn.nodemanager.aux-services</name>
        <value>mapreduce_shuffle,spark_shuffle</value>
    </property>

    <property>
        <name>yarn.nodemanager.aux-services.mapreduce_shuffle.class</name>
        <value>org.apache.hadoop.mapred.ShuffleHandler</value>
    </property>

    <!-- ResourceManager HA ์„ค์ • -->
    <property>
        <name>yarn.resourcemanager.ha.enabled</name>
        <value>true</value>
    </property>

    <property>
        <name>yarn.resourcemanager.cluster-id</name>
        <value>yarn-cluster</value>
    </property>

    <!-- Fair Scheduler ์„ค์ • -->
    <property>
        <name>yarn.resourcemanager.scheduler.class</name>
        <value>org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.FairScheduler</value>
    </property>
</configuration>

HDFS CLI Commands

# HDFS ๊ธฐ๋ณธ ๋ช…๋ น์–ด

# ๋””๋ ‰ํ† ๋ฆฌ ๋ชฉ๋ก ์กฐํšŒ
hdfs dfs -ls /user/hadoop/data

# ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ
hdfs dfs -mkdir -p /user/hadoop/input

# ๋กœ์ปฌ ํŒŒ์ผ์„ HDFS๋กœ ์—…๋กœ๋“œ
hdfs dfs -put local_file.csv /user/hadoop/input/
hdfs dfs -copyFromLocal large_data.parquet /user/hadoop/input/

# HDFS ํŒŒ์ผ์„ ๋กœ์ปฌ๋กœ ๋‹ค์šด๋กœ๋“œ
hdfs dfs -get /user/hadoop/output/result.csv ./
hdfs dfs -copyToLocal /user/hadoop/output/* ./local_output/

# ํŒŒ์ผ ๋‚ด์šฉ ํ™•์ธ
hdfs dfs -cat /user/hadoop/input/sample.txt
hdfs dfs -head /user/hadoop/input/large_file.csv  # ์ฒซ 1KB
hdfs dfs -tail /user/hadoop/output/log.txt        # ๋งˆ์ง€๋ง‰ 1KB

# ํŒŒ์ผ/๋””๋ ‰ํ† ๋ฆฌ ์‚ญ์ œ
hdfs dfs -rm /user/hadoop/temp/old_file.txt
hdfs dfs -rm -r /user/hadoop/temp/                # ์žฌ๊ท€ ์‚ญ์ œ

# ํŒŒ์ผ ์ด๋™/์ด๋ฆ„ ๋ณ€๊ฒฝ
hdfs dfs -mv /user/hadoop/input/old.csv /user/hadoop/input/new.csv

# ๋ณต์ œ ๊ณ„์ˆ˜ ๋ณ€๊ฒฝ
hdfs dfs -setrep -w 2 /user/hadoop/archive/

# ๋””์Šคํฌ ์‚ฌ์šฉ๋Ÿ‰ ํ™•์ธ
hdfs dfs -du -h /user/hadoop/
hdfs dfs -df -h  # ์ „์ฒด ํด๋Ÿฌ์Šคํ„ฐ ์šฉ๋Ÿ‰

# ํŒŒ์ผ ์‹œ์Šคํ…œ ์ ๊ฒ€
hdfs fsck /user/hadoop/data -files -blocks

# ํด๋Ÿฌ์Šคํ„ฐ ๋ฆฌํฌํŠธ
hdfs dfsadmin -report

Accessing HDFS from Python (hdfs library)

# Python์œผ๋กœ HDFS ํŒŒ์ผ ์‹œ์Šคํ…œ ์ ‘๊ทผ
from hdfs import InsecureClient
import pandas as pd

# HDFS ํด๋ผ์ด์–ธํŠธ ์ƒ์„ฑ
client = InsecureClient('http://namenode:9870', user='hadoop')

# ๋””๋ ‰ํ† ๋ฆฌ ๋ชฉ๋ก ์กฐํšŒ
files = client.list('/user/hadoop/data')
print(f"ํŒŒ์ผ ๋ชฉ๋ก: {files}")

# ํŒŒ์ผ ์—…๋กœ๋“œ
client.upload('/user/hadoop/input/data.csv', 'local_data.csv')

# ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ
client.download('/user/hadoop/output/result.csv', 'local_result.csv')

# ํŒŒ์ผ ์ง์ ‘ ์ฝ๊ธฐ (with๋ฌธ ์‚ฌ์šฉ)
with client.read('/user/hadoop/data/sample.csv', encoding='utf-8') as reader:
    df = pd.read_csv(reader)
    print(df.head())

# ํŒŒ์ผ ์“ฐ๊ธฐ
with client.write('/user/hadoop/output/processed.json', encoding='utf-8') as writer:
    import json
    json.dump({'result': 'success', 'count': 1000}, writer)

# ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ
client.makedirs('/user/hadoop/new_project/input')

# ํŒŒ์ผ ์‚ญ์ œ
client.delete('/user/hadoop/temp/old_file.txt')

# ์ƒํƒœ ์ •๋ณด ์กฐํšŒ
status = client.status('/user/hadoop/data/large_file.parquet')
print(f"ํŒŒ์ผ ํฌ๊ธฐ: {status['length']} bytes")
print(f"๋ณต์ œ ๊ณ„์ˆ˜: {status['replication']}")

Practical Conversation Example

Junior Engineer: "I'm trying to set up a Hadoop cluster. Is NameNode HA really necessary?"
Senior Engineer: "It's a must for production. The NameNode is a SPOF (single point of failure), so without HA, if the NameNode fails, the entire cluster stops. You need to set up automatic failover with ZooKeeper."
Junior Engineer: "How should I configure YARN memory? The node has 128GB of memory..."
Senior Engineer: "Leave at least 20% for the OS and other daemons. Then allocate about 100GB to YARN, and set the container minimum to 1GB and maximum to around 16GB. Some people also set vcores to 1.5 to 2 times the actual number of cores."
Junior Engineer: "I hear people use cloud services instead of Hadoop these days. Do I need to learn Hadoop?"
Senior Engineer: "You should know the basic concepts. Cloud services like EMR and Dataproc are also based on Hadoop. And it's still widely used in on-premises environments and places with data sovereignty issues."

Cautions

HDFS Cautions

  • Storing many small files puts a burden on NameNode memory - merging files is recommended
  • Files smaller than the block size do not waste space, but incur metadata overhead
  • Changing the replication factor generates network traffic - perform it during idle time

YARN Cautions

  • Memory overcommit settings can cause OOM
  • If queue capacity settings do not match the workload, resources are wasted
  • If the ApplicationMaster fails, the entire job restarts - checkpointing is essential

Cluster Operations

  • Data skew causes load to concentrate on specific nodes
  • Disk failures are recovered automatically, but monitoring is essential
  • Network bandwidth is often the bottleneck

Related Terms

Learn More