View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.mapreduce;
20  
21  import org.apache.commons.io.IOUtils;
22  import org.apache.hadoop.conf.Configuration;
23  import org.apache.hadoop.fs.FileUtil;
24  import org.apache.hadoop.fs.LocalFileSystem;
25  import org.apache.hadoop.fs.Path;
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.LargeTests;
28  import org.apache.hadoop.hbase.client.HTable;
29  import org.apache.hadoop.hbase.client.Put;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.apache.hadoop.hbase.util.LauncherSecurityManager;
32  import org.apache.hadoop.mapreduce.Job;
33  import org.apache.hadoop.util.GenericOptionsParser;
34  import org.junit.AfterClass;
35  import org.junit.BeforeClass;
36  import org.junit.Test;
37  import org.junit.experimental.categories.Category;
38  
39  import java.io.*;
40  
41  import static org.junit.Assert.assertTrue;
42  import static org.junit.Assert.assertEquals;
43  import static org.junit.Assert.fail;
44  
45  @Category(LargeTests.class)
46  public class TestCellCounter {
47    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
48    private static final byte[] ROW1 = Bytes.toBytes("row1");
49    private static final byte[] ROW2 = Bytes.toBytes("row2");
50    private static final String FAMILY_A_STRING = "a";
51    private static final String FAMILY_B_STRING = "b";
52    private static final byte[] FAMILY_A = Bytes.toBytes(FAMILY_A_STRING);
53    private static final byte[] FAMILY_B = Bytes.toBytes(FAMILY_B_STRING);
54    private static final byte[] QUALIFIER = Bytes.toBytes("q");
55  
56    private static Path FQ_OUTPUT_DIR;
57    private static final String OUTPUT_DIR = "target" + File.separator + "test-data" + File.separator
58        + "output";
59    private static long now = System.currentTimeMillis();
60  
61    @BeforeClass
62    public static void beforeClass() throws Exception {
63      UTIL.startMiniCluster();
64      UTIL.startMiniMapReduceCluster();
65      FQ_OUTPUT_DIR = new Path(OUTPUT_DIR).makeQualified(new LocalFileSystem());
66      FileUtil.fullyDelete(new File(OUTPUT_DIR));
67    }
68  
69    @AfterClass
70    public static void afterClass() throws Exception {
71      UTIL.shutdownMiniMapReduceCluster();
72      UTIL.shutdownMiniCluster();
73    }
74  
75    /**
76     * Test CellCounter all data should print to output
77     * 
78     */
79    @Test (timeout=300000)
80    public void testCellCounter() throws Exception {
81      String sourceTable = "sourceTable";
82      byte[][] families = { FAMILY_A, FAMILY_B };
83      HTable t = UTIL.createTable(Bytes.toBytes(sourceTable), families);
84      try{
85      Put p = new Put(ROW1);
86      p.add(FAMILY_A, QUALIFIER, now, Bytes.toBytes("Data11"));
87      p.add(FAMILY_B, QUALIFIER, now + 1, Bytes.toBytes("Data12"));
88      p.add(FAMILY_A, QUALIFIER, now + 2, Bytes.toBytes("Data13"));
89      t.put(p);
90      p = new Put(ROW2);
91      p.add(FAMILY_B, QUALIFIER, now, Bytes.toBytes("Dat21"));
92      p.add(FAMILY_A, QUALIFIER, now + 1, Bytes.toBytes("Data22"));
93      p.add(FAMILY_B, QUALIFIER, now + 2, Bytes.toBytes("Data23"));
94      t.put(p);
95      String[] args = { sourceTable, FQ_OUTPUT_DIR.toString(), ";", "^row1" };
96      runCount(args);
97      FileInputStream inputStream = new FileInputStream(OUTPUT_DIR + File.separator + 
98          "part-r-00000");
99      String data = IOUtils.toString(inputStream);
100     inputStream.close();
101     assertTrue(data.contains("Total Families Across all Rows" + "\t" + "2"));
102     assertTrue(data.contains("Total Qualifiers across all Rows" + "\t" + "2"));
103     assertTrue(data.contains("Total ROWS" + "\t" + "1"));
104     assertTrue(data.contains("b;q" + "\t" + "1"));
105     assertTrue(data.contains("a;q" + "\t" + "1"));
106     assertTrue(data.contains("row1;a;q_Versions" + "\t" + "1"));
107     assertTrue(data.contains("row1;b;q_Versions" + "\t" + "1"));
108     }finally{
109       t.close();
110     }
111 
112   }
113 
114   private boolean runCount(String[] args) throws IOException, InterruptedException,
115       ClassNotFoundException {
116     // need to make a copy of the configuration because to make sure
117     // different temp dirs are used.
118     GenericOptionsParser opts = new GenericOptionsParser(
119         new Configuration(UTIL.getConfiguration()), args);
120     Configuration configuration = opts.getConfiguration();
121     args = opts.getRemainingArgs();
122     Job job = CellCounter.createSubmittableJob(configuration, args);
123     job.waitForCompletion(false);
124     return job.isSuccessful();
125   }
126 
127   /**
128    * Test main method of CellCounter
129    */
130   @Test (timeout=300000)
131   public void testCellCounterMain() throws Exception {
132 
133     PrintStream oldPrintStream = System.err;
134     SecurityManager SECURITY_MANAGER = System.getSecurityManager();
135     LauncherSecurityManager newSecurityManager= new LauncherSecurityManager();
136     System.setSecurityManager(newSecurityManager);
137     ByteArrayOutputStream data = new ByteArrayOutputStream();
138     String[] args = {};
139     System.setErr(new PrintStream(data));
140     try {
141       System.setErr(new PrintStream(data));
142 
143       try {
144         CellCounter.main(args);
145         fail("should be SecurityException");
146       } catch (SecurityException e) {
147         assertEquals(-1, newSecurityManager.getExitCode());
148         assertTrue(data.toString().contains("ERROR: Wrong number of parameters:"));
149         // should be information about usage
150         assertTrue(data.toString().contains("Usage:"));
151       }
152 
153     } finally {
154       System.setErr(oldPrintStream);
155       System.setSecurityManager(SECURITY_MANAGER);
156     }
157   }
158 }