MAPREDUCE服务 MRS-创建Phoenix表:代码样例

时间:2024-06-13 09:39:11

代码样例

以下代码片段在com.huawei.bigdata.hbase.examples包的“PhoenixSample”类的testCreateTable方法中。

  /**
   * 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.");
  }
support.huaweicloud.com/devg-lts-mrs/mrs_07_080036.html