Redis源码学习二:RedisCluster源码阅读(1)

1.获取指定rowkey对应region和regionserver

(1).获取一个表的RegionLocator

1
2
3
TableName tableName = TableName.valueOf(Bytes.toBytes("myTableName"))
RegionLocator locator = connection.getRegionLocator(tableName);

(2).使用RegionLocator定位rowkey对应的Region和Region Server

1
2
byte[] rowKey = xx
HRegionLocation location = locator.getRegionLocation(rowKey);

其中HRegionLocation包含了Region、RegionServer等信息。

1
2
public HRegionInfo getRegionInfo();
public ServerName getServerName();

(3).RegionLocator的其他功能

1
2
3
4
5
public List<HRegionLocation> getAllRegionLocations();
public byte [][] getStartKeys() throws IOException;
public byte[][] getEndKeys() throws IOException;
public Pair<byte[][],byte[][]> getStartEndKeys() throws IOException;
TableName getName();

2.获取regionServer下所有的Region

1
2
ServerName serverName = ServerName.valueOf(“xxxx”); List<HRegionInfo> hRegionInfoList = connection.getAdmin().getOnlineRegions(serverName); for (HRegionInfo hRegionInfo : hRegionInfoList){
System.out.println(Bytes.toString(hRegionInfo.getRegionName())); }