001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.mapreduce;
019
020import java.io.File;
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.List;
024import java.util.Map;
025import java.util.NavigableMap;
026import java.util.TreeMap;
027import org.apache.hadoop.conf.Configurable;
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.fs.FileUtil;
030import org.apache.hadoop.hbase.Cell;
031import org.apache.hadoop.hbase.CellUtil;
032import org.apache.hadoop.hbase.HBaseClassTestRule;
033import org.apache.hadoop.hbase.HBaseTestingUtil;
034import org.apache.hadoop.hbase.TableName;
035import org.apache.hadoop.hbase.client.Admin;
036import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
037import org.apache.hadoop.hbase.client.Connection;
038import org.apache.hadoop.hbase.client.ConnectionFactory;
039import org.apache.hadoop.hbase.client.Durability;
040import org.apache.hadoop.hbase.client.Put;
041import org.apache.hadoop.hbase.client.Result;
042import org.apache.hadoop.hbase.client.ResultScanner;
043import org.apache.hadoop.hbase.client.Scan;
044import org.apache.hadoop.hbase.client.Table;
045import org.apache.hadoop.hbase.client.TableDescriptor;
046import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
047import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
048import org.apache.hadoop.hbase.testclassification.LargeTests;
049import org.apache.hadoop.hbase.testclassification.MapReduceTests;
050import org.apache.hadoop.hbase.util.Bytes;
051import org.apache.hadoop.io.MapWritable;
052import org.apache.hadoop.io.Text;
053import org.apache.hadoop.mapreduce.Job;
054import org.apache.hadoop.mapreduce.lib.output.NullOutputFormat;
055import org.junit.AfterClass;
056import org.junit.Before;
057import org.junit.BeforeClass;
058import org.junit.ClassRule;
059import org.junit.Test;
060import org.junit.experimental.categories.Category;
061import org.slf4j.Logger;
062import org.slf4j.LoggerFactory;
063
064@Category({MapReduceTests.class, LargeTests.class})
065public class TestTimeRangeMapRed {
066
067  @ClassRule
068  public static final HBaseClassTestRule CLASS_RULE =
069      HBaseClassTestRule.forClass(TestTimeRangeMapRed.class);
070
071  private final static Logger log = LoggerFactory.getLogger(TestTimeRangeMapRed.class);
072  private static final HBaseTestingUtil UTIL =
073    new HBaseTestingUtil();
074  private Admin admin;
075
076  private static final byte [] KEY = Bytes.toBytes("row1");
077  private static final NavigableMap<Long, Boolean> TIMESTAMP = new TreeMap<>();
078  static {
079    TIMESTAMP.put((long)1245620000, false);
080    TIMESTAMP.put((long)1245620005, true); // include
081    TIMESTAMP.put((long)1245620010, true); // include
082    TIMESTAMP.put((long)1245620055, true); // include
083    TIMESTAMP.put((long)1245620100, true); // include
084    TIMESTAMP.put((long)1245620150, false);
085    TIMESTAMP.put((long)1245620250, false);
086  }
087  static final long MINSTAMP = 1245620005;
088  static final long MAXSTAMP = 1245620100 + 1; // maxStamp itself is excluded. so increment it.
089
090  static final TableName TABLE_NAME = TableName.valueOf("table123");
091  static final byte[] FAMILY_NAME = Bytes.toBytes("text");
092  static final byte[] COLUMN_NAME = Bytes.toBytes("input");
093
094  @BeforeClass
095  public static void beforeClass() throws Exception {
096    UTIL.startMiniCluster();
097  }
098
099  @AfterClass
100  public static void afterClass() throws Exception {
101    UTIL.shutdownMiniCluster();
102  }
103
104  @Before
105  public void before() throws Exception {
106    this.admin = UTIL.getAdmin();
107  }
108
109  private static class ProcessTimeRangeMapper
110  extends TableMapper<ImmutableBytesWritable, MapWritable>
111  implements Configurable {
112
113    private Configuration conf = null;
114    private Table 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<>();
121      for (Cell kv : result.listCells()) {
122        tsList.add(kv.getTimestamp());
123      }
124
125      List<Put> puts = new ArrayList<>();
126      for (Long ts : tsList) {
127        Put put = new Put(key.get());
128        put.setDurability(Durability.SKIP_WAL);
129        put.addColumn(FAMILY_NAME, COLUMN_NAME, ts, Bytes.toBytes(true));
130        puts.add(put);
131      }
132      table.put(puts);
133    }
134
135    @Override
136    public Configuration getConf() {
137      return conf;
138    }
139
140    @Override
141    public void setConf(Configuration configuration) {
142      this.conf = configuration;
143      try {
144        Connection connection = ConnectionFactory.createConnection(conf);
145        table = connection.getTable(TABLE_NAME);
146      } catch (IOException e) {
147        e.printStackTrace();
148      }
149    }
150  }
151
152  @Test
153  public void testTimeRangeMapRed()
154    throws IOException, InterruptedException, ClassNotFoundException {
155    final TableDescriptor tableDescriptor =
156      TableDescriptorBuilder.newBuilder(TABLE_NAME).setColumnFamily(ColumnFamilyDescriptorBuilder
157        .newBuilder(FAMILY_NAME).setMaxVersions(Integer.MAX_VALUE).build()).build();
158    admin.createTable(tableDescriptor);
159    List<Put> puts = new ArrayList<>();
160    for (Map.Entry<Long, Boolean> entry : TIMESTAMP.entrySet()) {
161      Put put = new Put(KEY);
162      put.setDurability(Durability.SKIP_WAL);
163      put.addColumn(FAMILY_NAME, COLUMN_NAME, entry.getKey(), Bytes.toBytes(false));
164      puts.add(put);
165    }
166    Table table = UTIL.getConnection().getTable(tableDescriptor.getTableName());
167    table.put(puts);
168    runTestOnTable();
169    verify(table);
170    table.close();
171  }
172
173  private void runTestOnTable()
174  throws IOException, InterruptedException, ClassNotFoundException {
175    Job job = null;
176    try {
177      job = new Job(UTIL.getConfiguration(), "test123");
178      job.setOutputFormatClass(NullOutputFormat.class);
179      job.setNumReduceTasks(0);
180      Scan scan = new Scan();
181      scan.addColumn(FAMILY_NAME, COLUMN_NAME);
182      scan.setTimeRange(MINSTAMP, MAXSTAMP);
183      scan.readAllVersions();
184      TableMapReduceUtil.initTableMapperJob(TABLE_NAME,
185        scan, ProcessTimeRangeMapper.class, Text.class, Text.class, job);
186      job.waitForCompletion(true);
187    } catch (IOException e) {
188      // TODO Auto-generated catch block
189      e.printStackTrace();
190    } finally {
191      if (job != null) {
192        FileUtil.fullyDelete(
193          new File(job.getConfiguration().get("hadoop.tmp.dir")));
194      }
195    }
196  }
197
198  private void verify(final Table table) throws IOException {
199    Scan scan = new Scan();
200    scan.addColumn(FAMILY_NAME, COLUMN_NAME);
201    scan.readVersions(1);
202    ResultScanner scanner = table.getScanner(scan);
203    for (Result r: scanner) {
204      for (Cell kv : r.listCells()) {
205        log.debug(Bytes.toString(r.getRow()) + "\t" + Bytes.toString(CellUtil.cloneFamily(kv))
206            + "\t" + Bytes.toString(CellUtil.cloneQualifier(kv))
207            + "\t" + kv.getTimestamp() + "\t" + Bytes.toBoolean(CellUtil.cloneValue(kv)));
208        org.junit.Assert.assertEquals(TIMESTAMP.get(kv.getTimestamp()),
209          Bytes.toBoolean(CellUtil.cloneValue(kv)));
210      }
211    }
212    scanner.close();
213  }
214
215}
216