Search in sources :

Example 1 with OneByteSimpleHash

use of io.cdap.cdap.hbase.wd.RowKeyDistributorByHashPrefix.OneByteSimpleHash in project cdap by caskdata.

the class HBaseTableFactory method createTable.

/**
 * Creates a new instance of {@link Table} for the given {@link TableId}. If the hbase table doesn't
 * exist, a new one will be created with the given number of splits.
 */
private HTableWithRowKeyDistributor createTable(TableId tableId, int splits, Class<? extends Coprocessor> coprocessor) throws IOException {
    // Lookup the table descriptor from the cache first. If it is there, we assume the HBase table exists
    // Otherwise, attempt to create it.
    Table table = null;
    HTableDescriptor htd = tableDescriptors.get(tableId);
    if (htd == null) {
        synchronized (this) {
            htd = tableDescriptors.get(tableId);
            if (htd == null) {
                boolean tableExists;
                try (HBaseAdmin admin = new HBaseAdmin(hConf)) {
                    tableExists = tableUtil.tableExists(admin, tableId);
                }
                // Create the table if the table doesn't exist
                try (HBaseDDLExecutor ddlExecutor = ddlExecutorFactory.get()) {
                    // If table exists, then skip creating coprocessor etc
                    if (!tableExists) {
                        TableId metadataTableId = tableUtil.createHTableId(NamespaceId.SYSTEM, cConf.get(Constants.MessagingSystem.METADATA_TABLE_NAME));
                        ColumnFamilyDescriptorBuilder cfdBuilder = HBaseTableUtil.getColumnFamilyDescriptorBuilder(Bytes.toString(COLUMN_FAMILY), hConf);
                        TableDescriptorBuilder tdBuilder = HBaseTableUtil.getTableDescriptorBuilder(tableId, cConf).addColumnFamily(cfdBuilder.build()).addProperty(Constants.MessagingSystem.HBASE_MESSAGING_TABLE_PREFIX_NUM_BYTES, Integer.toString(1)).addProperty(Constants.MessagingSystem.KEY_DISTRIBUTOR_BUCKETS_ATTR, Integer.toString(splits)).addProperty(Constants.MessagingSystem.HBASE_METADATA_TABLE_NAMESPACE, metadataTableId.getNamespace()).addProperty(HTableDescriptor.SPLIT_POLICY, cConf.get(Constants.MessagingSystem.TABLE_HBASE_SPLIT_POLICY)).addCoprocessor(coprocessorManager.getCoprocessorDescriptor(coprocessor, Coprocessor.PRIORITY_USER));
                        // Set the key distributor size the same as the initial number of splits,
                        // essentially one bucket per split.
                        byte[][] splitKeys = HBaseTableUtil.getSplitKeys(splits, splits, new RowKeyDistributorByHashPrefix(new OneByteSimpleHash(splits)));
                        ddlExecutor.createTableIfNotExists(tdBuilder.build(), splitKeys);
                        table = tableUtil.createTable(hConf, tableId);
                        htd = table.getTableDescriptor();
                        tableDescriptors.put(tableId, htd);
                    } else {
                        table = tableUtil.createTable(hConf, tableId);
                        htd = table.getTableDescriptor();
                        tableDescriptors.put(tableId, htd);
                    }
                }
            }
        }
    }
    if (table == null) {
        table = tableUtil.createTable(hConf, tableId);
    }
    return new HTableWithRowKeyDistributor(table, new RowKeyDistributorByHashPrefix(new OneByteSimpleHash(getKeyDistributorBuckets(tableId, htd))));
}
Also used : HBaseDDLExecutor(io.cdap.cdap.spi.hbase.HBaseDDLExecutor) TableId(io.cdap.cdap.data2.util.TableId) HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) MetadataTable(io.cdap.cdap.messaging.store.MetadataTable) MessageTable(io.cdap.cdap.messaging.store.MessageTable) PayloadTable(io.cdap.cdap.messaging.store.PayloadTable) Table(org.apache.hadoop.hbase.client.Table) RowKeyDistributorByHashPrefix(io.cdap.cdap.hbase.wd.RowKeyDistributorByHashPrefix) ColumnFamilyDescriptorBuilder(io.cdap.cdap.data2.util.hbase.ColumnFamilyDescriptorBuilder) OneByteSimpleHash(io.cdap.cdap.hbase.wd.RowKeyDistributorByHashPrefix.OneByteSimpleHash) HTableDescriptorBuilder(io.cdap.cdap.data2.util.hbase.HTableDescriptorBuilder) TableDescriptorBuilder(io.cdap.cdap.data2.util.hbase.TableDescriptorBuilder) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor)

Aggregations

TableId (io.cdap.cdap.data2.util.TableId)1 ColumnFamilyDescriptorBuilder (io.cdap.cdap.data2.util.hbase.ColumnFamilyDescriptorBuilder)1 HTableDescriptorBuilder (io.cdap.cdap.data2.util.hbase.HTableDescriptorBuilder)1 TableDescriptorBuilder (io.cdap.cdap.data2.util.hbase.TableDescriptorBuilder)1 RowKeyDistributorByHashPrefix (io.cdap.cdap.hbase.wd.RowKeyDistributorByHashPrefix)1 OneByteSimpleHash (io.cdap.cdap.hbase.wd.RowKeyDistributorByHashPrefix.OneByteSimpleHash)1 MessageTable (io.cdap.cdap.messaging.store.MessageTable)1 MetadataTable (io.cdap.cdap.messaging.store.MetadataTable)1 PayloadTable (io.cdap.cdap.messaging.store.PayloadTable)1 HBaseDDLExecutor (io.cdap.cdap.spi.hbase.HBaseDDLExecutor)1 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)1 HBaseAdmin (org.apache.hadoop.hbase.client.HBaseAdmin)1 Table (org.apache.hadoop.hbase.client.Table)1