Over a million developers have joined DZone.

Using awk and Friends with Hadoop

· Database Zone

Imagine you have a CSV file that you want to manipulate. Here’s a sample file we can play with:


Our goal is to transform this into the following form by combining the last three columns:


In Linux this would take all of two seconds (excuse the awkward awk command):

shell$ awk -F"," '{ print $1","$2","$3$4$5 }' people.txt

 What if you wanted to quickly do the same in HDFS - and let’s assume you want to write the results back to HDFS. One approach would be to use the HDFS CLI to stream the inputs into awk, and stream the awk output back into HDFS. You could do this with the HDFS cat and put - options (note that adding a hyphen after put instructs the put command to stream data from standard input to HDFS):

shell$ hadoop fs -cat people.txt | awk -F"," '{ print $1","$2","$3$4$5 }' | hadoop fs -put - people-coalesed.txt

BTW, if your input and output files are LZOP-compressed then this command would work:

shell$ hadoop fs -cat people.txt.lzo | lzop -dc | awk -F"," '{ print $1","$2","$3$4$5 }' | \
         lzop -c | hadoop fs -put - people-coalesed.txt.lzo

This is great if your file isn’t too large, but if it’s multiple gigabytes in length then you probably want to harness the power of MapReduce to get this done in a jiffy! The words “in a jiffy” and “MapReduce” aren’t commonly used together, so what do we do? Well you could crack open Pig or Hive and write some custom user-defined functions, but this means you end up in Java which we want to avoid.

Hadoop Streaming comes to the rescue in these situations. Let’s first create our awk script which will be executed:

shell$ cat people.awk
#!/bin/awk -f

BEGIN { FS = "," }
{ print $1","$2","$3$4$5 }

In Linux, if you make this awk script executable, you could execute is as follows:

shell$ ./people.awk people.txt

In MapReduce-land we don’t need to join data in this particular example, so we don’t need to run any reducers. Call your awk script from mappers via Hadoop Streaming with this command:

shell$ HADOOP_HOME=/usr/lib/hadoop
shell$ ${HADOOP_HOME}/bin/hadoop \
  jar ${HADOOP_HOME}/contrib/streaming/*.jar \
  -D mapreduce.job.reduces=0 \
  -D mapred.reduce.tasks=0 \
  -input people.txt \
  -output people-coalesed \
  -mapper people.awk \
  -file people.awk

A few options in the Hadoop Streaming command are worth examining:


Finally - to get LZO into the picture you need to add -inputformat, -D mapred.output.compress and -D mapred.output.compression.codec arguments:

shell$ HADOOP_HOME=/usr/lib/hadoop
shell$ ${HADOOP_HOME}/bin/hadoop \
  jar ${HADOOP_HOME}/contrib/streaming/*.jar \
  -D mapreduce.job.reduces=0 \
  -D mapred.reduce.tasks=0 \
  -D mapred.output.compress=true \
  -D stream.map.input.ignoreKey=true \
  -D mapred.output.compression.codec=com.hadoop.compression.lzo.LzopCodec \
  -inputformat com.hadoop.mapred.DeprecatedLzoTextInputFormat \
  -input people.txt.lzo \
  -output people-coalesed \
  -mapper people.awk \
  -file people.awk


Published at DZone with permission of Alex Holmes , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}