diff --git a/src/main/java/org/apache/hadoop/hbase/thrift/ThriftServer.java b/src/main/java/org/apache/hadoop/hbase/thrift/ThriftServer.java index 3b7f377..7f489de 100644 --- a/src/main/java/org/apache/hadoop/hbase/thrift/ThriftServer.java +++ b/src/main/java/org/apache/hadoop/hbase/thrift/ThriftServer.java @@ -29,6 +29,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; +import java.util.Iterator; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; @@ -45,6 +46,7 @@ import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.HRegionInfo; import org.apache.hadoop.hbase.HServerAddress; +import org.apache.hadoop.hbase.HRegionLocation; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.client.Delete; @@ -67,6 +69,7 @@ import org.apache.hadoop.hbase.thrift.generated.IllegalArgument; import org.apache.hadoop.hbase.thrift.generated.Mutation; import org.apache.hadoop.hbase.thrift.generated.TCell; import org.apache.hadoop.hbase.thrift.generated.TRegionInfo; +import org.apache.hadoop.hbase.thrift.generated.TRegionLocation; import org.apache.hadoop.hbase.thrift.generated.TRowResult; import org.apache.hadoop.hbase.util.Bytes; import org.apache.thrift.TException; @@ -268,22 +271,54 @@ public class ThriftServer { @Override public List getTableRegions(ByteBuffer tableName) throws IOError { - try{ + Map m = this.getRegionsInfo(tableName); + return new ArrayList(m.keySet()); + } + + public Map getRegionsInfo(ByteBuffer tableName) + throws IOError { + try { HTable table = getTable(tableName); Map regionsInfo = table.getRegionsInfo(); - List regions = new ArrayList(); - - for (HRegionInfo regionInfo : regionsInfo.keySet()){ - TRegionInfo region = new TRegionInfo(); - region.startKey = ByteBuffer.wrap(regionInfo.getStartKey()); - region.endKey = ByteBuffer.wrap(regionInfo.getEndKey()); - region.id = regionInfo.getRegionId(); - region.name = ByteBuffer.wrap(regionInfo.getRegionName()); - region.version = regionInfo.getVersion(); - regions.add(region); + Map m = new HashMap(); + + for (Iterator it = regionsInfo.entrySet().iterator(); it.hasNext();) { + Map.Entry entry = (Map.Entry)it.next(); + HRegionInfo regionInfo = (HRegionInfo)entry.getKey(); + HServerAddress addrInfo = (HServerAddress)entry.getValue(); + + TRegionInfo region = new TRegionInfo( + ByteBuffer.wrap(regionInfo.getStartKey()), + ByteBuffer.wrap(regionInfo.getEndKey()), + regionInfo.getRegionId(), + ByteBuffer.wrap(regionInfo.getRegionName()), + regionInfo.getVersion()); + ByteBuffer addr = ByteBuffer.wrap(Bytes.toBytes(addrInfo.toString())); + m.put(region, addr); } - return regions; - } catch (IOException e){ + return m; + } catch (IOException e) { + throw new IOError(e.getMessage()); + } + } + + public TRegionLocation getRegionLocation(ByteBuffer tableName, ByteBuffer row) + throws IOError { + try { + HTable table = getTable(tableName); + HRegionLocation regionLocation = table.getRegionLocation(row.array()); + HRegionInfo regionInfo = regionLocation.getRegionInfo(); + HServerAddress addrInfo = regionLocation.getServerAddress(); + + TRegionInfo region = new TRegionInfo( + ByteBuffer.wrap(regionInfo.getStartKey()), + ByteBuffer.wrap(regionInfo.getEndKey()), + regionInfo.getRegionId(), + ByteBuffer.wrap(regionInfo.getRegionName()), + regionInfo.getVersion()); + ByteBuffer addr = ByteBuffer.wrap(Bytes.toBytes(addrInfo.toString())); + return new TRegionLocation(region, addr); + } catch (IOException e) { throw new IOError(e.getMessage()); } } diff --git a/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift b/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift index 57c7744..924e2a6 100644 --- a/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift +++ b/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift @@ -90,6 +90,14 @@ struct TRegionInfo { } /** + * A TRegionLocation contains TRegionInfo and Server Address (host:port style string) + */ +struct TRegionLocation { + 1:TRegionInfo regionInfo, + 2:Text serverAddress +} + +/** * A Mutation object is used to either update or delete a column-value. */ struct Mutation { @@ -209,6 +217,28 @@ service Hbase { throws (1:IOError io) /** + * Get all the regions and their address for a specified table. + * + * @return A map of TRegionInfo with its server address (host:port style string) + */ + map getRegionsInfo( + /** table name */ + 1:Text tableName) + throws (1:IOError io) + + /** + * Find the region on which given row is being served. + * + * @return Location of the row. + */ + TRegionLocation getRegionLocation( + /** table name */ + 1:Text tableName, + /** row key */ + 2:Text row) + throws (1:IOError io) + + /** * Create a table with the specified column families. The name * field for each ColumnDescriptor must be set and must end in a * colon (:). All other fields are optional and will get default