Skip to content

Instantly share code, notes, and snippets.

@rishav-rohit
Created September 16, 2014 07:24
Show Gist options
  • Save rishav-rohit/5b9d4ca32cafaa857bdd to your computer and use it in GitHub Desktop.
Save rishav-rohit/5b9d4ca32cafaa857bdd to your computer and use it in GitHub Desktop.
HBase multi table input union example reducer
package com.rishav.hbase.union;
import java.io.IOException;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
public class UnionReducer extends TableReducer<Text, IntWritable, ImmutableBytesWritable>{
@Override
public void reduce(Text key, Iterable<IntWritable> values, Context context) {
if (key.toString().startsWith("s")) {
Integer dayStoreSales = 0;
for (IntWritable storeSale : values) {
dayStoreSales = dayStoreSales + new Integer(storeSale.toString());
}
Put put = new Put(Bytes.toBytes(key.toString()));
put.add(Bytes.toBytes("cf1"), Bytes.toBytes("tSales"), Bytes.toBytes(dayStoreSales));
try {
context.write(null, put);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Integer dayStoreSales = 0;
for (IntWritable storeSale : values) {
dayStoreSales = dayStoreSales + new Integer(storeSale.toString());
}
Put put = new Put(Bytes.toBytes(key.toString()));
put.add(Bytes.toBytes("cf1"), Bytes.toBytes("tSales"), Bytes.toBytes(dayStoreSales));
try {
context.write(null, put);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment