{"id":1432,"date":"2022-06-14T12:04:41","date_gmt":"2022-06-14T04:04:41","guid":{"rendered":"http:\/\/www.eait.co\/?p=1432"},"modified":"2022-06-14T12:04:41","modified_gmt":"2022-06-14T04:04:41","slug":"hbase-region-%e5%9d%87%e8%a1%a1%e8%ae%be%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/notes.coremix.net\/?p=1432","title":{"rendered":"Hbase Region \u5747\u8861\u8bbe\u7f6e"},"content":{"rendered":"<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\r\npackage hbaseapi;\r\n        import org.apache.hadoop.conf.Configuration;\r\n        import org.apache.hadoop.hbase.*;\r\n        import org.apache.hadoop.hbase.client.*;\r\n        import org.apache.hadoop.hbase.util.Bytes;\r\n\r\n        import java.io.IOException;\r\n        import java.text.DecimalFormat;\r\n        import java.util.ArrayList;\r\n        import java.util.Iterator;\r\n        import java.util.List;\r\n        import java.util.TreeSet;\r\n\r\n\/**\r\n * \u5c1d\u8bd5\u5c06\u8868\u6539\u4e3a\u7edd\u5bf9\u5747\u8861\uff08\u4fee\u6539key\u503c\u524d\u9762\u7684\u4e24\u4e2a\u6570\u5b57\u4e3a\u6709\u5e8f\u6570\u5b57\u800c\u4e0d\u662f\u968f\u673a\u6570\uff09\r\n *\/\r\npublic class absBalance{\r\n    public static Configuration conf;\r\n    static Connection connection = null;\r\n    static HBaseAdmin admin = null;\r\n\r\n\r\n    static {\r\n\r\n        try {\r\n            conf = HBaseConfiguration.create();\r\n            conf.set(&quot;hbase.zookeeper.quorum&quot;, &quot;bigdata166&quot;);\r\n            conf.set(&quot;hbase.zookeeper.property.clientport&quot;, &quot;2181&quot;);\r\n\r\n            connection = ConnectionFactory.createConnection(conf);\r\n            admin = (HBaseAdmin) connection.getAdmin();\r\n        } catch (IOException e) {\r\n            e.printStackTrace();\r\n        }\r\n\r\n    }\r\n\r\n\r\n\r\n\r\n    \/**\r\n     * \u5206\u533a\u952e\r\n     *\r\n     * @param regions region\u4e2a\u6570\r\n     * @return splitKeys\r\n     *\/\r\n    private static byte&#x5B;]&#x5B;] genSplitKeys(int regions) {\r\n        \/\/\u7b2c\u4e00\u6b65\uff1a\u751f\u6210\u5206\u533a\u952e\r\n        String&#x5B;] keys = new String&#x5B;regions];\r\n        \/\/\u683c\u5f0f\u5316\u5206\u533a\u952e\u7684\u5f62\u5f0f  00 01 02\r\n        DecimalFormat df = new DecimalFormat(&quot;00&quot;);\r\n        for (int i = 0; i &lt; regions; i++) {\r\n            keys&#x5B;i] = df.format(i*10) + &quot;&quot;;\r\n        }\r\n\r\n\r\n        \/\/\u7b2c\u4e8c\u6b65\uff1a\u5206\u533a\u952e\u6392\u5e8f\r\n        \/\/\u6392\u5e8f \u4fdd\u8bc1\u4f60\u8fd9\u4e2a\u5206\u533a\u952e\u662f\u6709\u5e8f\u5f97\r\n        TreeSet&lt;byte&#x5B;]&gt; treeSet = new TreeSet&lt;&gt;(Bytes.BYTES_COMPARATOR);\r\n        for (int i = 0; i &lt; regions; i++) {\r\n            treeSet.add(Bytes.toBytes(keys&#x5B;i]));\r\n        }\r\n\r\n\r\n        \/\/\u7b2c\u4e09\u6b65\uff1a\u8fed\u4ee3\u8f93\u51fa\r\n        byte&#x5B;]&#x5B;] splitKeys = new byte&#x5B;regions]&#x5B;];\r\n        Iterator&lt;byte&#x5B;]&gt; iterator = treeSet.iterator();\r\n        int index = 0;\r\n        while (iterator.hasNext()) {\r\n            byte&#x5B;] next = iterator.next();\r\n            splitKeys&#x5B;index++] = next;\r\n        }\r\n\r\n        return splitKeys;\r\n    }\r\n\r\n    public static boolean isTableExist(String tableName) throws IOException {\r\n        return admin.tableExists(tableName);\r\n    }\r\n\r\n    \/**\r\n     * (\u5224\u65ad\u8868)\u521b\u5efa\u8868\r\n     *\r\n     * @param tableName    \u8868\u540d\r\n     * @param columnFamily \u5217\u65cf&lt;=3 \u591a\u4e2a  \u53ef\u53d8\r\n     *\/\r\n    public static void createRegionsTable(String tableName, int regions, String... columnFamily) throws Exception {\r\n\r\n        if (isTableExist(tableName)) {\r\n            System.out.println(&quot;\u8868\u5df2\u5b58\u5728&quot;);\r\n        } else {\r\n            \/\/\u901a\u8fc7\u8868\u63cf\u8ff0\u5668\r\n            HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));\r\n            \/\/\u5faa\u73af\u521b\u5efa\u5217\u65cf\r\n            for (String cf : columnFamily) {\r\n                HColumnDescriptor hcd = new HColumnDescriptor(cf);\r\n                descriptor.addFamily(hcd);\r\n            }\r\n\r\n            admin.createTable(descriptor, genSplitKeys(regions)); \/\/  \u6839\u636e\u4e8c\u7ef4\u6570\u7ec4\u521b\u5efaregion\r\n            System.out.println(&quot;\u8868\u521b\u5efa\u6210\u529f&quot;);\r\n\r\n        }\r\n\r\n    }\r\n\r\n\r\n    private static String getRandomNumber() {\r\n        return (Math.random() + &quot;&quot;).substring(2, 4);\r\n    }\r\n\r\n    private static List&lt;Put&gt; batchPut() {\r\n        List&lt;Put&gt; list = new ArrayList&lt;Put&gt;();\r\n        for (int i = 1; i &lt;= 100000; i++) {\r\n            String frs = Math.ceil((double) i\/1000)+&quot;&quot;;\r\n            byte&#x5B;] rowkey = Bytes.toBytes(frs + &quot;-&quot; + System.currentTimeMillis() + &quot;-&quot; + i);\r\n            Put put = new Put(rowkey);\r\n            put.addColumn(Bytes.toBytes(&quot;cf1&quot;), Bytes.toBytes(&quot;name&quot;), Bytes.toBytes(&quot;andy&quot; + i));\r\n            list.add(put);\r\n        }\r\n\r\n\r\n        return list;\r\n    }\r\n\r\n    \/**\r\n     * \u83b7\u53d6\u6b64row\u5728\u54ea\u4e00\u4e2aregion\u4e0a\r\n     *\r\n     * @param tableName\r\n     * @param row\r\n     * @throws IOException\r\n     *\/\r\n    private static void getRegionLocation(String tableName, byte&#x5B;] row) throws IOException {\r\n        RegionLocator location = connection.getRegionLocator(TableName.valueOf(tableName));\r\n        HRegionInfo rg = location.getRegionLocation(row).getRegionInfo();\r\n        String regionname = Bytes.toString(rg.getRegionName());\r\n        String strkey = Bytes.toString(rg.getStartKey());\r\n        String endkey = Bytes.toString(rg.getEndKey());\r\n        System.out.println(regionname);\r\n        System.out.println(strkey);\r\n        System.out.println(endkey);\r\n    }\r\n\r\n\r\n\r\n    public static void main(String&#x5B;] args) throws Exception {\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\u6d4b\u8bd5\u4ee3\u7801\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n        byte&#x5B;]&#x5B;] test = genSplitKeys(10);\r\n        for (byte&#x5B;] bytes : test) {\r\n            System.out.println(Bytes.toString(bytes));\r\n        }\r\n\r\n        \/\/getRow(&quot;student&quot;, &quot;1001&quot;);\r\n\r\n\/\/        getRowQualifier(&quot;student&quot;, &quot;1001&quot;, &quot;cf1&quot;, &quot;sex&quot;);\r\n\r\n\r\n\/\/        initNameSpace(&quot;ccc&quot;);\r\n\/\/        dropTable(&quot;cndy&quot;);\r\n        createRegionsTable(&quot;fndy&quot;, 10, &quot;cf1&quot;);\r\n        Table table = connection.getTable(TableName.valueOf(&quot;fndy&quot;));\r\n        table.put(batchPut());  \/\/ \u81ea\u52a8\u5c06\u6570\u636e\u5747\u5300\u5b58\u50a8\u5230\u91cc\u9762\r\n        \/\/genSplitKeys(6);\r\n\/\/        getRegionLocation(&quot;cndy&quot;, Bytes.toBytes(&quot;75-1649578056056-90350&quot;));\r\n\/\/        getRegionLocation(&quot;cndy&quot;, Bytes.toBytes(&quot;97-xxx&quot;));\r\n\/\/        getRegionLocation(&quot;cndy&quot;, Bytes.toBytes(&quot;97-999&quot;));\r\n\/\/        getRegionLocation(&quot;cndy&quot;, Bytes.toBytes(&quot;97-777&quot;));\r\n\/\/        getRegionLocation(&quot;cndy&quot;, Bytes.toBytes(&quot;44-777&quot;));\r\n\/\/        getRegionLocation(&quot;cndy&quot;, Bytes.toBytes(&quot;44***777&quot;));\r\n\/\/        getRegionLocation(&quot;cndy&quot;, Bytes.toBytes(&quot;s4***777&quot;));\r\n\/\/        getRegionLocation(&quot;cndy&quot;, Bytes.toBytes(&quot;24***777&quot;));\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>package hbaseapi; import org.apache.hadoop.conf.Configu [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1432","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/1432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1432"}],"version-history":[{"count":1,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/1432\/revisions"}],"predecessor-version":[{"id":1433,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/1432\/revisions\/1433"}],"wp:attachment":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}