1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.mapred;
20
21 import java.io.IOException;
22
23 import org.apache.hadoop.hbase.classification.InterfaceAudience;
24 import org.apache.hadoop.hbase.classification.InterfaceStability;
25 import org.apache.hadoop.conf.Configured;
26 import org.apache.hadoop.fs.Path;
27 import org.apache.hadoop.hbase.HBaseConfiguration;
28 import org.apache.hadoop.hbase.client.Result;
29 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
30 import org.apache.hadoop.mapred.FileOutputFormat;
31 import org.apache.hadoop.mapred.JobClient;
32 import org.apache.hadoop.mapred.JobConf;
33 import org.apache.hadoop.mapred.OutputCollector;
34 import org.apache.hadoop.mapred.Reporter;
35 import org.apache.hadoop.mapred.lib.IdentityReducer;
36 import org.apache.hadoop.util.Tool;
37 import org.apache.hadoop.util.ToolRunner;
38
39
40
41
42
43
44 @Deprecated
45 @InterfaceAudience.Public
46 @InterfaceStability.Stable
47 public class RowCounter extends Configured implements Tool {
48
49 static final String NAME = "rowcounter";
50
51
52
53
54 static class RowCounterMapper
55 implements TableMap<ImmutableBytesWritable, Result> {
56 private static enum Counters {ROWS}
57
58 public void map(ImmutableBytesWritable row, Result values,
59 OutputCollector<ImmutableBytesWritable, Result> output,
60 Reporter reporter)
61 throws IOException {
62
63 reporter.incrCounter(Counters.ROWS, 1);
64 }
65
66 public void configure(JobConf jc) {
67
68 }
69
70 public void close() throws IOException {
71
72 }
73 }
74
75
76
77
78
79
80 public JobConf createSubmittableJob(String[] args) throws IOException {
81 JobConf c = new JobConf(getConf(), getClass());
82 c.setJobName(NAME);
83
84 StringBuilder sb = new StringBuilder();
85 final int columnoffset = 2;
86 for (int i = columnoffset; i < args.length; i++) {
87 if (i > columnoffset) {
88 sb.append(" ");
89 }
90 sb.append(args[i]);
91 }
92
93 TableMapReduceUtil.initTableMapJob(args[1], sb.toString(),
94 RowCounterMapper.class, ImmutableBytesWritable.class, Result.class, c);
95 c.setNumReduceTasks(0);
96
97 FileOutputFormat.setOutputPath(c, new Path(args[0]));
98 return c;
99 }
100
101 static int printUsage() {
102 System.out.println(NAME +
103 " <outputdir> <tablename> <column1> [<column2>...]");
104 return -1;
105 }
106
107 public int run(final String[] args) throws Exception {
108
109 if (args.length < 3) {
110 System.err.println("ERROR: Wrong number of parameters: " + args.length);
111 return printUsage();
112 }
113 JobClient.runJob(createSubmittableJob(args));
114 return 0;
115 }
116
117
118
119
120
121 public static void main(String[] args) throws Exception {
122 int errCode = ToolRunner.run(HBaseConfiguration.create(), new RowCounter(), args);
123 System.exit(errCode);
124 }
125 }