package com.hbase.HBaseAdmin; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.RegionLocator; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Pair; /** * @author:FengZhen * @create:2018年9月6日 */ public class CreateTable { private static String addr="HDP233,HDP232,HDP231"; private static String port="2181"; private static Connection connection; /** * 獲取連接 */ public static void getConnection(){ Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum",addr); conf.set("hbase.zookeeper.property.clientPort", port); try { connection = ConnectionFactory.createConnection(conf); } catch (IOException e) { e.printStackTrace(); } } /* * 關閉連接 */ public static void close() { /** * close connection **/ if (connection != null) { try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { createTablePrePartition(); } /** * 建表 */ public static void createTable() { getConnection(); try { //獲取admin實例 Admin admin = connection.getAdmin(); //創建表描述符 HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("test_create")); //添加列族描述符到表描述符中 HColumnDescriptor columnDescriptor = new HColumnDescriptor(Bytes.toBytes("info")); tableDescriptor.addFamily(columnDescriptor); //調用create方法 admin.createTable(tableDescriptor); //檢查表是否可用 boolean avail = admin.isTableAvailable(TableName.valueOf("test_create")); System.out.println("Table available: " + avail); } catch (IOException e) { e.printStackTrace(); } finally { close(); } } //---------------------------------------通過預分區建表----------------------------------------- /** * Printing regions of table: test_pre_partition1 [1]start key: ,end key:1 [2]start key:1 ,end key:13 [3]start key:13 ,end key:25 [4]start key:25 ,end key:37 [5]start key:37 ,end key:49 [6]start key:49 ,end key:61 [7]start key:61 ,end key:73 [8]start key:73 ,end key:85 [9]start key:85 ,end key:100 [10]start key:100 ,end key: Printing regions of table: test_pre_partition2 [1]start key: ,end key:A [2]start key:A ,end key:D [3]start key:D ,end key:G [4]start key:G ,end key:K [5]start key:K ,end key:O [6]start key:O ,end key:T [7]start key:T ,end key: */ /** * 打印表中region信息 * @param tableName * @throws IOException */ public static void printTableRegions(String tableName) throws IOException { System.out.println("Printing regions of table: " + tableName); //返回表中所有region的起始行鍵與終止行鍵列表 RegionLocator regionLocator = connection.getRegionLocator(TableName.valueOf(tableName)); //獲取所有region的邊界。 //第一個region的起始行鍵與最后一個region的終止行鍵都是空字節,這是HBase中默認的規則 //起始和終止行鍵都是已經計算好的,或是提供給用戶的拆分鍵。 //需要注意的是,前一個region的終止行鍵與后一個region的起始行鍵是串聯起來的 //終止行鍵不包含在前一個region中,而是作為起始行鍵包含在后一個region中。 Pair<byte[][], byte[][]> pair = regionLocator.getStartEndKeys(); for(int n = 0; n < pair.getFirst().length; n++) { byte[] sk = pair.getFirst()[n]; byte[] ek = pair.getSecond()[n]; System.out.println("[" + (n + 1) + "]" + "start key:" + (sk.length == 8 ? Bytes.toLong(sk) : Bytes.toStringBinary(sk)) + " ,end key:" + (ek.length == 8 ? Bytes.toLong(ek) : Bytes.toStringBinary(ek))); } } /** * 通過預分區的方式建表 * @throws IOException */ public static void createTablePrePartition() { getConnection(); String tableName = "test_pre_partition1"; String tableName2 = "test_pre_partition2"; try { //獲取admin實例 Admin admin = connection.getAdmin(); //創建表描述符 HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName)); //添加列族描述符到表描述符中 HColumnDescriptor columnDescriptor = new HColumnDescriptor(Bytes.toBytes("info")); tableDescriptor.addFamily(columnDescriptor); //調用create方法,同時設置region邊界。 //能夠以特定數量拆分特定起始行鍵和特定終止行鍵,並創建表。 //startKey必須小於endKey,並且numRegions需要大於等於3,否則會拋出異常,這樣才能確保region有最小的集合 //此方法使用Bytes.split()方法計算region邊界,然后將計算得到的邊界作為已拆分邊界列表,並調用createTable(final HTableDescriptor desc, byte[][] splitKeys)方法 admin.createTable(tableDescriptor, Bytes.toBytes(1L), Bytes.toBytes(100L), 10); printTableRegions(tableName); //創建表中region的拆分行鍵 byte[][] regions = new byte[][] { Bytes.toBytes("A"), Bytes.toBytes("D"), Bytes.toBytes("G"), Bytes.toBytes("K"), Bytes.toBytes("O"), Bytes.toBytes("T") }; tableDescriptor.setName(TableName.valueOf(tableName2)); //使用新表明和region的已拆分鍵值列表作為參數調用建表命令 //使用已拆分行鍵的集合:使用了已經拆分好的region邊界列表,因此結果都是與預期相符的。 admin.createTable(tableDescriptor, regions); printTableRegions(tableName2); } catch (IOException e) { e.printStackTrace(); } finally { close(); } } }
還有createTableAsync方法,這個方法使用表描述符和預拆分的region邊界作為參數,並進行異步建表,但執行過程與createTable殊途同歸
同步模式僅僅是異步模式的簡單封裝,增加了不斷檢查這個任務是否已經完成的循環操作。