View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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); // include
71      TIMESTAMP.put((long)1245620010, true); // include
72      TIMESTAMP.put((long)1245620055, true); // include
73      TIMESTAMP.put((long)1245620100, true); // include
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; // maxStamp itself is excluded. so increment it.
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.startMiniCluster();
87    }
88  
89    @AfterClass
90    public static void afterClass() throws Exception {
91      UTIL.shutdownMiniCluster();
92    }
93  
94    @Before
95    public void before() throws Exception {
96      this.admin = new HBaseAdmin(UTIL.getConfiguration());
97    }
98  
99    @After
100   public void after() throws IOException {
101     this.admin.close();
102   }
103 
104   private static class ProcessTimeRangeMapper
105   extends TableMapper<ImmutableBytesWritable, MapWritable>
106   implements Configurable {
107 
108     private Configuration conf = null;
109     private Table table = null;
110 
111     @Override
112     public void map(ImmutableBytesWritable key, Result result,
113         Context context)
114     throws IOException {
115       List<Long> tsList = new ArrayList<Long>();
116       for (Cell kv : result.listCells()) {
117         tsList.add(kv.getTimestamp());
118       }
119 
120       List<Put> puts = new ArrayList<>();
121       for (Long ts : tsList) {
122         Put put = new Put(key.get());
123         put.setDurability(Durability.SKIP_WAL);
124         put.add(FAMILY_NAME, COLUMN_NAME, ts, Bytes.toBytes(true));
125         puts.add(put);
126       }
127       table.put(puts);
128     }
129 
130     @Override
131     public Configuration getConf() {
132       return conf;
133     }
134 
135     @Override
136     public void setConf(Configuration configuration) {
137       this.conf = configuration;
138       try {
139         table = new HTable(HBaseConfiguration.create(conf), TABLE_NAME);
140       } catch (IOException e) {
141         e.printStackTrace();
142       }
143     }
144   }
145 
146   @Test
147   public void testTimeRangeMapRed()
148   throws IOException, InterruptedException, ClassNotFoundException {
149     final HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);
150     final HColumnDescriptor col = new HColumnDescriptor(FAMILY_NAME);
151     col.setMaxVersions(Integer.MAX_VALUE);
152     desc.addFamily(col);
153     admin.createTable(desc);
154     List<Put> puts = new ArrayList<Put>();
155     for (Map.Entry<Long, Boolean> entry : TIMESTAMP.entrySet()) {
156       Put put = new Put(KEY);
157       put.setDurability(Durability.SKIP_WAL);
158       put.add(FAMILY_NAME, COLUMN_NAME, entry.getKey(), Bytes.toBytes(false));
159       puts.add(put);
160     }
161     Table table = new HTable(UTIL.getConfiguration(), desc.getTableName());
162     table.put(puts);
163     runTestOnTable();
164     verify(table);
165     table.close();
166   }
167 
168   private void runTestOnTable()
169   throws IOException, InterruptedException, ClassNotFoundException {
170     UTIL.startMiniMapReduceCluster();
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       // TODO Auto-generated catch block
185       e.printStackTrace();
186     } finally {
187       UTIL.shutdownMiniMapReduceCluster();
188       if (job != null) {
189         FileUtil.fullyDelete(
190           new File(job.getConfiguration().get("hadoop.tmp.dir")));
191       }
192     }
193   }
194 
195   private void verify(final Table table) throws IOException {
196     Scan scan = new Scan();
197     scan.addColumn(FAMILY_NAME, COLUMN_NAME);
198     scan.setMaxVersions(1);
199     ResultScanner scanner = table.getScanner(scan);
200     for (Result r: scanner) {
201       for (Cell kv : r.listCells()) {
202         log.debug(Bytes.toString(r.getRow()) + "\t" + Bytes.toString(CellUtil.cloneFamily(kv))
203             + "\t" + Bytes.toString(CellUtil.cloneQualifier(kv))
204             + "\t" + kv.getTimestamp() + "\t" + Bytes.toBoolean(CellUtil.cloneValue(kv)));
205         org.junit.Assert.assertEquals(TIMESTAMP.get(kv.getTimestamp()),
206           (Boolean)Bytes.toBoolean(CellUtil.cloneValue(kv)));
207       }
208     }
209     scanner.close();
210   }
211 
212 }
213