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 boolean content = false;
62
63 for (KeyValue value: values.list()) {
64 if (value.getValue().length > 0) {
65 content = true;
66 break;
67 }
68 }
69 if (!content) {
70
71 return;
72 }
73
74 reporter.incrCounter(Counters.ROWS, 1);
75 }
76
77 public void configure(JobConf jc) {
78
79 }
80
81 public void close() throws IOException {
82
83 }
84 }
85
86
87
88
89
90
91 public JobConf createSubmittableJob(String[] args) throws IOException {
92 JobConf c = new JobConf(getConf(), getClass());
93 c.setJobName(NAME);
94
95 StringBuilder sb = new StringBuilder();
96 final int columnoffset = 2;
97 for (int i = columnoffset; i < args.length; i++) {
98 if (i > columnoffset) {
99 sb.append(" ");
100 }
101 sb.append(args[i]);
102 }
103
104 TableMapReduceUtil.initTableMapJob(args[1], sb.toString(),
105 RowCounterMapper.class, ImmutableBytesWritable.class, Result.class, c);
106 c.setNumReduceTasks(0);
107
108 FileOutputFormat.setOutputPath(c, new Path(args[0]));
109 return c;
110 }
111
112 static int printUsage() {
113 System.out.println(NAME +
114 " <outputdir> <tablename> <column1> [<column2>...]");
115 return -1;
116 }
117
118 public int run(final String[] args) throws Exception {
119
120 if (args.length < 3) {
121 System.err.println("ERROR: Wrong number of parameters: " + args.length);
122 return printUsage();
123 }
124 JobClient.runJob(createSubmittableJob(args));
125 return 0;
126 }
127
128
129
130
131
132 public static void main(String[] args) throws Exception {
133 int errCode = ToolRunner.run(HBaseConfiguration.create(), new RowCounter(), args);
134 System.exit(errCode);
135 }
136 }