コード例 #1
0
  /**
   * Creates a HiveQL external table.
   *
   * @param query
   * @return True if the table could be created, false otherwise.
   */
  public boolean doCreateTable(String query) {
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    boolean res = true;

    try {
      // get a connection to the Hive/Shark server
      con = getConnection();

      // create a statement
      stmt = con.createStatement();

      // execute the query
      rs = stmt.executeQuery(query);
    } catch (Throwable e) {
      LOGGER.error(
          "Runtime error (The Hive table cannot be created. Hive query='"
              + query
              + "'. Details="
              + e.getMessage()
              + ")");
      res = false;
    } finally {
      return res && closeHiveObjects(con, stmt, rs);
    } // try catch finally
  } // doCreateTable
コード例 #2
0
  /**
   * Close all the Hive objects previously opened by doCreateTable and doQuery.
   *
   * @param con
   * @param stmt
   * @param rs
   * @return True if the Hive objects have been closed, false otherwise.
   */
  private boolean closeHiveObjects(Connection con, Statement stmt, ResultSet rs) {
    boolean res = true;

    if (con != null) {
      try {
        con.close();
      } catch (SQLException e) {
        LOGGER.error(
            "Runtime error (The Hive connection could not be closed. Details="
                + e.getMessage()
                + ")");
        res = false;
      } // try catch // try catch
    } // if

    if (stmt != null) {
      try {
        stmt.close();
      } catch (SQLException e) {
        LOGGER.error(
            "Runtime error (The Hive statement could not be closed. Details="
                + e.getMessage()
                + ")");
        res = false;
      } // try catch // try catch
    } // if

    if (rs != null) {
      try {
        rs.close();
      } catch (SQLException e) {
        LOGGER.error(
            "Runtime error (The Hive result set could not be closed. Details="
                + e.getMessage()
                + ")");
        res = false;
      } // try catch // try catch
    } // if

    return res;
  } // closeHiveObjects
コード例 #3
0
  /**
   * Executes a HiveQL sentence.
   *
   * @param query
   * @return True if the query succeded, false otherwise.
   */
  public boolean doQuery(String query) {
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    boolean res = true;

    try {
      // get a connection to the Hive/Shark server
      con = getConnection();

      // create a statement
      stmt = con.createStatement();

      // execute the query
      rs = stmt.executeQuery(query);

      // iterate on the result
      while (rs.next()) {
        String s = "";

        for (int i = 1; i < rs.getMetaData().getColumnCount(); i++) {
          s += rs.getString(i) + ",";
        } // for

        s += rs.getString(rs.getMetaData().getColumnCount());
      } // while
    } catch (Throwable e) {
      LOGGER.error(
          "Runtime error (The Hive query cannot be executed. Hive query='"
              + query
              + "'. Details="
              + e.getMessage()
              + ")");
      res = false;
    } finally {
      return res && closeHiveObjects(con, stmt, rs);
    } // try catch finally
  } // doQuery
コード例 #4
0
  /**
   * Gets a connection to the Hive server.
   *
   * @return
   * @throws Exception
   */
  private Connection getConnection() throws Exception {
    // dynamically load the Hive JDBC driver
    Class.forName(DRIVERNAME);

    // return a connection based on the Hive JDBC driver
    LOGGER.debug(
        "Connecting to jdbc:hive://"
            + hiveServer
            + ":"
            + hivePort
            + "/default?user="******"&password=XXXXXXXXXX");
    return DriverManager.getConnection(
        "jdbc:hive://"
            + hiveServer
            + ":"
            + hivePort
            + "/default?user="******"&password="
            + hadoopPassword);
  } // getConnection