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.mapred;
019
020import static org.junit.Assert.assertTrue;
021
022import java.io.File;
023import java.io.IOException;
024import org.apache.hadoop.fs.FileUtil;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.client.Put;
027import org.apache.hadoop.hbase.client.Result;
028import org.apache.hadoop.hbase.client.Table;
029import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
030import org.apache.hadoop.hbase.mapreduce.TestTableMapReduceBase;
031import org.apache.hadoop.hbase.testclassification.LargeTests;
032import org.apache.hadoop.hbase.testclassification.MapReduceTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.apache.hadoop.mapred.JobClient;
035import org.apache.hadoop.mapred.JobConf;
036import org.apache.hadoop.mapred.MapReduceBase;
037import org.apache.hadoop.mapred.OutputCollector;
038import org.apache.hadoop.mapred.Reporter;
039import org.apache.hadoop.mapred.RunningJob;
040import org.junit.ClassRule;
041import org.junit.experimental.categories.Category;
042import org.slf4j.Logger;
043import org.slf4j.LoggerFactory;
044
045/**
046 * Test Map/Reduce job over HBase tables. The map/reduce process we're testing on our tables is
047 * simple - take every row in the table, reverse the value of a particular cell, and write it back
048 * to the table.
049 */
050@Category({ MapReduceTests.class, LargeTests.class })
051@SuppressWarnings("deprecation")
052public class TestTableMapReduce extends TestTableMapReduceBase {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056    HBaseClassTestRule.forClass(TestTableMapReduce.class);
057
058  private static final Logger LOG = LoggerFactory.getLogger(TestTableMapReduce.class.getName());
059
060  protected Logger getLog() {
061    return LOG;
062  }
063
064  /**
065   * Pass the given key and processed record reduce
066   */
067  static class ProcessContentsMapper extends MapReduceBase
068    implements TableMap<ImmutableBytesWritable, Put> {
069
070    /**
071     * Pass the key, and reversed value to reduce
072     */
073    public void map(ImmutableBytesWritable key, Result value,
074      OutputCollector<ImmutableBytesWritable, Put> output, Reporter reporter) throws IOException {
075      output.collect(key, TestTableMapReduceBase.map(key, value));
076    }
077  }
078
079  @Override
080  protected void runTestOnTable(Table table) throws IOException {
081    JobConf jobConf = null;
082    try {
083      LOG.info("Before map/reduce startup");
084      jobConf = new JobConf(UTIL.getConfiguration(), TestTableMapReduce.class);
085      jobConf.setJobName("process column contents");
086      jobConf.setNumReduceTasks(1);
087      TableMapReduceUtil.initTableMapJob(table.getName().getNameAsString(),
088        Bytes.toString(INPUT_FAMILY), ProcessContentsMapper.class, ImmutableBytesWritable.class,
089        Put.class, jobConf);
090      TableMapReduceUtil.initTableReduceJob(table.getName().getNameAsString(),
091        IdentityTableReduce.class, jobConf);
092
093      LOG.info("Started " + table.getName());
094      RunningJob job = JobClient.runJob(jobConf);
095      assertTrue(job.isSuccessful());
096      LOG.info("After map/reduce completion");
097
098      // verify map-reduce results
099      verify(table.getName());
100    } finally {
101      if (jobConf != null) {
102        FileUtil.fullyDelete(new File(jobConf.get("hadoop.tmp.dir")));
103      }
104    }
105  }
106}