Skip to content

Commit

Permalink
Updated example for ch04, copros.
Browse files Browse the repository at this point in the history
  • Loading branch information
larsgeorge committed Apr 6, 2015
1 parent 38b3130 commit 14fbe48
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 53 deletions.
9 changes: 5 additions & 4 deletions ch04/src/main/java/coprocessor/EndpointCombinedExample.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package coprocessor;

import java.io.IOException;
import java.util.Map;

import coprocessor.generated.RowCounterProtos;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
Expand All @@ -13,10 +16,8 @@
import org.apache.hadoop.hbase.ipc.BlockingRpcCallback;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.Pair;
import util.HBaseHelper;

import java.io.IOException;
import java.util.Map;
import util.HBaseHelper;

// cc EndpointCombinedExample Example extending the batch call to execute multiple endpoint calls
public class EndpointCombinedExample {
Expand Down Expand Up @@ -92,7 +93,7 @@ public Pair<Long, Long> call(RowCounterProtos.RowCountService counter)
", Count: " + entry.getValue());/*]*/
}
/*[*/System.out.println("Total Row Count: " + totalRows);
System.out.println("Total KeyValue Count: " + totalKeyValues);/*]*/
System.out.println("Total Cell Count: " + totalKeyValues);/*]*/
// ^^ EndpointCombinedExample
} catch (Throwable throwable) {
throwable.printStackTrace();
Expand Down
7 changes: 4 additions & 3 deletions ch04/src/main/java/coprocessor/EndpointExample.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package coprocessor;

import java.io.IOException;
import java.util.Map;

import coprocessor.generated.RowCounterProtos;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
Expand All @@ -11,10 +14,8 @@
import org.apache.hadoop.hbase.client.coprocessor.Batch;
import org.apache.hadoop.hbase.ipc.BlockingRpcCallback;
import org.apache.hadoop.hbase.util.Bytes;
import util.HBaseHelper;

import java.io.IOException;
import java.util.Map;
import util.HBaseHelper;

// cc EndpointExample Example using the custom row-count endpoint
// vv EndpointExample
Expand Down
83 changes: 56 additions & 27 deletions ch04/src/main/java/coprocessor/EndpointProxyExample.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,81 @@
package coprocessor;

import java.io.IOException;

import coprocessor.generated.RowCounterProtos;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.InclusiveStopFilter;
import org.apache.hadoop.hbase.ipc.CoprocessorRpcChannel;
import org.apache.hadoop.hbase.util.Bytes;

import util.HBaseHelper;

import java.io.IOException;
import java.util.Map;
import static coprocessor.generated.RowCounterProtos.*;

// cc EndpointProxyExample Example using the proxy call of HTable to invoke an endpoint on a single region
public class EndpointProxyExample {

public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
TableName tableName = TableName.valueOf("testtable");
Connection connection = ConnectionFactory.createConnection(conf);
HBaseHelper helper = HBaseHelper.getHelper(conf);
helper.dropTable("testtable");
helper.createTable("testtable", "colfam1", "colfam2");
helper.createTable("testtable", 3, "colfam1", "colfam2");
helper.put("testtable",
new String[]{"row1", "row2", "row3", "row4", "row5"},
new String[]{"colfam1", "colfam2"},
new String[]{"qual1", "qual1"},
new long[]{1, 2},
new String[]{"val1", "val2"});
new String[]{"colfam1", "colfam2"}, new String[]{"qual1", "qual1"},
new long[]{1, 2}, new String[]{"val1", "val2"});
System.out.println("Before endpoint call...");
helper.dump("testtable",
new String[]{"row1", "row2", "row3", "row4", "row5"},
null, null);
HBaseAdmin admin = new HBaseAdmin(conf);
// try {
// admin.split("testtable", "row3");
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
HTable table = new HTable(conf, "testtable");
new String[]{"row1", "row2", "row3", "row4", "row5"}, null, null);
Admin admin = connection.getAdmin();
try {
admin.split(tableName, Bytes.toBytes("row3"));
} catch (Exception e) {
e.printStackTrace();
}
Table table = connection.getTable(tableName);
// wait for the split to be done
// while (table.getRegionsInfo().size() < 2)
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// }
while (admin.getTableRegions(tableName).size() < 2)
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
try {
//vv EndpointProxyExample
// RowCountProtocol protocol = table.coprocessorProxy(
// RowCountProtocol.class, Bytes.toBytes("row4"));
// long rowsInRegion = protocol.getRowCount();
// System.out.println("Region Row Count: " + rowsInRegion);
HRegionInfo hri = admin.getTableRegions(tableName).get(0);
Scan scan = new Scan(hri.getStartKey(), hri.getEndKey())
.setMaxVersions();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println("Result: " + result);
}

