Skip to content

Commit b55bfa1

Browse files
authored
Create lr.rb
list hbase regions for a table (or over a regionserver)
1 parent 91396fa commit b55bfa1

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

lr.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
def list_regions(table_name, region_server_name = "")
2+
admin = @hbase.admin(@hbase)
3+
admin_instance = admin.instance_variable_get("@admin")
4+
conn_instance = admin_instance.getConnection()
5+
cluster_status = admin_instance.getClusterStatus()
6+
hregion_locator_instance = conn_instance.getRegionLocator(TableName.valueOf(table_name))
7+
hregion_locator_list = hregion_locator_instance.getAllRegionLocations()
8+
9+
table_max_file_size = (admin_instance.getTableDescriptor(TableName.valueOf(table_name)).getMaxFileSize()/1024/1024).ceil
10+
if table_max_file_size < 0
11+
table_max_file_size = (admin_instance.getConfiguration.getLong("hbase.hregion.max.filesize", 5)/1024/1024).ceil
12+
end
13+
results = Array.new
14+
15+
begin
16+
hregion_locator_list.each do |hregion|
17+
hregion_info = hregion.getRegionInfo()
18+
server_name = hregion.getServerName()
19+
if hregion.getServerName().toString.start_with? region_server_name
20+
startKey = Bytes.toString(hregion.getRegionInfo().getStartKey())
21+
endKey = Bytes.toString(hregion.getRegionInfo().getEndKey())
22+
region_load_map = cluster_status.getLoad(server_name).getRegionsLoad()
23+
region_load = region_load_map.get(hregion_info.getRegionName())
24+
region_store_file_size = region_load.getStorefileSizeMB()
25+
occupancy = (region_store_file_size*100/table_max_file_size).ceil
26+
region_requests = region_load.getRequestsCount()
27+
results << { "server" => hregion.getServerName().toString(), "name" => hregion_info.getRegionNameAsString(), "startkey" => startKey, "endkey" => endKey, "size" => region_store_file_size, "occupancy" => occupancy, "requests" => region_requests }
28+
end
29+
end
30+
ensure
31+
hregion_locator_instance.close()
32+
end
33+
34+
@end_time = Time.now
35+
36+
printf("%-60s | %-60s | %-15s | %-15s | %-15s | %-15s | %-15s", "SERVER_NAME", "REGION_NAME", "START_KEY", "END_KEY", "SIZE(MB)", "OCCUPANCY(%)", "REQ");
37+
printf("\n")
38+
for result in results
39+
printf("%-60s | %-60s | %-15s | %-15s | %-15s | %-15s | %-15s", result["server"], result["name"], result["startkey"], result["endkey"], result["size"], result["occupancy"], result["requests"]);
40+
printf("\n")
41+
end
42+
printf("%d rows", results.size)
43+
44+
end

0 commit comments

Comments
 (0)