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):
brew install hadoopHomebrew 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:
export HADOOP_OPTS="$HADOOP_OPTS -Djava.net.preferIPv4Stack=true"and change it to:
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:
<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:
<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:
<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:
<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):
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:
source ~/.zshrcFormat HDFS
Before the first start, format the NameNode. This erases any existing HDFS data, so run it only once:
hdfs namenode -formatAllow 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:
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsaAdd the public key to the keys accepted for your account, and make the file readable only by you:
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keyschmod 0600 ~/.ssh/authorized_keysCheck that you can log in without a password:
$ ssh localhostLast login: Fri Mar 6 20:30:53 2015$ exitStart and stop Hadoop
Start HDFS and YARN:
hstartStop them:
hstopUse basic HDFS commands
Relative HDFS paths resolve to /user/<username>, which does not exist on a new file system. Create it first:
hadoop fs -mkdir -p /user/$(whoami)Then copy a file in, organise it and run the word count example on it:
# Copy a local file to HDFShadoop fs -copyFromLocal test.txt# List files and directorieshadoop fs -ls# Create a directory named Directory1hadoop fs -mkdir Directory1# Move test.txt into Directory1hadoop fs -mv test.txt Directory1# Run the word count example on Directory1 and write the result to outputhadoop jar $(brew --prefix hadoop)/libexec/share/hadoop/mapreduce/hadoop-mapreduce-examples-*.jar wordcount Directory1 output# Show the resulthadoop fs -cat output/part-r-00000The output directory must not exist before the job runs; delete it with hadoop fs -rm -r output to run the job again.
References
- Hadoop: setting up a single node cluster (Apache Hadoop docs)
- Hadoop file system shell commands (Apache Hadoop docs)
- Hadoop file system shell, version 2.8.0
- Deprecated Hadoop properties and their replacements
- Hadoop formula (Homebrew)
- Hadoop Dünyasına Giriş ve Temel Kavramlar, Hadoop series part 1 (Bilgisayar Kavramları, video in Turkish)