Updated on 2022-11-18 GMT+08:00

Creating a Phoenix Table

Function

Phoenix can be installed on HBase to enable it to support SQL and JDBC APIs. This way, SQL users can access the HBase cluster.

Example Code

The following code snippet belongs to the testCreateTable method in the PhoenixSample class of the com.huawei.bigdata.hbase.examplespackage.

  /**
   * Create Table
   */
  public void testCreateTable() {
    LOG.info("Entering testCreateTable.");
    String URL = "jdbc:phoenix:" + conf.get("hbase.zookeeper.quorum");
    // Create table
    String createTableSQL =
        "CREATE TABLE IF NOT EXISTS TEST (id integer not null primary key, name varchar, "
            + "account char(6), birth date)";
    try (Connection conn = DriverManager.getConnection(url, props);
        Statement stat = conn.createStatement()) {
      // Execute Create SQL
      stat.executeUpdate(createTableSQL);
      LOG.info("Create table successfully.");
    } catch (Exception e) {
      LOG.error("Create table failed.", e);
    }
    LOG.info("Exiting testCreateTable.");
  }
  /**
   * Drop Table
   */
  public void testDrop() {
    LOG.info("Entering testDrop.");
    String URL = "jdbc:phoenix:" + conf.get("hbase.zookeeper.quorum");
    // Delete table
    String dropTableSQL = "DROP TABLE TEST";

    try (Connection conn = DriverManager.getConnection(url, props);
        Statement stat = conn.createStatement()) {
      stat.executeUpdate(dropTableSQL);
      LOG.info("Drop successfully.");
    } catch (Exception e) {
      LOG.error("Drop failed.", e);
    }
    LOG.info("Exiting testDrop.");
  }