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