1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.mapreduce;
21
22 import java.io.IOException;
23
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.hbase.HBaseConfiguration;
26 import org.apache.hadoop.hbase.KeyValue;
27 import org.apache.hadoop.hbase.client.Result;
28 import org.apache.hadoop.hbase.client.Scan;
29 import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter;
30 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
31 import org.apache.hadoop.hbase.util.Bytes;
32 import org.apache.hadoop.mapreduce.Job;
33 import org.apache.hadoop.mapreduce.lib.output.NullOutputFormat;
34 import org.apache.hadoop.util.GenericOptionsParser;
35
36
37
38
39
40 public class RowCounter {
41
42
43 static final String NAME = "rowcounter";
44
45
46
47
48 static class RowCounterMapper
49 extends TableMapper<ImmutableBytesWritable, Result> {
50
51
52 public static enum Counters {ROWS}
53
54
55
56
57
58
59
60
61
62
63
64 @Override
65 public void map(ImmutableBytesWritable row, Result values,
66 Context context)
67 throws IOException {
68
69 context.getCounter(Counters.ROWS).increment(1);
70 }
71 }
72
73
74
75
76
77
78
79
80
81 public static Job createSubmittableJob(Configuration conf, String[] args)
82 throws IOException {
83 String tableName = args[0];
84 Job job = new Job(conf, NAME + "_" + tableName);
85 job.setJarByClass(RowCounter.class);
86
87 StringBuilder sb = new StringBuilder();
88 final int columnoffset = 1;
89 for (int i = columnoffset; i < args.length; i++) {
90 if (i > columnoffset) {
91 sb.append(" ");
92 }
93 sb.append(args[i]);
94 }
95 Scan scan = new Scan();
96 scan.setFilter(new FirstKeyOnlyFilter());
97 if (sb.length() > 0) {
98 for (String columnName :sb.toString().split(" ")) {
99 String [] fields = columnName.split(":");
100 if(fields.length == 1) {
101 scan.addFamily(Bytes.toBytes(fields[0]));
102 } else {
103 scan.addColumn(Bytes.toBytes(fields[0]), Bytes.toBytes(fields[1]));
104 }
105 }
106 }
107
108 job.setOutputFormatClass(NullOutputFormat.class);
109 TableMapReduceUtil.initTableMapperJob(tableName, scan,
110 RowCounterMapper.class, ImmutableBytesWritable.class, Result.class, job);
111 job.setNumReduceTasks(0);
112 return job;
113 }
114
115
116
117
118
119
120
121 public static void main(String[] args) throws Exception {
122 Configuration conf = HBaseConfiguration.create();
123 String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
124 if (otherArgs.length < 1) {
125 System.err.println("ERROR: Wrong number of parameters: " + args.length);
126 System.err.println("Usage: RowCounter <tablename> [<column1> <column2>...]");
127 System.exit(-1);
128 }
129 Job job = createSubmittableJob(conf, otherArgs);
130 System.exit(job.waitForCompletion(true) ? 0 : 1);
131 }
132 }