HBASE-2556 Add convenience method to HBaseAdmin to get a collection of HRegionInfo objects for each table

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1130316 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2011-06-01 21:51:02 +00:00
parent fe8dce6636
commit ac8a57e93b
5 changed files with 83 additions and 5 deletions

View File

@ -236,6 +236,8 @@ Release 0.91.0 - Unreleased
HBASE-3931 Allow adding attributes to Get
HBASE-3942 The thrift scannerOpen functions should support row caching
(Adam Worthington)
HBASE-2556 Add convenience method to HBaseAdmin to get a collection of
HRegionInfo objects for each table (Ming Ma)
TASKS
HBASE-3559 Move report of split to master OFF the heartbeat channel

View File

@ -21,8 +21,10 @@ package org.apache.hadoop.hbase.client;
import java.io.Closeable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -1240,6 +1242,27 @@ public class HBaseAdmin implements Abortable, Closeable {
new HBaseAdmin(copyOfConf);
}
/**
* get the regions of a given table.
*
* @param tableName the name of the table
* @return Ordered list of {@link HRegionInfo}. *
* @throws IOException
*/
public List<HRegionInfo> getTableRegions(final byte[] tableName) throws IOException
{
CatalogTracker ct = getCatalogTracker();
List<HRegionInfo> Regions;
try {
Regions = MetaReader.getTableRegions(ct, tableName, true);
} finally {
cleanupCatalogTracker(ct);
}
return Regions;
}
public void close() throws IOException {
if (this.connection != null) {
this.connection.close();

View File

@ -270,11 +270,10 @@ public class ThriftServer {
public List<TRegionInfo> getTableRegions(ByteBuffer tableName)
throws IOError {
try{
HTable table = getTable(tableName);
Map<HRegionInfo, HServerAddress> regionsInfo = table.getRegionsInfo();
List<HRegionInfo> HRegions = this.admin.getTableRegions(tableName.array());
List<TRegionInfo> regions = new ArrayList<TRegionInfo>();
for (HRegionInfo regionInfo : regionsInfo.keySet()){
for (HRegionInfo regionInfo : HRegions){
TRegionInfo region = new TRegionInfo();
region.startKey = ByteBuffer.wrap(regionInfo.getStartKey());
region.endKey = ByteBuffer.wrap(regionInfo.getEndKey());

View File

@ -830,4 +830,35 @@ public class TestAdmin {
this.admin.deleteTable(tableName);
}
}
}
/**
* For HBASE-2556
* @throws IOException
*/
@Test
public void testGetTableRegions() throws IOException {
byte[] tableName = Bytes.toBytes("testGetTableRegions");
int expectedRegions = 10;
// Use 80 bit numbers to make sure we aren't limited
byte [] startKey = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
byte [] endKey = { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 };
HTableDescriptor desc = new HTableDescriptor(tableName);
desc.addFamily(new HColumnDescriptor(HConstants.CATALOG_FAMILY));
admin = new HBaseAdmin(TEST_UTIL.getConfiguration());
admin.createTable(desc, startKey, endKey, expectedRegions);
List<HRegionInfo> RegionInfos = admin.getTableRegions(tableName);
assertEquals("Tried to create " + expectedRegions + " regions " +
"but only found " + RegionInfos.size(),
expectedRegions, RegionInfos.size());
}
}

View File

@ -66,6 +66,7 @@ public class TestThriftServer extends HBaseClusterTestCase {
doTestTableMutations();
doTestTableTimestampsAndColumns();
doTestTableScanners();
doTestGetTableRegions();
}
/**
@ -98,7 +99,8 @@ public class TestThriftServer extends HBaseClusterTestCase {
handler.disableTable(tableAname);*/
handler.deleteTable(tableAname);
}
/**
* Tests adding a series of Mutations and BatchMutations, including a
* delete mutation. Also tests data retrieval, and getting back multiple
@ -310,6 +312,27 @@ public class TestThriftServer extends HBaseClusterTestCase {
handler.deleteTable(tableAname);
}
/**
* For HBASE-2556
* Tests for GetTableRegions
*
* @throws Exception
*/
public void doTestGetTableRegions() throws Exception {
ThriftServer.HBaseHandler handler = new ThriftServer.HBaseHandler(this.conf);
handler.createTable(tableAname, getColumnDescriptors());
int RegionCount = handler.getTableRegions(tableAname).size();
assertEquals("empty table should have only 1 region, " +
"but found " + RegionCount, RegionCount, 1);
handler.disableTable(tableAname);
handler.deleteTable(tableAname);
RegionCount = handler.getTableRegions(tableAname).size();
assertEquals("non-existing table should have 0 region, " +
"but found " + RegionCount, RegionCount, 0);
}
/**
*
* @return a List of ColumnDescriptors for use in creating a table. Has one