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.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.fs.FileSystem;
26 import org.apache.hadoop.hbase.HBaseConfiguration;
27 import org.apache.hadoop.hbase.client.HTable;
28 import org.apache.hadoop.hbase.client.Put;
29 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
30 import org.apache.hadoop.fs.FileAlreadyExistsException;
31 import org.apache.hadoop.mapred.InvalidJobConfException;
32 import org.apache.hadoop.mapred.JobConf;
33 import org.apache.hadoop.mapred.FileOutputFormat;
34 import org.apache.hadoop.mapred.RecordWriter;
35 import org.apache.hadoop.mapred.Reporter;
36 import org.apache.hadoop.util.Progressable;
37
38
39
40
41 @Deprecated
42 public class TableOutputFormat extends
43 FileOutputFormat<ImmutableBytesWritable, Put> {
44
45
46 public static final String OUTPUT_TABLE = "hbase.mapred.outputtable";
47 private final Log LOG = LogFactory.getLog(TableOutputFormat.class);
48
49
50
51
52
53 protected static class TableRecordWriter
54 implements RecordWriter<ImmutableBytesWritable, Put> {
55 private HTable m_table;
56
57
58
59
60
61
62 public TableRecordWriter(HTable table) {
63 m_table = table;
64 }
65
66 public void close(Reporter reporter)
67 throws IOException {
68 m_table.close();
69 }
70
71 public void write(ImmutableBytesWritable key,
72 Put value) throws IOException {
73 m_table.put(new Put(value));
74 }
75 }
76
77 @Override
78 @SuppressWarnings("unchecked")
79 public RecordWriter getRecordWriter(FileSystem ignored,
80 JobConf job, String name, Progressable progress) throws IOException {
81
82
83
84 String tableName = job.get(OUTPUT_TABLE);
85 HTable table = null;
86 try {
87 table = new HTable(HBaseConfiguration.create(job), tableName);
88 } catch(IOException e) {
89 LOG.error(e);
90 throw e;
91 }
92 table.setAutoFlush(false);
93 return new TableRecordWriter(table);
94 }
95
96 @Override
97 public void checkOutputSpecs(FileSystem ignored, JobConf job)
98 throws FileAlreadyExistsException, InvalidJobConfException, IOException {
99
100 String tableName = job.get(OUTPUT_TABLE);
101 if(tableName == null) {
102 throw new IOException("Must specify table name");
103 }
104 }
105 }