Esta página aún no está disponible en su idioma local. Estamos trabajando arduamente para agregar más versiones de idiomas. Gracias por tu apoyo.

On this page

Reading the PhoenixTable

Updated on 2022-09-14 GMT+08:00

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.");
  }
Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback