1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
117
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
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
150 assertTrue(data.toString().contains("Usage:"));
151 }
152
153 } finally {
154 System.setErr(oldPrintStream);
155 System.setSecurityManager(SECURITY_MANAGER);
156 }
157 }
158 }