1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.mapreduce;
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.classification.InterfaceAudience;
26 import org.apache.hadoop.classification.InterfaceStability;
27 import org.apache.hadoop.conf.Configurable;
28 import org.apache.hadoop.conf.Configuration;
29 import org.apache.hadoop.hbase.KeyValue;
30 import org.apache.hadoop.hbase.client.HTable;
31 import org.apache.hadoop.hbase.client.Scan;
32 import org.apache.hadoop.hbase.util.Bytes;
33 import org.apache.hadoop.util.StringUtils;
34
35
36
37
38 @InterfaceAudience.Public
39 @InterfaceStability.Stable
40 public class TableInputFormat extends TableInputFormatBase
41 implements Configurable {
42
43 private final Log LOG = LogFactory.getLog(TableInputFormat.class);
44
45
46 public static final String INPUT_TABLE = "hbase.mapreduce.inputtable";
47
48
49
50 public static final String SCAN = "hbase.mapreduce.scan";
51
52 public static final String SCAN_ROW_START = "hbase.mapreduce.scan.row.start";
53
54 public static final String SCAN_ROW_STOP = "hbase.mapreduce.scan.row.stop";
55
56 public static final String SCAN_COLUMN_FAMILY = "hbase.mapreduce.scan.column.family";
57
58 public static final String SCAN_COLUMNS = "hbase.mapreduce.scan.columns";
59
60 public static final String SCAN_TIMESTAMP = "hbase.mapreduce.scan.timestamp";
61
62 public static final String SCAN_TIMERANGE_START = "hbase.mapreduce.scan.timerange.start";
63
64 public static final String SCAN_TIMERANGE_END = "hbase.mapreduce.scan.timerange.end";
65
66 public static final String SCAN_MAXVERSIONS = "hbase.mapreduce.scan.maxversions";
67
68 public static final String SCAN_CACHEBLOCKS = "hbase.mapreduce.scan.cacheblocks";
69
70 public static final String SCAN_CACHEDROWS = "hbase.mapreduce.scan.cachedrows";
71
72
73 private Configuration conf = null;
74
75
76
77
78
79
80
81 @Override
82 public Configuration getConf() {
83 return conf;
84 }
85
86
87
88
89
90
91
92
93
94 @Override
95 public void setConf(Configuration configuration) {
96 this.conf = configuration;
97 String tableName = conf.get(INPUT_TABLE);
98 try {
99 setHTable(new HTable(new Configuration(conf), tableName));
100 } catch (Exception e) {
101 LOG.error(StringUtils.stringifyException(e));
102 }
103
104 Scan scan = null;
105
106 if (conf.get(SCAN) != null) {
107 try {
108 scan = TableMapReduceUtil.convertStringToScan(conf.get(SCAN));
109 } catch (IOException e) {
110 LOG.error("An error occurred.", e);
111 }
112 } else {
113 try {
114 scan = new Scan();
115
116 if (conf.get(SCAN_ROW_START) != null) {
117 scan.setStartRow(Bytes.toBytes(conf.get(SCAN_ROW_START)));
118 }
119
120 if (conf.get(SCAN_ROW_STOP) != null) {
121 scan.setStopRow(Bytes.toBytes(conf.get(SCAN_ROW_STOP)));
122 }
123
124 if (conf.get(SCAN_COLUMNS) != null) {
125 addColumns(scan, conf.get(SCAN_COLUMNS));
126 }
127
128 if (conf.get(SCAN_COLUMN_FAMILY) != null) {
129 scan.addFamily(Bytes.toBytes(conf.get(SCAN_COLUMN_FAMILY)));
130 }
131
132 if (conf.get(SCAN_TIMESTAMP) != null) {
133 scan.setTimeStamp(Long.parseLong(conf.get(SCAN_TIMESTAMP)));
134 }
135
136 if (conf.get(SCAN_TIMERANGE_START) != null && conf.get(SCAN_TIMERANGE_END) != null) {
137 scan.setTimeRange(
138 Long.parseLong(conf.get(SCAN_TIMERANGE_START)),
139 Long.parseLong(conf.get(SCAN_TIMERANGE_END)));
140 }
141
142 if (conf.get(SCAN_MAXVERSIONS) != null) {
143 scan.setMaxVersions(Integer.parseInt(conf.get(SCAN_MAXVERSIONS)));
144 }
145
146 if (conf.get(SCAN_CACHEDROWS) != null) {
147 scan.setCaching(Integer.parseInt(conf.get(SCAN_CACHEDROWS)));
148 }
149
150
151 scan.setCacheBlocks((conf.getBoolean(SCAN_CACHEBLOCKS, false)));
152 } catch (Exception e) {
153 LOG.error(StringUtils.stringifyException(e));
154 }
155 }
156
157 setScan(scan);
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171 private static void addColumn(Scan scan, byte[] familyAndQualifier) {
172 byte [][] fq = KeyValue.parseColumn(familyAndQualifier);
173 if (fq.length > 1 && fq[1] != null && fq[1].length > 0) {
174 scan.addColumn(fq[0], fq[1]);
175 } else {
176 scan.addFamily(fq[0]);
177 }
178 }
179
180
181
182
183
184
185
186
187 public static void addColumns(Scan scan, byte [][] columns) {
188 for (byte[] column : columns) {
189 addColumn(scan, column);
190 }
191 }
192
193
194
195
196
197
198
199
200
201
202 private static void addColumns(Scan scan, String columns) {
203 String[] cols = columns.split(" ");
204 for (String col : cols) {
205 addColumn(scan, Bytes.toBytes(col));
206 }
207 }
208
209 }