CoprocessorRpcChannel channel = table.coprocessorService(
Bytes.toBytes("row1"));
RowCountService.BlockingInterface service =
RowCountService.newBlockingStub(channel);
CountRequest request = CountRequest.newBuilder().build();
CountResponse response = service.getCellCount(null, request);
long cellsInRegion = response.hasCount() ? response.getCount() : -1;
System.out.println("Region Cell Count: " + cellsInRegion);

request = CountRequest.newBuilder().build();
response = service.getRowCount(null, request);
long rowsInRegion = response.hasCount() ? response.getCount() : -1;
System.out.println("Region Row Count: " + rowsInRegion);
// ^^ EndpointProxyExample
} catch (Throwable throwable) {
throwable.printStackTrace();
Expand Down
39 changes: 20 additions & 19 deletions ch04/src/main/java/coprocessor/MasterObserverExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.coprocessor.BaseMasterObserver;
import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment;
import org.apache.hadoop.hbase.coprocessor.ObserverContext;
Expand All @@ -21,29 +22,29 @@ public class MasterObserverExample extends BaseMasterObserver {
public static final Log LOG = LogFactory.getLog(HRegion.class);
// vv MasterObserverExample

// @Override
@Override
public void postCreateTable(
ObserverContext<MasterCoprocessorEnvironment> env,
HRegionInfo[] regions, boolean sync)
throws IOException {
ObserverContext<MasterCoprocessorEnvironment> ctx,
HTableDescriptor desc, HRegionInfo[] regions)
throws IOException {
// ^^ MasterObserverExample
LOG.debug("Got postCreateTable callback");
// vv MasterObserverExample
// String tableName = regions[0].getTableDesc().getNameAsString(); // co MasterObserverExample-1-GetName Get the new table's name from the table descriptor.
//
// // ^^ MasterObserverExample
// LOG.debug("Created table: " + tableName + ", region count: " + regions.length);
// // vv MasterObserverExample
// MasterServices services = env.getEnvironment().getMasterServices();
// MasterFileSystem masterFileSystem = services.getMasterFileSystem(); // co MasterObserverExample-2-Services Get the available services and retrieve a reference to the actual file system.
// FileSystem fileSystem = masterFileSystem.getFileSystem();
//
// Path blobPath = new Path(tableName + "-blobs"); // co MasterObserverExample-3-Path Create a new directory that will store binary data from the client application.
// fileSystem.mkdirs(blobPath);
//
// // ^^ MasterObserverExample
// LOG.debug("Created " + blobPath + ": " + fileSystem.exists(blobPath));
// // vv MasterObserverExample
String tableName = desc.getNameAsString(); // co MasterObserverExample-1-GetName Get the new table's name from the table descriptor.

// ^^ MasterObserverExample
LOG.debug("Created table: " + tableName + ", region count: " + regions.length);
// vv MasterObserverExample
MasterServices services = ctx.getEnvironment().getMasterServices();
MasterFileSystem masterFileSystem = services.getMasterFileSystem(); // co MasterObserverExample-2-Services Get the available services and retrieve a reference to the actual file system.
FileSystem fileSystem = masterFileSystem.getFileSystem();

Path blobPath = new Path(tableName + "-blobs"); // co MasterObserverExample-3-Path Create a new directory that will store binary data from the client application.
fileSystem.mkdirs(blobPath);

// ^^ MasterObserverExample
LOG.debug("Created " + blobPath + ": " + fileSystem.exists(blobPath));
// vv MasterObserverExample
}
}
// ^^ MasterObserverExample

0 comments on commit 14fbe48

Please sign in to comment.