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