Skip to content

Hadoop

Install Hadoop on macOS with Homebrew, configure a single-node HDFS and YARN setup, enable SSH to localhost and run basic HDFS commands.

4 min readUpdated

Apache Hadoop stores large data sets in a distributed file system (HDFS) and processes them with MapReduce jobs scheduled by YARN. For development, all daemons can run on one machine in pseudo-distributed mode. This note sets that up on macOS with Homebrew: install, configuration files, SSH to localhost, start and stop aliases, and a few HDFS commands. It was written for Hadoop 2.8; the property names follow the current single-node guide.

Install with Homebrew

Install Hadoop (Homebrew also installs a Java runtime if needed):

Terminal window
brew install hadoop

Homebrew installs Hadoop under its prefix, for example /usr/local/Cellar/hadoop/2.8.0 on Intel Macs or /opt/homebrew/Cellar/hadoop/<version> on Apple silicon. $(brew --prefix hadoop) points to the installed version, and the configuration files are in its libexec/etc/hadoop/ directory.

Configure Hadoop

hadoop-env.sh

On macOS, Hadoop can fail to start with Kerberos realm errors even though Kerberos is not used. Clearing the realm and KDC avoids this. In libexec/etc/hadoop/hadoop-env.sh, find the line:

Terminal window
export HADOOP_OPTS="$HADOOP_OPTS -Djava.net.preferIPv4Stack=true"

and change it to:

Terminal window
export HADOOP_OPTS="$HADOOP_OPTS -Djava.net.preferIPv4Stack=true -Djava.security.krb5.realm= -Djava.security.krb5.kdc="

core-site.xml

Set the HDFS address and the base directory for Hadoop’s temporary and data files:

core-site.xml
<configuration>
<property>
<name>hadoop.tmp.dir</name>
<value>/usr/local/Cellar/hadoop/hdfs/tmp</value>
<description>A base for other temporary directories.</description>
</property>
<property>
<name>fs.defaultFS</name>
<value>hdfs://localhost:9000</value>
</property>
</configuration>

fs.defaultFS replaces the deprecated fs.default.name. Any directory you can write to works for hadoop.tmp.dir; by default HDFS keeps its data under it.

hdfs-site.xml

A single node can hold only one copy of each block, so set the replication factor to 1:

hdfs-site.xml
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
</configuration>

mapred-site.xml

Run MapReduce jobs on YARN. The second property is the one the Hadoop 3 single-node guide adds so that jobs find the MapReduce libraries:

mapred-site.xml
<configuration>
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
<property>
<name>mapreduce.application.classpath</name>
<value>$HADOOP_MAPRED_HOME/share/hadoop/mapreduce/*:$HADOOP_MAPRED_HOME/share/hadoop/mapreduce/lib/*</value>
</property>
</configuration>

The original version of this note set mapred.job.tracker to localhost:9010. That property belongs to the Hadoop 1 JobTracker and has no effect when YARN is used.

yarn-site.xml

Enable the shuffle service that MapReduce needs on the NodeManager:

yarn-site.xml
<configuration>
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
<property>
<name>yarn.nodemanager.env-whitelist</name>
<value>JAVA_HOME,HADOOP_COMMON_HOME,HADOOP_HDFS_HOME,HADOOP_CONF_DIR,CLASSPATH_PREPEND_DISTCACHE,HADOOP_YARN_HOME,HADOOP_HOME,PATH,LANG,TZ,HADOOP_MAPRED_HOME</value>
</property>
</configuration>

Add start and stop aliases

The start scripts launch the HDFS daemons and then YARN; the stop scripts do the reverse. Add two aliases to your shell profile (~/.zshrc on current macOS, ~/.bash_profile or ~/.profile for bash):

Terminal window
alias hstart="$(brew --prefix hadoop)/sbin/start-dfs.sh; $(brew --prefix hadoop)/sbin/start-yarn.sh"
alias hstop="$(brew --prefix hadoop)/sbin/stop-yarn.sh; $(brew --prefix hadoop)/sbin/stop-dfs.sh"

Reload the profile so the aliases are available in the current terminal:

Terminal window
source ~/.zshrc

Format HDFS

Before the first start, format the NameNode. This erases any existing HDFS data, so run it only once:

Terminal window
hdfs namenode -format

Allow SSH to localhost

The start scripts log in to each node, including localhost, over SSH without a password.

Enable Remote Login

Turn on the SSH server in System Settings > General > Sharing > Remote Login (System Preferences > Sharing on older macOS).

Authorise your key

If ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub do not exist yet, create a key pair without a passphrase:

Terminal window
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa

Add the public key to the keys accepted for your account, and make the file readable only by you:

Terminal window
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 0600 ~/.ssh/authorized_keys

Check that you can log in without a password:

Terminal window
$ ssh localhost
Last login: Fri Mar 6 20:30:53 2015
$ exit

Start and stop Hadoop

Start HDFS and YARN:

Terminal window
hstart

Stop them:

Terminal window
hstop

Use basic HDFS commands

Relative HDFS paths resolve to /user/<username>, which does not exist on a new file system. Create it first:

Terminal window
hadoop fs -mkdir -p /user/$(whoami)

Then copy a file in, organise it and run the word count example on it:

Terminal window
# Copy a local file to HDFS
hadoop fs -copyFromLocal test.txt
# List files and directories
hadoop fs -ls
# Create a directory named Directory1
hadoop fs -mkdir Directory1
# Move test.txt into Directory1
hadoop fs -mv test.txt Directory1
# Run the word count example on Directory1 and write the result to output
hadoop jar $(brew --prefix hadoop)/libexec/share/hadoop/mapreduce/hadoop-mapreduce-examples-*.jar wordcount Directory1 output
# Show the result
hadoop fs -cat output/part-r-00000

The output directory must not exist before the job runs; delete it with hadoop fs -rm -r output to run the job again.

References

Related notes

4 min readTürkçe

Ollama

GPU'suz, yalnız CPU ile çalışan makinelerde Ollama ile yerel LLM çalıştırma ve hızı belirleyen ayarlar.

  • AI & Data
  • DevOps & Containers
2 min read

Distance measures

Formulas for the distance measures used in data science: Manhattan, Euclidean, Minkowski, Chebyshev, cosine, Hamming and Jaccard.

  • AI & Data
5 min read

Algorithms for machine learning

How supervised learning fits a model: hypothesis function, cost function, gradient descent, feature scaling, overfitting and cross-validation.

  • AI & Data

Type to search the whole site.