Search in sources :

Example 21 with HBaseAdmin

use of org.apache.hadoop.hbase.client.HBaseAdmin in project honeycomb by altamiracorp.

the class TableCreator method createTable.

/**
     * Creates a table in HBase to store all Honeycomb tables
     *
     * @param configuration Configuration of the HTable
     * @throws IOException
     */
public static void createTable(Configuration configuration) throws IOException {
    HTableDescriptor tableDescriptor;
    try {
        HBaseAdmin.checkHBaseAvailable(configuration);
    } catch (MasterNotRunningException e) {
        logger.fatal(String.format("HMaster doesn't appear to be running. Zookeeper quorum: %s", configuration.get("hbase.zookeeper.quorum")), e);
        throw e;
    } catch (ZooKeeperConnectionException e) {
        logger.fatal("Failed to connect to zookeeper when checking HBase. Zookeeper quorum: " + configuration.get("hbase.zookeeper.quorum"), e);
        throw e;
    }
    String columnFamily = configuration.get(ConfigConstants.COLUMN_FAMILY);
    byte[] tableName = configuration.get(ConfigConstants.TABLE_NAME).getBytes();
    HColumnDescriptor columnDescriptor = new HColumnDescriptor(columnFamily);
    HBaseAdmin admin = new HBaseAdmin(configuration);
    columnDescriptor.setBloomFilterType(StoreFile.BloomType.ROW).setDataBlockEncoding(DataBlockEncoding.PREFIX).setMaxVersions(1);
    if (!admin.tableExists(tableName)) {
        logger.info("Creating HBase table");
        tableDescriptor = new HTableDescriptor(tableName);
        tableDescriptor.addFamily(columnDescriptor);
        admin.createTable(tableDescriptor);
    }
    tableDescriptor = admin.getTableDescriptor(tableName);
    if (!tableDescriptor.hasFamily(columnFamily.getBytes())) {
        logger.info("Adding column family to HBase table");
        if (!admin.isTableDisabled(tableName)) {
            logger.info("Disabling HBase table");
            admin.disableTable(tableName);
        }
        admin.addColumn(tableName, columnDescriptor);
    }
    if (admin.isTableDisabled(tableName)) {
        logger.info("Enabling HBase table");
        admin.enableTable(tableName);
    }
    try {
        admin.flush(tableName);
    } catch (InterruptedException e) {
        logger.warn("HBaseAdmin flush was interrupted. Retrying.");
        try {
            admin.flush(tableName);
        } catch (InterruptedException e1) {
            throw new RuntimeException(e1);
        }
    }
    logger.info("HBase table successfully initialized.");
}
Also used : HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) MasterNotRunningException(org.apache.hadoop.hbase.MasterNotRunningException) ZooKeeperConnectionException(org.apache.hadoop.hbase.ZooKeeperConnectionException) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor)

Example 22 with HBaseAdmin

use of org.apache.hadoop.hbase.client.HBaseAdmin in project siena by mandubian.

the class HBaseDdlGenerator method dropTables.

public void dropTables() {
    HBaseConfiguration config = new HBaseConfiguration();
    try {
        HBaseAdmin admin = new HBaseAdmin(config);
        HTableDescriptor[] descriptors = admin.listTables();
        for (HTableDescriptor hTableDescriptor : descriptors) {
            String name = hTableDescriptor.getNameAsString();
            admin.disableTable(name);
            admin.deleteTable(name);
        }
    } catch (IOException e) {
        throw new SienaException(e);
    }
}
Also used : HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) HBaseConfiguration(org.apache.hadoop.hbase.HBaseConfiguration) IOException(java.io.IOException) SienaException(siena.SienaException) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor)

Example 23 with HBaseAdmin

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

the class HBaseClusterSingleton method ensureTable.

/**
   * Creates a table with the specified column families.
   * @param tableName the table name
   * @param cfs the column families
   * @throws IOException
   */
public void ensureTable(byte[] tableName, byte[][] cfs) throws IOException {
    HBaseAdmin admin = htu.getHBaseAdmin();
    if (!admin.tableExists(tableName)) {
        HTable hTable = htu.createTable(tableName, cfs);
        hTable.close();
    }
}
Also used : HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) HTable(org.apache.hadoop.hbase.client.HTable)

Example 24 with HBaseAdmin

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

the class HBaseClusterSingleton method truncateAllTables.

/**
   * Truncates all tables
   * @throws Exception
   */
public void truncateAllTables() throws Exception {
    HBaseAdmin admin = htu.getHBaseAdmin();
    for (HTableDescriptor table : admin.listTables()) {
        HTable hTable = htu.deleteTableData(table.getName());
        hTable.close();
    }
}
Also used : HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) HTable(org.apache.hadoop.hbase.client.HTable) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor)

Example 25 with HBaseAdmin

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

the class AlterTableIT method testAddNewColumnFamilyProperties.

@Test
public void testAddNewColumnFamilyProperties() throws Exception {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(getUrl(), props);
    conn.setAutoCommit(false);
    try {
        conn.createStatement().execute("CREATE TABLE " + dataTableFullName + "  (a_string varchar not null, col1 integer, cf1.col2 integer, col3 integer , cf2.col4 integer " + "  CONSTRAINT pk PRIMARY KEY (a_string)) " + generateDDLOptions("immutable_rows=true , SALT_BUCKETS=3 " + (!columnEncoded ? ",IMMUTABLE_STORAGE_SCHEME=" + PTable.ImmutableStorageScheme.ONE_CELL_PER_COLUMN : "")));
        String ddl = "Alter table " + dataTableFullName + " add cf3.col5 integer, cf4.col6 integer in_memory=true";
        conn.createStatement().execute(ddl);
        try (HBaseAdmin admin = conn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin()) {
            HTableDescriptor tableDesc = admin.getTableDescriptor(Bytes.toBytes(dataTableFullName));
            assertTrue(tableDesc.isCompactionEnabled());
            HColumnDescriptor[] columnFamilies = tableDesc.getColumnFamilies();
            assertEquals(5, columnFamilies.length);
            assertEquals("0", columnFamilies[0].getNameAsString());
            assertFalse(columnFamilies[0].isInMemory());
            assertEquals("CF1", columnFamilies[1].getNameAsString());
            assertFalse(columnFamilies[1].isInMemory());
            assertEquals("CF2", columnFamilies[2].getNameAsString());
            assertFalse(columnFamilies[2].isInMemory());
            assertEquals("CF3", columnFamilies[3].getNameAsString());
            assertTrue(columnFamilies[3].isInMemory());
            assertEquals("CF4", columnFamilies[4].getNameAsString());
            assertTrue(columnFamilies[4].isInMemory());
        }
    } finally {
        conn.close();
    }
}
Also used : HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) TestUtil.closeConnection(org.apache.phoenix.util.TestUtil.closeConnection) Properties(java.util.Properties) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) BaseTest(org.apache.phoenix.query.BaseTest) Test(org.junit.Test)

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