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.File;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.NavigableMap;
27 import java.util.TreeMap;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.conf.Configurable;
32 import org.apache.hadoop.conf.Configuration;
33 import org.apache.hadoop.fs.FileUtil;
34 import org.apache.hadoop.hbase.*;
35 import org.apache.hadoop.hbase.client.HBaseAdmin;
36 import org.apache.hadoop.hbase.client.HTable;
37 import org.apache.hadoop.hbase.client.Put;
38 import org.apache.hadoop.hbase.client.Result;
39 import org.apache.hadoop.hbase.client.ResultScanner;
40 import org.apache.hadoop.hbase.client.Scan;
41 import org.apache.hadoop.hbase.client.Durability;
42 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
43 import org.apache.hadoop.hbase.testclassification.LargeTests;
44 import org.apache.hadoop.hbase.util.Bytes;
45 import org.apache.hadoop.io.MapWritable;
46 import org.apache.hadoop.io.Text;
47 import org.apache.hadoop.mapreduce.Job;
48 import org.apache.hadoop.mapreduce.lib.output.NullOutputFormat;
49 import org.junit.After;
50 import org.junit.AfterClass;
51 import org.junit.Before;
52 import org.junit.BeforeClass;
53 import org.junit.Test;
54 import org.junit.experimental.categories.Category;
55
56 @Category(LargeTests.class)
57 public class TestTimeRangeMapRed {
58 private final static Log log = LogFactory.getLog(TestTimeRangeMapRed.class);
59 private static final HBaseTestingUtility UTIL =
60 new HBaseTestingUtility();
61 private HBaseAdmin admin;
62
63 private static final byte [] KEY = Bytes.toBytes("row1");
64 private static final NavigableMap<Long, Boolean> TIMESTAMP =
65 new TreeMap<Long, Boolean>();
66 static {
67 TIMESTAMP.put((long)1245620000, false);
68 TIMESTAMP.put((long)1245620005, true);
69 TIMESTAMP.put((long)1245620010, true);
70 TIMESTAMP.put((long)1245620055, true);
71 TIMESTAMP.put((long)1245620100, true);
72 TIMESTAMP.put((long)1245620150, false);
73 TIMESTAMP.put((long)1245620250, false);
74 }
75 static final long MINSTAMP = 1245620005;
76 static final long MAXSTAMP = 1245620100 + 1;
77
78 static final byte[] TABLE_NAME = Bytes.toBytes("table123");
79 static final byte[] FAMILY_NAME = Bytes.toBytes("text");
80 static final byte[] COLUMN_NAME = Bytes.toBytes("input");
81
82 @BeforeClass
83 public static void beforeClass() throws Exception {
84 UTIL.startMiniCluster();
85 }
86
87 @AfterClass
88 public static void afterClass() throws Exception {
89 UTIL.shutdownMiniCluster();
90 }
91
92 @Before
93 public void before() throws Exception {
94 this.admin = new HBaseAdmin(UTIL.getConfiguration());
95 }
96
97 @After
98 public void after() throws IOException {
99 this.admin.close();
100 }
101
102 private static class ProcessTimeRangeMapper
103 extends TableMapper<ImmutableBytesWritable, MapWritable>
104 implements Configurable {
105
106 private Configuration conf = null;
107 private HTable table = null;
108
109 @Override
110 public void map(ImmutableBytesWritable key, Result result,
111 Context context)
112 throws IOException {
113 List<Long> tsList = new ArrayList<Long>();
114 for (Cell kv : result.listCells()) {
115 tsList.add(kv.getTimestamp());
116 }
117
118 for (Long ts : tsList) {
119 Put put = new Put(key.get());
120 put.setDurability(Durability.SKIP_WAL);
121 put.add(FAMILY_NAME, COLUMN_NAME, ts, Bytes.toBytes(true));
122 table.put(put);
123 }
124 table.flushCommits();
125 }
126
127 @Override
128 public Configuration getConf() {
129 return conf;
130 }
131
132 @Override
133 public void setConf(Configuration configuration) {
134 this.conf = configuration;
135 try {
136 table = new HTable(HBaseConfiguration.create(conf), TABLE_NAME);
137 } catch (IOException e) {
138 e.printStackTrace();
139 }
140 }
141 }
142
143 @Test
144 public void testTimeRangeMapRed()
145 throws IOException, InterruptedException, ClassNotFoundException {
146 final HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
147 final HColumnDescriptor col = new HColumnDescriptor(FAMILY_NAME);
148 col.setMaxVersions(Integer.MAX_VALUE);
149 desc.addFamily(col);
150 admin.createTable(desc);
151 HTable table = new HTable(UTIL.getConfiguration(), desc.getTableName());
152 prepareTest(table);
153 runTestOnTable();
154 verify(table);
155 }
156
157 private void prepareTest(final HTable table) throws IOException {
158 for (Map.Entry<Long, Boolean> entry : TIMESTAMP.entrySet()) {
159 Put put = new Put(KEY);
160 put.setDurability(Durability.SKIP_WAL);
161 put.add(FAMILY_NAME, COLUMN_NAME, entry.getKey(), Bytes.toBytes(false));
162 table.put(put);
163 }
164 table.flushCommits();
165 }
166
167 private void runTestOnTable()
168 throws IOException, InterruptedException, ClassNotFoundException {
169 UTIL.startMiniMapReduceCluster();
170 Job job = null;
171 try {
172 job = new Job(UTIL.getConfiguration(), "test123");
173 job.setOutputFormatClass(NullOutputFormat.class);
174 job.setNumReduceTasks(0);
175 Scan scan = new Scan();
176 scan.addColumn(FAMILY_NAME, COLUMN_NAME);
177 scan.setTimeRange(MINSTAMP, MAXSTAMP);
178 scan.setMaxVersions();
179 TableMapReduceUtil.initTableMapperJob(Bytes.toString(TABLE_NAME),
180 scan, ProcessTimeRangeMapper.class, Text.class, Text.class, job);
181 job.waitForCompletion(true);
182 } catch (IOException e) {
183
184 e.printStackTrace();
185 } finally {
186 UTIL.shutdownMiniMapReduceCluster();
187 if (job != null) {
188 FileUtil.fullyDelete(
189 new File(job.getConfiguration().get("hadoop.tmp.dir")));
190 }
191 }
192 }
193
194 private void verify(final HTable table) throws IOException {
195 Scan scan = new Scan();
196 scan.addColumn(FAMILY_NAME, COLUMN_NAME);
197 scan.setMaxVersions(1);
198 ResultScanner scanner = table.getScanner(scan);
199 for (Result r: scanner) {
200 for (Cell kv : r.listCells()) {
201 log.debug(Bytes.toString(r.getRow()) + "\t" + Bytes.toString(CellUtil.cloneFamily(kv))
202 + "\t" + Bytes.toString(CellUtil.cloneQualifier(kv))
203 + "\t" + kv.getTimestamp() + "\t" + Bytes.toBoolean(CellUtil.cloneValue(kv)));
204 org.junit.Assert.assertEquals(TIMESTAMP.get(kv.getTimestamp()),
205 (Boolean)Bytes.toBoolean(CellUtil.cloneValue(kv)));
206 }
207 }
208 scanner.close();
209 }
210
211 }
212