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.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.conf.Configurable;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.hbase.HConstants;
29 import org.apache.hadoop.hbase.client.Delete;
30 import org.apache.hadoop.hbase.client.HConnectionManager;
31 import org.apache.hadoop.hbase.client.HTable;
32 import org.apache.hadoop.hbase.client.Put;
33 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
34 import org.apache.hadoop.io.Writable;
35 import org.apache.hadoop.mapreduce.JobContext;
36 import org.apache.hadoop.mapreduce.OutputCommitter;
37 import org.apache.hadoop.mapreduce.OutputFormat;
38 import org.apache.hadoop.mapreduce.RecordWriter;
39 import org.apache.hadoop.mapreduce.TaskAttemptContext;
40
41
42
43
44
45
46
47
48 public class TableOutputFormat<KEY> extends OutputFormat<KEY, Writable>
49 implements Configurable {
50
51 private final Log LOG = LogFactory.getLog(TableOutputFormat.class);
52
53
54 public static final String OUTPUT_TABLE = "hbase.mapred.outputtable";
55
56
57
58
59
60
61
62 public static final String QUORUM_ADDRESS = "hbase.mapred.output.quorum";
63
64
65 public static final String
66 REGION_SERVER_CLASS = "hbase.mapred.output.rs.class";
67
68 public static final String
69 REGION_SERVER_IMPL = "hbase.mapred.output.rs.impl";
70
71
72 private Configuration conf = null;
73
74 private HTable table;
75
76
77
78
79
80
81 protected static class TableRecordWriter<KEY>
82 extends RecordWriter<KEY, Writable> {
83
84
85 private HTable table;
86
87
88
89
90
91
92 public TableRecordWriter(HTable table) {
93 this.table = table;
94 }
95
96
97
98
99
100
101
102
103 @Override
104 public void close(TaskAttemptContext context)
105 throws IOException {
106 table.flushCommits();
107
108
109
110
111
112 HConnectionManager.deleteAllConnections(true);
113 }
114
115
116
117
118
119
120
121
122
123 @Override
124 public void write(KEY key, Writable value)
125 throws IOException {
126 if (value instanceof Put) this.table.put(new Put((Put)value));
127 else if (value instanceof Delete) this.table.delete(new Delete((Delete)value));
128 else throw new IOException("Pass a Delete or a Put");
129 }
130 }
131
132
133
134
135
136
137
138
139
140
141 @Override
142 public RecordWriter<KEY, Writable> getRecordWriter(
143 TaskAttemptContext context)
144 throws IOException, InterruptedException {
145 return new TableRecordWriter<KEY>(this.table);
146 }
147
148
149
150
151
152
153
154
155
156 @Override
157 public void checkOutputSpecs(JobContext context) throws IOException,
158 InterruptedException {
159
160
161 }
162
163
164
165
166
167
168
169
170
171
172 @Override
173 public OutputCommitter getOutputCommitter(TaskAttemptContext context)
174 throws IOException, InterruptedException {
175 return new TableOutputCommitter();
176 }
177
178 public Configuration getConf() {
179 return conf;
180 }
181
182 @Override
183 public void setConf(Configuration conf) {
184 String tableName = conf.get(OUTPUT_TABLE);
185 String address = conf.get(QUORUM_ADDRESS);
186 String serverClass = conf.get(REGION_SERVER_CLASS);
187 String serverImpl = conf.get(REGION_SERVER_IMPL);
188 try {
189 if (address != null) {
190 ZKUtil.applyClusterKeyToConf(conf, address);
191 }
192 if (serverClass != null) {
193 conf.set(HConstants.REGION_SERVER_CLASS, serverClass);
194 conf.set(HConstants.REGION_SERVER_IMPL, serverImpl);
195 }
196 this.table = new HTable(conf, tableName);
197 table.setAutoFlush(false);
198 LOG.info("Created table instance for " + tableName);
199 } catch(IOException e) {
200 LOG.error(e);
201 }
202 }
203 }