Search in sources :

Example 76 with HBaseAdmin

use of org.apache.hadoop.hbase.client.HBaseAdmin in project cdap by caskdata.

the class HBaseStreamConsumerStateStoreFactory method create.

@Override
public synchronized StreamConsumerStateStore create(StreamConfig streamConfig) throws IOException {
    NamespaceId namespace = streamConfig.getStreamId().getParent();
    TableId streamStateStoreTableId = StreamUtils.getStateStoreTableId(namespace);
    TableId hbaseTableId = tableUtil.createHTableId(new NamespaceId(streamStateStoreTableId.getNamespace()), streamStateStoreTableId.getTableName());
    boolean tableExist;
    try (HBaseAdmin admin = new HBaseAdmin(hConf)) {
        tableExist = tableUtil.tableExists(admin, hbaseTableId);
    }
    if (!tableExist) {
        try (HBaseDDLExecutor ddlExecutor = ddlExecutorFactory.get()) {
            TableDescriptorBuilder tdBuilder = HBaseTableUtil.getTableDescriptorBuilder(hbaseTableId, cConf);
            ColumnFamilyDescriptorBuilder cfdBuilder = HBaseTableUtil.getColumnFamilyDescriptorBuilder(Bytes.toString(QueueEntryRow.COLUMN_FAMILY), hConf);
            tdBuilder.addColumnFamily(cfdBuilder.build());
            ddlExecutor.createTableIfNotExists(tdBuilder.build(), null);
        }
    }
    HTable hTable = tableUtil.createHTable(hConf, hbaseTableId);
    hTable.setWriteBufferSize(Constants.Stream.HBASE_WRITE_BUFFER_SIZE);
    hTable.setAutoFlushTo(false);
    return new HBaseStreamConsumerStateStore(streamConfig, hTable);
}
Also used : TableId(co.cask.cdap.data2.util.TableId) HBaseDDLExecutor(co.cask.cdap.spi.hbase.HBaseDDLExecutor) HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) ColumnFamilyDescriptorBuilder(co.cask.cdap.data2.util.hbase.ColumnFamilyDescriptorBuilder) TableDescriptorBuilder(co.cask.cdap.data2.util.hbase.TableDescriptorBuilder) NamespaceId(co.cask.cdap.proto.id.NamespaceId) HTable(org.apache.hadoop.hbase.client.HTable)

Example 77 with HBaseAdmin

use of org.apache.hadoop.hbase.client.HBaseAdmin in project cdap by caskdata.

the class HBase10CDH550TableUpdater method createTableIfNotExists.

@Override
protected void createTableIfNotExists(Configuration conf) throws IOException {
    try (HBaseAdmin admin = new HBaseAdmin(conf)) {
        String tableName = StatusUtils.getReplicationStateTableName(conf);
        if (admin.tableExists(tableName)) {
            return;
        }
        HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
        htd.addFamily(new HColumnDescriptor(ReplicationConstants.ReplicationStatusTool.TIME_FAMILY));
        admin.createTable(htd);
        LOG.info("Created Table {}.", tableName);
    }
}
Also used : HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor)

Example 78 with HBaseAdmin

use of org.apache.hadoop.hbase.client.HBaseAdmin in project cdap by caskdata.

the class HBaseQueueAdmin method upgradeQueues.

private Map<TableId, Future<?>> upgradeQueues(final NamespaceMeta namespaceMeta, ExecutorService executor) throws Exception {
    try (HBaseAdmin admin = new HBaseAdmin(hConf)) {
        String hbaseNamespace = tableUtil.getHBaseNamespace(namespaceMeta);
        List<TableId> tableIds = tableUtil.listTablesInNamespace(admin, hbaseNamespace);
        List<TableId> stateStoreTableIds = Lists.newArrayList();
        Map<TableId, Future<?>> futures = new HashMap<>();
        for (final TableId tableId : tableIds) {
            // It's important to skip config table enabled.
            if (isDataTable(tableId)) {
                Runnable runnable = new Runnable() {

                    public void run() {
                        try {
                            LOG.info("Upgrading queue table: {}", tableId);
                            Properties properties = new Properties();
                            HTableDescriptor desc = tableUtil.getHTableDescriptor(admin, tableId);
                            if (desc.getValue(HBaseQueueAdmin.PROPERTY_PREFIX_BYTES) == null) {
                                // It's the old queue table. Set the property prefix bytes to SALT_BYTES
                                properties.setProperty(HBaseQueueAdmin.PROPERTY_PREFIX_BYTES, Integer.toString(SaltedHBaseQueueStrategy.SALT_BYTES));
                            }
                            upgrade(tableId, properties);
                            LOG.info("Upgraded queue table: {}", tableId);
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                };
                Future<?> future = executor.submit(runnable);
                futures.put(tableId, future);
            } else if (isStateStoreTable(tableId)) {
                stateStoreTableIds.add(tableId);
            }
        }
        // Upgrade of state store table
        for (final TableId tableId : stateStoreTableIds) {
            Runnable runnable = new Runnable() {

                public void run() {
                    try {
                        LOG.info("Upgrading queue state store: {}", tableId);
                        DatasetId stateStoreId = createStateStoreDataset(namespaceMeta.getName());
                        DatasetAdmin datasetAdmin = datasetFramework.getAdmin(stateStoreId, null);
                        if (datasetAdmin == null) {
                            LOG.error("No dataset admin available for {}", stateStoreId);
                            return;
                        }
                        datasetAdmin.upgrade();
                        LOG.info("Upgraded queue state store: {}", tableId);
                    } catch (Exception e) {
                        new RuntimeException(e);
                    }
                }
            };
            Future<?> future = executor.submit(runnable);
            futures.put(tableId, future);
        }
        return futures;
    }
}
Also used : TableId(co.cask.cdap.data2.util.TableId) HashMap(java.util.HashMap) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) DatasetProperties(co.cask.cdap.api.dataset.DatasetProperties) TableProperties(co.cask.cdap.api.dataset.table.TableProperties) Properties(java.util.Properties) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) DatasetId(co.cask.cdap.proto.id.DatasetId) HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) Future(java.util.concurrent.Future)

