Friday, December 18, 2015

Install and run pig 0.15.0 on hadoop 2.6.2 for Linux or Ubuntu

Below are instructions to download install and Run PIG 0.15.0

DownLoad tar.gz

Move it to a new location: on my machine /usr/local/pig-0.15.0
      sudo mv pig-0.15.0 /usr/local/

Now setup env vars, below are variables on my machine (ubuntu 14+ LTS) (file .bashrc):
     #PIG VARS
     export PIG_HOME='/usr/local/pig-0.15.0'
     export PATH=$PIG_HOME/bin:$PATH
     export PIG_CLASSPATH=$HADOOP_INSTALL/etc/hadoop
     #PIG VARS END

Thursday, December 17, 2015

Hadoop ChainReducer example

Below is example of how to use ChainReducer, you can override data at each stage.

(It is important to note that you use ChainReducer.setReducer only once for a job )

To run jar (pack things in jar it is easy) use:
hadoop jar chainingreducer.jar  /user/hduser/wordcount/input /user/hduser/wordcount/output


Below is the flow:
TokenizerMapper (data read from file) --> TokenizerMapper2 (data modified) --> IntSumReducer (Reduced on key and sum calculated, word count) -> TokenizerMapper3 (again data modified)


ChainingReducerClass.java

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.chain.ChainMapper;
import org.apache.hadoop.mapreduce.lib.chain.ChainReducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class ChainingReducerClass {

public static class TokenizerMapper extends
Mapper<Object, Text, TextPair, IntWritable> {

private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
private static TextPair textPair = new TextPair();

public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
Text word2 = new Text();
word2.set("mapper1");
Text word3 = new Text();
word3.set("ggg");
textPair.set(word, word2, word3);
context.write(textPair, one);
}
}
}

public static class TokenizerMapper2 extends
Mapper<TextPair, IntWritable, TextPair, IntWritable> {

private static TextPair textPair = new TextPair();

public void map(TextPair key, IntWritable value, Context context)
throws IOException, InterruptedException {

Text text3 = new Text();
text3.set("fff");
textPair.set(key.getFirst(), key.getSecond(), text3);
context.write(textPair, value);

}

}

public static class IntSumReducer extends
Reducer<TextPair, IntWritable, TextPair, IntWritable> {
private IntWritable result = new IntWritable();

public void reduce(TextPair key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}

public static class TokenizerMapper3 extends
Mapper<TextPair, IntWritable, TextPair, IntWritable> {

private static TextPair textPair = new TextPair();

public void map(TextPair key, IntWritable value, Context context)
throws IOException, InterruptedException {

Text text3 = new Text();
text3.set("333");
textPair.set(key.getFirst(), key.getSecond(), text3);
context.write(textPair, value);

}

}

public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");

// //////////////Here add chaining code for mappers
Configuration mapAConf = new Configuration(false);
ChainMapper.addMapper(job, TokenizerMapper.class, Object.class,
Text.class, TextPair.class, IntWritable.class, mapAConf);

Configuration mapBConf = new Configuration(false);
ChainMapper.addMapper(job, TokenizerMapper2.class, TextPair.class,
IntWritable.class, TextPair.class, IntWritable.class, mapBConf);
// //////////////end of chaining for mappers
job.setJarByClass(ChainingReducerClass.class);

job.setCombinerClass(IntSumReducer.class);

// there can only be one setreducer
ChainReducer.setReducer(job, IntSumReducer.class, TextPair.class,
IntWritable.class, TextPair.class, IntWritable.class,
new Configuration(false));
// reducers results go to the mapper
ChainReducer.addMapper(job, TokenizerMapper3.class, TextPair.class,
IntWritable.class, TextPair.class, IntWritable.class, null);

FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}

}

Few useful Hadoop 2.6.2 commands

Below are few useful Hadoop (2.6.2) commands:

stop-yarn.sh

stop-dfs.sh

hadoop namenode -format

start-dfs.sh

#will list processes running in hadoop
jps

hdfs dfs -mkdir /user

hdfs dfs -mkdir /user/hduser

hdfs dfs -mkdir /user/hduser/wordcount

hdfs dfs -mkdir /user/hduser/wordcount/input

cd /usr/local/hadoop/temp_hduser

#copy file from local to hdfs
hdfs dfs -put input1.txt /user/hduser/wordcount/input/file0
hdfs dfs -put input1.txt /user/hduser/wordcount/input/file1

start-yarn.sh

#For removing dirs if you have
hdfs  dfs -rmr /user/hduser/wordcount/output

#Run your jar will read everything from the input dir, below will not work if Yarn is not started :)
hadoop jar chainingmapper.jar  /user/hduser/wordcount/input /user/hduser/wordcount/output

Wednesday, December 16, 2015

Hadoop ChainMapper Example

Below is example of ChainMapper where input read from file, goes to another mapper and then to reducer with more complex output. It shows you can also override data at stages.


ChainMapperClass.java

import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.chain.ChainMapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class ChainMapperClass {

public static class TokenizerMapper extends
Mapper<Object, Text, TextPair, IntWritable> {

private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
private static TextPair textPair = new TextPair();

public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
Text word2 = new Text();
word2.set("mapper1");
Text word3 = new Text();
word3.set("ggg");
textPair.set(word, word2, word3);
context.write(textPair, one);
}
}
}

public static class TokenizerMapper2 extends
Mapper<TextPair, IntWritable, TextPair, IntWritable> {

private static TextPair textPair = new TextPair();

public void map(TextPair key, IntWritable value, Context context)
throws IOException, InterruptedException {

Text text3 = new Text();
text3.set("fff");
textPair.set(key.getFirst(), key.getSecond(), text3);
context.write(textPair, value);

}

}

public static class IntSumReducer extends
Reducer<TextPair, IntWritable, TextPair, IntWritable> {
private IntWritable result = new IntWritable();

public void reduce(TextPair key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}

public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");

// //////////////Here add chaining code for mappers
Configuration mapAConf = new Configuration(false);
ChainMapper.addMapper(job, TokenizerMapper.class, Object.class,
Text.class, TextPair.class, IntWritable.class, mapAConf);

Configuration mapBConf = new Configuration(false);
ChainMapper.addMapper(job, TokenizerMapper2.class, TextPair.class,
IntWritable.class, TextPair.class, IntWritable.class, mapBConf);
// //////////////end of chaining for mappers
job.setJarByClass(ChainMapperClass.class);

// //job.setMapperClass(TokenizerMapper.class);

job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);

// //job.setMapOutputKeyClass(TextPair.class);
// //job.setMapOutputValueClass(IntWritable.class);

// job.setOutputKeyClass(TextPair.class);
// job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}

}