Search in sources :

Example 1 with BeelineInPlaceUpdateStream

use of org.apache.hive.beeline.logs.BeelineInPlaceUpdateStream in project hive by apache.

the class Commands method executeInternal.

// Return false only occurred error when execution the sql and the sql should follow the rules
// of beeline.
private boolean executeInternal(String sql, boolean call) {
    if (!beeLine.isBeeLine()) {
        sql = cliToBeelineCmd(sql);
    }
    if (sql == null || sql.length() == 0) {
        return true;
    }
    if (beeLine.isComment(sql)) {
        // skip this and rest cmds in the line
        return true;
    }
    // is source CMD
    if (isSourceCMD(sql)) {
        return sourceFile(sql);
    }
    if (sql.startsWith(BeeLine.COMMAND_PREFIX)) {
        return beeLine.execCommandWithPrefix(sql);
    }
    String prefix = call ? "call" : "sql";
    if (sql.startsWith(prefix)) {
        sql = sql.substring(prefix.length());
    }
    // batch statements?
    if (beeLine.getBatch() != null) {
        beeLine.getBatch().add(sql);
        return true;
    }
    if (!(beeLine.assertConnection())) {
        return false;
    }
    ClientHook hook = ClientCommandHookFactory.get().getHook(beeLine, sql);
    try {
        Statement stmnt = null;
        boolean hasResults;
        Thread logThread = null;
        try {
            long start = System.currentTimeMillis();
            if (call) {
                stmnt = beeLine.getDatabaseConnection().getConnection().prepareCall(sql);
                hasResults = ((CallableStatement) stmnt).execute();
            } else {
                stmnt = beeLine.createStatement();
                // In test mode we want the operation logs regardless of the settings
                if (!beeLine.isTestMode() && beeLine.getOpts().isSilent()) {
                    hasResults = stmnt.execute(sql);
                } else {
                    InPlaceUpdateStream.EventNotifier eventNotifier = new InPlaceUpdateStream.EventNotifier();
                    logThread = new Thread(createLogRunnable(stmnt, eventNotifier));
                    logThread.setDaemon(true);
                    logThread.start();
                    if (stmnt instanceof HiveStatement) {
                        HiveStatement hiveStatement = (HiveStatement) stmnt;
                        hiveStatement.setInPlaceUpdateStream(new BeelineInPlaceUpdateStream(beeLine.getErrorStream(), eventNotifier));
                    }
                    hasResults = stmnt.execute(sql);
                    logThread.interrupt();
                    logThread.join(DEFAULT_QUERY_PROGRESS_THREAD_TIMEOUT);
                }
            }
            beeLine.showWarnings();
            if (hasResults) {
                OutputFile outputFile = beeLine.getRecordOutputFile();
                if (beeLine.isTestMode() && outputFile != null && outputFile.isActiveConverter()) {
                    outputFile.fetchStarted();
                    if (!sql.trim().toLowerCase().startsWith("explain")) {
                        outputFile.foundQuery(true);
                    } else {
                        outputFile.foundQuery(false);
                    }
                }
                do {
                    ResultSet rs = stmnt.getResultSet();
                    try {
                        int count = beeLine.print(rs);
                        long end = System.currentTimeMillis();
                        if (showReport()) {
                            beeLine.output(beeLine.loc("rows-selected", count) + " " + beeLine.locElapsedTime(end - start), true, beeLine.getErrorStream());
                        }
                    } finally {
                        if (logThread != null) {
                            logThread.join(DEFAULT_QUERY_PROGRESS_THREAD_TIMEOUT);
                            showRemainingLogsIfAny(stmnt);
                            logThread = null;
                        }
                        rs.close();
                    }
                } while (BeeLine.getMoreResults(stmnt));
                if (beeLine.isTestMode() && outputFile != null && outputFile.isActiveConverter()) {
                    outputFile.fetchFinished();
                }
            } else {
                int count = stmnt.getUpdateCount();
                long end = System.currentTimeMillis();
                if (showReport()) {
                    beeLine.output(beeLine.loc("rows-affected", count) + " " + beeLine.locElapsedTime(end - start), true, beeLine.getErrorStream());
                }
            }
        } finally {
            if (logThread != null) {
                if (!logThread.isInterrupted()) {
                    logThread.interrupt();
                }
                logThread.join(DEFAULT_QUERY_PROGRESS_THREAD_TIMEOUT);
                if (stmnt != null) {
                    showRemainingLogsIfAny(stmnt);
                }
            }
            if (stmnt != null) {
                stmnt.close();
            }
        }
    } catch (Exception e) {
        return beeLine.error(e);
    }
    beeLine.showWarnings();
    if (hook != null) {
        hook.postHook(beeLine);
    }
    return true;
}
Also used : HiveStatement(org.apache.hive.jdbc.HiveStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) IOException(java.io.IOException) BeelineInPlaceUpdateStream(org.apache.hive.beeline.logs.BeelineInPlaceUpdateStream) InPlaceUpdateStream(org.apache.hive.jdbc.logs.InPlaceUpdateStream) ResultSet(java.sql.ResultSet) HiveStatement(org.apache.hive.jdbc.HiveStatement) BeelineInPlaceUpdateStream(org.apache.hive.beeline.logs.BeelineInPlaceUpdateStream)

Aggregations

IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 CallableStatement (java.sql.CallableStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 BeelineInPlaceUpdateStream (org.apache.hive.beeline.logs.BeelineInPlaceUpdateStream)1 HiveStatement (org.apache.hive.jdbc.HiveStatement)1 InPlaceUpdateStream (org.apache.hive.jdbc.logs.InPlaceUpdateStream)1