[21] | 1 | /** |
---|
| 2 | * Program: DemoHBaseSink.java |
---|
| 3 | * Editor: Waue Chen |
---|
| 4 | * From : NCHC. Taiwn |
---|
| 5 | * Last Update Date: 07/02/2008 |
---|
| 6 | * Re-code from : Cloud9: A MapReduce Library for Hadoop |
---|
| 7 | */ |
---|
| 8 | /* |
---|
| 9 | * Cloud9: A MapReduce Library for Hadoop |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | package tw.org.nchc.demo; |
---|
| 13 | |
---|
| 14 | import java.io.IOException; |
---|
| 15 | import java.util.Iterator; |
---|
| 16 | |
---|
| 17 | import org.apache.hadoop.fs.Path; |
---|
| 18 | import org.apache.hadoop.hbase.io.ImmutableBytesWritable; |
---|
| 19 | import org.apache.hadoop.hbase.mapred.TableReduce; |
---|
| 20 | import org.apache.hadoop.io.LongWritable; |
---|
| 21 | import org.apache.hadoop.io.MapWritable; |
---|
| 22 | import org.apache.hadoop.io.Text; |
---|
| 23 | import org.apache.hadoop.mapred.JobClient; |
---|
| 24 | import org.apache.hadoop.mapred.JobConf; |
---|
| 25 | import org.apache.hadoop.mapred.OutputCollector; |
---|
| 26 | import org.apache.hadoop.mapred.Reporter; |
---|
| 27 | import org.apache.hadoop.mapred.lib.IdentityMapper; |
---|
| 28 | import org.apache.hadoop.mapred.lib.IdentityReducer; |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * |
---|
| 32 | */ |
---|
| 33 | public class DemoHBaseSink { |
---|
| 34 | |
---|
| 35 | private static class ReduceClass extends TableReduce<LongWritable, Text> { |
---|
| 36 | |
---|
| 37 | // this is the column we're going to be writing |
---|
| 38 | private static final Text col = new Text("default:text"); |
---|
| 39 | |
---|
| 40 | // this map holds the columns per row |
---|
| 41 | private MapWritable map = new MapWritable(); |
---|
| 42 | |
---|
| 43 | public void reduce(LongWritable key, Iterator<Text> values, |
---|
| 44 | OutputCollector<Text, MapWritable> output, Reporter reporter) |
---|
| 45 | throws IOException { |
---|
| 46 | |
---|
| 47 | // contents must be ImmutableBytesWritable |
---|
| 48 | ImmutableBytesWritable bytes = new ImmutableBytesWritable(values |
---|
| 49 | .next().getBytes()); |
---|
| 50 | |
---|
| 51 | // populate the current row |
---|
| 52 | map.clear(); |
---|
| 53 | map.put(col, bytes); |
---|
| 54 | |
---|
| 55 | // add the row with the key as the row id |
---|
| 56 | output.collect(new Text(key.toString()), map); |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | private DemoHBaseSink() { |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * Runs the demo. |
---|
| 65 | */ |
---|
| 66 | public static void main(String[] args) throws IOException { |
---|
| 67 | String filename = "/shared/sample"; |
---|
| 68 | |
---|
| 69 | int mapTasks = 1; |
---|
| 70 | int reduceTasks = 1; |
---|
| 71 | |
---|
| 72 | JobConf conf = new JobConf(DemoHBaseSink.class); |
---|
| 73 | conf.setJobName("wordcount"); |
---|
| 74 | |
---|
| 75 | // must initialize the TableReduce before running job |
---|
| 76 | TableReduce.initJob("test", ReduceClass.class, conf); |
---|
| 77 | |
---|
| 78 | conf.setNumMapTasks(mapTasks); |
---|
| 79 | conf.setNumReduceTasks(reduceTasks); |
---|
[27] | 80 | conf.setInputPath(new Path(filename)); |
---|
[21] | 81 | conf.setMapperClass(IdentityMapper.class); |
---|
| 82 | conf.setCombinerClass(IdentityReducer.class); |
---|
| 83 | conf.setReducerClass(ReduceClass.class); |
---|
| 84 | |
---|
| 85 | JobClient.runJob(conf); |
---|
| 86 | } |
---|
| 87 | } |
---|