1   /**
2    * Copyright 2007 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.mapreduce;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.NavigableMap;
28  import java.util.TreeMap;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.hadoop.conf.Configurable;
33  import org.apache.hadoop.conf.Configuration;
34  import org.apache.hadoop.fs.FileUtil;
35  import org.apache.hadoop.hbase.HBaseConfiguration;
36  import org.apache.hadoop.hbase.HBaseTestingUtility;
37  import org.apache.hadoop.hbase.HColumnDescriptor;
38  import org.apache.hadoop.hbase.HTableDescriptor;
39  import org.apache.hadoop.hbase.KeyValue;
40  import org.apache.hadoop.hbase.MasterNotRunningException;
41  import org.apache.hadoop.hbase.ZooKeeperConnectionException;
42  import org.apache.hadoop.hbase.client.HBaseAdmin;
43  import org.apache.hadoop.hbase.client.HTable;
44  import org.apache.hadoop.hbase.client.Put;
45  import org.apache.hadoop.hbase.client.Result;
46  import org.apache.hadoop.hbase.client.ResultScanner;
47  import org.apache.hadoop.hbase.client.Scan;
48  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
49  import org.apache.hadoop.hbase.util.Bytes;
50  import org.apache.hadoop.io.MapWritable;
51  import org.apache.hadoop.io.Text;
52  import org.apache.hadoop.mapreduce.Job;
53  import org.apache.hadoop.mapreduce.lib.output.NullOutputFormat;
54  import org.junit.After;
55  import org.junit.AfterClass;
56  import org.junit.Before;
57  import org.junit.BeforeClass;
58  import org.junit.Test;
59  
60  public class TestTimeRangeMapRed {
61    private final static Log log = LogFactory.getLog(TestTimeRangeMapRed.class);
62    private static final HBaseTestingUtility UTIL =
63      new HBaseTestingUtility();
64    private HBaseAdmin admin;
65  
66    private static final byte [] KEY = Bytes.toBytes("row1");
67    private static final NavigableMap<Long, Boolean> TIMESTAMP =
68      new TreeMap<Long, Boolean>();
69    static {
70      TIMESTAMP.put((long)1245620000, false);
71      TIMESTAMP.put((long)1245620005, true); // include
72      TIMESTAMP.put((long)1245620010, true); // include
73      TIMESTAMP.put((long)1245620055, true); // include
74      TIMESTAMP.put((long)1245620100, true); // include
75      TIMESTAMP.put((long)1245620150, false);
76      TIMESTAMP.put((long)1245620250, false);
77    }
78    static final long MINSTAMP = 1245620005;
79    static final long MAXSTAMP = 1245620100 + 1; // maxStamp itself is excluded. so increment it.
80  
81    static final byte[] TABLE_NAME = Bytes.toBytes("table123");
82    static final byte[] FAMILY_NAME = Bytes.toBytes("text");
83    static final byte[] COLUMN_NAME = Bytes.toBytes("input");
84  
85    @BeforeClass
86    public static void beforeClass() throws Exception {
87      System.setProperty("hadoop.log.dir",
88        UTIL.getConfiguration().get("hadoop.log.dir"));
89      UTIL.getConfiguration().set("mapred.output.dir",
90        UTIL.getConfiguration().get("hadoop.tmp.dir"));
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 MasterNotRunningException, ZooKeeperConnectionException {
101     this.admin = new HBaseAdmin(UTIL.getConfiguration());
102   }
103 
104   @After
105   public void after() throws IOException {
106     this.admin.close();
107   }
108 
109   private static class ProcessTimeRangeMapper
110   extends TableMapper<ImmutableBytesWritable, MapWritable>
111   implements Configurable {
112 
113     private Configuration conf = null;
114     private HTable table = null;
115 
116     @Override
117     public void map(ImmutableBytesWritable key, Result result,
118         Context context)
119     throws IOException {
120       List<Long> tsList = new ArrayList<Long>();
121       for (KeyValue kv : result.list()) {
122         tsList.add(kv.getTimestamp());
123       }
124 
125       for (Long ts : tsList) {
126         Put put = new Put(key.get());
127         put.setWriteToWAL(false);
128         put.add(FAMILY_NAME, COLUMN_NAME, ts, Bytes.toBytes(true));
129         table.put(put);
130       }
131       table.flushCommits();
132     }
133 
134     @Override
135     public Configuration getConf() {
136       return conf;
137     }
138 
139     @Override
140     public void setConf(Configuration configuration) {
141       this.conf = configuration;
142       try {
143         table = new HTable(HBaseConfiguration.create(conf), TABLE_NAME);
144       } catch (IOException e) {
145         e.printStackTrace();
146       }
147     }
148   }
149 
150   @Test
151   public void testTimeRangeMapRed()
152   throws IOException, InterruptedException, ClassNotFoundException {
153     final HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);
154     final HColumnDescriptor col = new HColumnDescriptor(FAMILY_NAME);
155     col.setMaxVersions(Integer.MAX_VALUE);
156     desc.addFamily(col);
157     admin.createTable(desc);
158     HTable table = new HTable(UTIL.getConfiguration(), desc.getName());
159     prepareTest(table);
160     runTestOnTable();
161     verify(table);
162   }
163 
164   private void prepareTest(final HTable table) throws IOException {
165     for (Map.Entry<Long, Boolean> entry : TIMESTAMP.entrySet()) {
166       Put put = new Put(KEY);
167       put.setWriteToWAL(false);
168       put.add(FAMILY_NAME, COLUMN_NAME, entry.getKey(), Bytes.toBytes(false));
169       table.put(put);
170     }
171     table.flushCommits();
172   }
173 
174   private void runTestOnTable()
175   throws IOException, InterruptedException, ClassNotFoundException {
176     UTIL.startMiniMapReduceCluster(1);
177     Job job = null;
178     try {
179       job = new Job(UTIL.getConfiguration(), "test123");
180       job.setOutputFormatClass(NullOutputFormat.class);
181       job.setNumReduceTasks(0);
182       Scan scan = new Scan();
183       scan.addColumn(FAMILY_NAME, COLUMN_NAME);
184       scan.setTimeRange(MINSTAMP, MAXSTAMP);
185       scan.setMaxVersions();
186       TableMapReduceUtil.initTableMapperJob(Bytes.toString(TABLE_NAME),
187         scan, ProcessTimeRangeMapper.class, Text.class, Text.class, job);
188       job.waitForCompletion(true);
189     } catch (IOException e) {
190       // TODO Auto-generated catch block
191       e.printStackTrace();
192     } finally {
193       UTIL.shutdownMiniMapReduceCluster();
194       if (job != null) {
195         FileUtil.fullyDelete(
196           new File(job.getConfiguration().get("hadoop.tmp.dir")));
197       }
198     }
199   }
200 
201   private void verify(final HTable table) throws IOException {
202     Scan scan = new Scan();
203     scan.addColumn(FAMILY_NAME, COLUMN_NAME);
204     scan.setMaxVersions(1);
205     ResultScanner scanner = table.getScanner(scan);
206     for (Result r: scanner) {
207       for (KeyValue kv : r.list()) {
208         log.debug(Bytes.toString(r.getRow()) + "\t" + Bytes.toString(kv.getFamily())
209             + "\t" + Bytes.toString(kv.getQualifier())
210             + "\t" + kv.getTimestamp() + "\t" + Bytes.toBoolean(kv.getValue()));
211         org.junit.Assert.assertEquals(TIMESTAMP.get(kv.getTimestamp()),
212           (Boolean)Bytes.toBoolean(kv.getValue()));
213       }
214     }
215     scanner.close();
216   }
217 }