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 org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.hadoop.conf.Configurable;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.fs.FileUtil;
26  import org.apache.hadoop.hbase.Cell;
27  import org.apache.hadoop.hbase.CellUtil;
28  import org.apache.hadoop.hbase.HBaseTestingUtility;
29  import org.apache.hadoop.hbase.HColumnDescriptor;
30  import org.apache.hadoop.hbase.HTableDescriptor;
31  import org.apache.hadoop.hbase.TableName;
32  import org.apache.hadoop.hbase.client.Admin;
33  import org.apache.hadoop.hbase.client.Connection;
34  import org.apache.hadoop.hbase.client.ConnectionFactory;
35  import org.apache.hadoop.hbase.client.Durability;
36  import org.apache.hadoop.hbase.client.Put;
37  import org.apache.hadoop.hbase.client.Result;
38  import org.apache.hadoop.hbase.client.ResultScanner;
39  import org.apache.hadoop.hbase.client.Scan;
40  import org.apache.hadoop.hbase.client.Table;
41  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
42  import org.apache.hadoop.hbase.testclassification.LargeTests;
43  import org.apache.hadoop.hbase.testclassification.MapReduceTests;
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.AfterClass;
50  import org.junit.Before;
51  import org.junit.BeforeClass;
52  import org.junit.Test;
53  import org.junit.experimental.categories.Category;
54  
55  import java.io.File;
56  import java.io.IOException;
57  import java.util.ArrayList;
58  import java.util.List;
59  import java.util.Map;
60  import java.util.NavigableMap;
61  import java.util.TreeMap;
62  
63  @Category({MapReduceTests.class, LargeTests.class})
64  public class TestTimeRangeMapRed {
65    private final static Log log = LogFactory.getLog(TestTimeRangeMapRed.class);
66    private static final HBaseTestingUtility UTIL =
67      new HBaseTestingUtility();
68    private Admin admin;
69  
70    private static final byte [] KEY = Bytes.toBytes("row1");
71    private static final NavigableMap<Long, Boolean> TIMESTAMP =
72      new TreeMap<Long, Boolean>();
73    static {
74      TIMESTAMP.put((long)1245620000, false);
75      TIMESTAMP.put((long)1245620005, true); // include
76      TIMESTAMP.put((long)1245620010, true); // include
77      TIMESTAMP.put((long)1245620055, true); // include
78      TIMESTAMP.put((long)1245620100, true); // include
79      TIMESTAMP.put((long)1245620150, false);
80      TIMESTAMP.put((long)1245620250, false);
81    }
82    static final long MINSTAMP = 1245620005;
83    static final long MAXSTAMP = 1245620100 + 1; // maxStamp itself is excluded. so increment it.
84  
85    static final TableName TABLE_NAME = TableName.valueOf("table123");
86    static final byte[] FAMILY_NAME = Bytes.toBytes("text");
87    static final byte[] COLUMN_NAME = Bytes.toBytes("input");
88  
89    @BeforeClass
90    public static void beforeClass() throws Exception {
91      UTIL.startMiniCluster();
92    }
93  
94    @AfterClass
95    public static void afterClass() throws Exception {
96      UTIL.shutdownMiniCluster();
97    }
98  
99    @Before
100   public void before() throws Exception {
101     this.admin = UTIL.getHBaseAdmin();
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.addColumn(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         Connection connection = ConnectionFactory.createConnection(conf);
140         table = connection.getTable(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.addColumn(FAMILY_NAME, COLUMN_NAME, entry.getKey(), Bytes.toBytes(false));
160       puts.add(put);
161     }
162     Table table = UTIL.getConnection().getTable(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       // TODO Auto-generated catch block
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           Bytes.toBoolean(CellUtil.cloneValue(kv)));
206       }
207     }
208     scanner.close();
209   }
210 
211 }
212