Save 10%Pay yearly on any plan. 6% half-yearly, 3% quarterly. Help centreWhatsAppTelegram TeamsOpen a ticket

First Steps with Hadoop on Ubuntu: HDFS Commands and Your First Jobs

Hadoop is installed; now what? The HDFS commands you will use daily, running and watching a job, the web pages, and where Hadoop fits beside Spark today.

4 min readBy Daniel

Reviewed by the AmazingRDP technical team

First steps with Hadoop on Ubuntu

Big data wants memory and disks. See our dedicated servers. See Cheap Dedicated Servers from $38.99

This page continues where the installation ends. If Hadoop is not on your machine yet, follow installing Hadoop on Ubuntu (single-node setup) first and come back when jps lists the five Hadoop processes. Here you learn to move data in and out, run jobs, and read what the system tells you.

The picture in three sentences

  • HDFS is the file system: files are cut into large blocks and stored on DataNodes; the NameNode keeps the index.
  • YARN is the scheduler: it hands out memory and processor time to jobs.
  • MapReduce is one way of writing jobs: a "map" step processes pieces of the data in parallel, a "reduce" step combines the results.

Start and check

start-dfs.sh
start-yarn.sh
jps
hdfs dfsadmin -report

The report shows capacity and live DataNodes. One live node is right for a single-machine setup.

HDFS: the commands you will use every day

They mirror the Linux commands, prefixed with hdfs dfs -.

hdfs dfs -mkdir -p /user/hadoop/logs
hdfs dfs -put access.log /user/hadoop/logs/
hdfs dfs -ls /user/hadoop/logs
hdfs dfs -cat /user/hadoop/logs/access.log | head
hdfs dfs -get /user/hadoop/logs/access.log copy.log
hdfs dfs -du -h /user/hadoop
hdfs dfs -rm -r /user/hadoop/old
CommandDoes
-put / -getCopy from the local disk into HDFS, and back
-ls, -cat, -tailList and read
-mv, -cp, -rm -rMove, copy, delete inside HDFS
-du -h, -df -hSpace used and free
-chmod, -chownPermissions, as on Linux

Two things surprise newcomers. Files in HDFS cannot be edited in place; you replace them. And HDFS is built for a modest number of large files: millions of tiny files overload the NameNode.

Look inside a file's storage

hdfs fsck /user/hadoop/logs/access.log -files -blocks

This shows how the file was cut into blocks (128 MB each by default) and whether every block has its copies.

Run a job

Hadoop ships with examples. Count the words in your file:

hadoop jar $HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-examples-*.jar wordcount /user/hadoop/logs /user/hadoop/out1
hdfs dfs -ls /user/hadoop/out1
hdfs dfs -cat /user/hadoop/out1/part-r-00000 | head
  • The output folder must not exist; Hadoop refuses to overwrite results.
  • A file named _SUCCESS in the output marks a finished job.
  • Another example needs no input and tests the machine: hadoop jar …examples-*.jar pi 4 1000.

Watch it

WhereShows
http://localhost:8088YARN: running and finished applications, with logs
http://localhost:9870HDFS: capacity, DataNodes, a file browser under Utilities
yarn application -listRunning jobs on the command line
yarn application -kill IDStops one
yarn logs -applicationId IDA finished job's output, when log aggregation is on

On a remote server, reach these pages through an SSH tunnel and keep the ports closed to the internet; the installation guide explains why.

Write a job without Java

Hadoop Streaming runs any program that reads lines and writes lines as the map and reduce steps. A word count with two small Python scripts:

hadoop jar $HADOOP_HOME/share/hadoop/tools/lib/hadoop-streaming-*.jar \
  -files mapper.py,reducer.py \
  -mapper "python3 mapper.py" -reducer "python3 reducer.py" \
  -input /user/hadoop/logs -output /user/hadoop/out2

The mapper prints word TAB 1 for every word; the reducer receives the lines sorted by word and adds them up.

Stop cleanly

stop-yarn.sh
stop-dfs.sh

Things that go wrong

SignCause
"Name node is in safe mode"Just after start HDFS is read-only while it checks its blocks. Wait a minute; hdfs dfsadmin -safemode get shows the state
"Output directory already exists"Choose a new output folder or delete the old one
Job stays at "ACCEPTED"YARN has no memory to give. Close other programs, or lower the memory settings in yarn-site.xml
"Connection refused" on port 9000HDFS is not running: jps, then start-dfs.sh
"could only be written to 0 of the 1 minReplication nodes"No DataNode is alive, or the disk is full

Where Hadoop stands today

Plain MapReduce is rarely used for new work; it is slow because it writes to disk between steps. Apache Spark does the same kinds of jobs in memory and is far quicker, and it runs on YARN and reads HDFS, so a Hadoop installation is still its natural home. Hive puts SQL on top. Many organisations have moved the storage side to object storage in a cloud. Learning HDFS and YARN is still worthwhile: the ideas of splitting data, moving the computation to it, and surviving failed machines are the same everywhere.

A sensible learning path

  1. The commands on this page, with a few gigabytes of real data such as web server logs.
  2. A streaming job of your own.
  3. Spark on the same machine.
  4. A second and third node, to see replication and failure handling.

For step 4, or data that outgrows a laptop, one machine with plenty of memory and disks goes a long way: dedicated servers. Java versions are covered in Java on Ubuntu.

Questions

How do I copy a file into HDFS?

hdfs dfs -put localfile /path/in/hdfs/

How do I run a MapReduce job?

With hadoop jar JARFILE CLASS INPUT OUTPUT, for example the bundled wordcount example. The output folder must not exist.

How do I check that Hadoop is working?

jps lists the processes, hdfs dfsadmin -report shows the DataNodes, and the web pages on ports 9870 and 8088 show the rest.

Is Hadoop still used?

HDFS and YARN are still widely installed, but new jobs are usually written for Spark, which runs on top of them.