Example 79 with HBaseAdmin

use of org.apache.hadoop.hbase.client.HBaseAdmin in project cdap by caskdata.

the class AbstractHBaseTableUtilTest method beforeClass.

@BeforeClass
public static void beforeClass() throws Exception {
    hAdmin = new HBaseAdmin(TEST_HBASE.getConfiguration());
    cConf = CConfiguration.create();
    ddlExecutor = new HBaseDDLExecutorFactory(cConf, TEST_HBASE.getConfiguration()).get();
}
Also used : HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) BeforeClass(org.junit.BeforeClass)

Example 80 with HBaseAdmin

use of org.apache.hadoop.hbase.client.HBaseAdmin in project cdap by caskdata.

the class DistributedStorageProviderNamespaceAdmin method create.

@Override
public void create(NamespaceMeta namespaceMeta) throws IOException, ExploreException, SQLException {
    // create filesystem directory
    super.create(namespaceMeta);
    // skip namespace creation in HBase for default namespace
    if (NamespaceId.DEFAULT.equals(namespaceMeta.getNamespaceId())) {
        return;
    }
    // create HBase namespace and set group C(reate) permission if a group is configured
    String hbaseNamespace = tableUtil.getHBaseNamespace(namespaceMeta);
    if (Strings.isNullOrEmpty(namespaceMeta.getConfig().getHbaseNamespace())) {
        try (HBaseDDLExecutor executor = hBaseDDLExecutorFactory.get()) {
            boolean created = executor.createNamespaceIfNotExists(hbaseNamespace);
            if (namespaceMeta.getConfig().getGroupName() != null) {
                try {
                    executor.grantPermissions(hbaseNamespace, null, ImmutableMap.of("@" + namespaceMeta.getConfig().getGroupName(), "C"));
                } catch (IOException | RuntimeException e) {
                    // don't leave a partial state behind, as this fails the create(), the namespace should be removed
                    if (created) {
                        try {
                            executor.deleteNamespaceIfExists(hbaseNamespace);
                        } catch (Throwable t) {
                            e.addSuppressed(t);
                        }
                    }
                    throw e;
                }
            }
        } catch (Throwable t) {
            try {
                // if we failed to create a namespace in hbase then do clean up for above creations
                super.delete(namespaceMeta.getNamespaceId());
            } catch (Exception e) {
                t.addSuppressed(e);
            }
            throw t;
        }
    }
    try (HBaseAdmin admin = new HBaseAdmin(hConf)) {
        if (!tableUtil.hasNamespace(admin, hbaseNamespace)) {
            throw new IOException(String.format("HBase namespace '%s' specified for new namespace '%s' does not" + " exist. Please specify an existing HBase namespace.", hbaseNamespace, namespaceMeta.getName()));
        }
    }
}
Also used : HBaseDDLExecutor(co.cask.cdap.spi.hbase.HBaseDDLExecutor) HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) IOException(java.io.IOException) IOException(java.io.IOException) SQLException(java.sql.SQLException) ExploreException(co.cask.cdap.explore.service.ExploreException)

Aggregations

HBaseAdmin (org.apache.hadoop.hbase.client.HBaseAdmin)180 Test (org.junit.Test)93 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)76 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)72 HColumnDescriptor (org.apache.hadoop.hbase.HColumnDescriptor)70 Connection (java.sql.Connection)66 Properties (java.util.Properties)54 TableName (org.apache.hadoop.hbase.TableName)36 IOException (java.io.IOException)33 ResultSet (java.sql.ResultSet)27 BaseTest (org.apache.phoenix.query.BaseTest)27 HTable (org.apache.hadoop.hbase.client.HTable)26 SQLException (java.sql.SQLException)22 TestUtil.closeConnection (org.apache.phoenix.util.TestUtil.closeConnection)22 Put (org.apache.hadoop.hbase.client.Put)16 Configuration (org.apache.hadoop.conf.Configuration)13 HTableInterface (org.apache.hadoop.hbase.client.HTableInterface)13 PreparedStatement (java.sql.PreparedStatement)12 PhoenixIOException (org.apache.phoenix.exception.PhoenixIOException)12 HBaseConfiguration (org.apache.hadoop.hbase.HBaseConfiguration)9