1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.coprocessor;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.IOException;
25
26 import org.apache.hadoop.hbase.TableName;
27 import org.apache.hadoop.hbase.HBaseTestingUtility;
28 import org.apache.hadoop.hbase.HColumnDescriptor;
29 import org.apache.hadoop.hbase.HTableDescriptor;
30 import org.apache.hadoop.hbase.MediumTests;
31 import org.apache.hadoop.hbase.client.Durability;
32 import org.apache.hadoop.hbase.client.HBaseAdmin;
33 import org.apache.hadoop.hbase.client.HTable;
34 import org.apache.hadoop.hbase.client.HTableInterface;
35 import org.apache.hadoop.hbase.client.Put;
36 import org.apache.hadoop.hbase.client.Result;
37 import org.apache.hadoop.hbase.client.ResultScanner;
38 import org.apache.hadoop.hbase.client.Scan;
39 import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
40 import org.junit.AfterClass;
41 import org.junit.Test;
42 import org.junit.experimental.categories.Category;
43
44
45
46
47 @Category(MediumTests.class)
48 public class TestOpenTableInCoprocessor {
49
50 private static final TableName otherTable =
51 TableName.valueOf("otherTable");
52 private static final byte[] family = new byte[] { 'f' };
53
54 private static boolean completed = false;
55
56
57
58
59 public static class SendToOtherTableCoprocessor extends BaseRegionObserver {
60
61 @Override
62 public void prePut(ObserverContext<RegionCoprocessorEnvironment> e, Put put, WALEdit edit,
63 final Durability durability) throws IOException {
64 HTableInterface table = e.getEnvironment().getTable(otherTable);
65 Put p = new Put(new byte[] { 'a' });
66 p.add(family, null, new byte[] { 'a' });
67 table.put(put);
68 table.flushCommits();
69 completed = true;
70 table.close();
71 }
72
73 }
74
75 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
76
77 @AfterClass
78 public static void cleanup() throws Exception {
79 UTIL.getHBaseAdmin().close();
80 }
81
82 @Test
83 public void testCoprocessorCanCreateConnectionToRemoteTable() throws Throwable {
84 HTableDescriptor primary = new HTableDescriptor(TableName.valueOf("primary"));
85 primary.addFamily(new HColumnDescriptor(family));
86
87 primary.addCoprocessor(SendToOtherTableCoprocessor.class.getName());
88
89 HTableDescriptor other = new HTableDescriptor(otherTable);
90 other.addFamily(new HColumnDescriptor(family));
91 UTIL.startMiniCluster();
92
93 HBaseAdmin admin = UTIL.getHBaseAdmin();
94 admin.createTable(primary);
95 admin.createTable(other);
96 admin.close();
97
98 HTable table = new HTable(UTIL.getConfiguration(), "primary");
99 Put p = new Put(new byte[] { 'a' });
100 p.add(family, null, new byte[] { 'a' });
101 table.put(p);
102 table.flushCommits();
103 table.close();
104
105 HTable target = new HTable(UTIL.getConfiguration(), otherTable);
106 assertTrue("Didn't complete update to target table!", completed);
107 assertEquals("Didn't find inserted row", 1, getKeyValueCount(target));
108 target.close();
109
110 UTIL.shutdownMiniCluster();
111 }
112
113
114
115
116
117
118
119 private int getKeyValueCount(HTable table) throws IOException {
120 Scan scan = new Scan();
121 scan.setMaxVersions(Integer.MAX_VALUE - 1);
122
123 ResultScanner results = table.getScanner(scan);
124 int count = 0;
125 for (Result res : results) {
126 count += res.list().size();
127 System.out.println(count + ") " + res);
128 }
129 results.close();
130
131 return count;
132 }
133 }