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

Reading the PhoenixTable

Function

The Phoenix table enables data reading.

Example Code

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

  /**
   * Select Data
   */
  public void testSelect() {
    LOG.info("Entering testSelect.");
    String URL = "jdbc:phoenix:" + conf.get("hbase.zookeeper.quorum");
    // Query
    String querySQL = "SELECT * FROM TEST WHERE id = ?";
    Connection conn = null;
    PreparedStatement preStat = null;
    Statement stat = null;
    ResultSet result = null;
    try {
      // Create Connection
      conn = DriverManager.getConnection(url, props);
      // Create Statement
      stat = conn.createStatement();
      // Create PrepareStatement
      preStat = conn.prepareStatement(querySQL);
      // Execute query
      preStat.setInt(1, 1);
      result = preStat.executeQuery();
      // Get result
      while (result.next()) {
        int id = result.getInt("id");
        String name = result.getString(1);
        System.out.println("id: " + id);
        System.out.println("name: " + name);
      }
      LOG.info("Select successfully.");
    } catch (Exception e) {
      LOG.error("Select failed.", e);
    } finally {
      if (null != result) {
        try {
          result.close();
        } catch (Exception e2) {
          LOG.error("Result close failed.", e2);
        }
      }
      if (null != stat) {
        try {
          stat.close();
        } catch (Exception e2) {
          LOG.error("Stat close failed.", e2);
        }
      }
      if (null != conn) {
        try {
          conn.close();
        } catch (Exception e2) {
          LOG.error("Connection close failed.", e2);
        }
      }
    }
    LOG.info("Exiting testSelect.");
  }