@Override public void close() throws HiveSQLException { setState(OperationState.CLOSED); if (driver != null) { driver.close(); driver.destroy(); } SessionState session = SessionState.get(); if (session.getTmpOutputFile() != null) { session.getTmpOutputFile().delete(); } }
/** * Reads the temporary results for non-Hive (non-Driver) commands to the resulting List of * strings. * * @param nLines number of lines read at once. If it is <= 0, then read all lines. */ private List<String> readResults(int nLines) throws HiveSQLException { if (resultReader == null) { SessionState sessionState = getParentSession().getSessionState(); File tmp = sessionState.getTmpOutputFile(); try { resultReader = new BufferedReader(new FileReader(tmp)); } catch (FileNotFoundException e) { LOG.error("File " + tmp + " not found. ", e); throw new HiveSQLException(e); } } List<String> results = new ArrayList<String>(); for (int i = 0; i < nLines || nLines <= 0; ++i) { try { String line = resultReader.readLine(); if (line == null) { // reached the end of the result file break; } else { results.add(line); } } catch (IOException e) { LOG.error("Reading temp results encountered an exception: ", e); throw new HiveSQLException(e); } } return results; }
/** * set current session to existing session object if a thread is running multiple sessions - it * must call this method with the new session object when switching from one session to another. */ public static SessionState start(SessionState startSs) { setCurrentSessionState(startSs); if (startSs.hiveHist == null) { if (startSs.getConf().getBoolVar(HiveConf.ConfVars.HIVE_SESSION_HISTORY_ENABLED)) { startSs.hiveHist = new HiveHistoryImpl(startSs); } else { // Hive history is disabled, create a no-op proxy startSs.hiveHist = HiveHistoryProxyHandler.getNoOpHiveHistoryProxy(); } } // Get the following out of the way when you start the session these take a // while and should be done when we start up. try { // Hive object instance should be created with a copy of the conf object. If the conf is // shared with SessionState, other parts of the code might update the config, but // Hive.get(HiveConf) would not recognize the case when it needs refreshing Hive.get(new HiveConf(startSs.conf)).getMSC(); UserGroupInformation sessionUGI = Utils.getUGI(); FileSystem.get(startSs.conf); // Create scratch dirs for this session startSs.createSessionDirs(sessionUGI.getShortUserName()); // Set temp file containing results to be sent to HiveClient if (startSs.getTmpOutputFile() == null) { try { startSs.setTmpOutputFile(createTempFile(startSs.getConf())); } catch (IOException e) { throw new RuntimeException(e); } } } catch (Exception e) { // Catch-all due to some exec time dependencies on session state // that would cause ClassNoFoundException otherwise throw new RuntimeException(e); } if (HiveConf.getVar(startSs.getConf(), HiveConf.ConfVars.HIVE_EXECUTION_ENGINE).equals("tez") && (startSs.isHiveServerQuery == false)) { try { if (startSs.tezSessionState == null) { startSs.tezSessionState = new TezSessionState(startSs.getSessionId()); } if (!startSs.tezSessionState.isOpen()) { startSs.tezSessionState.open(startSs.conf); // should use conf on session start-up } } catch (Exception e) { throw new RuntimeException(e); } } else { LOG.info("No Tez session required at this point. hive.execution.engine=mr."); } return startSs; }
private void setupSessionIO(SessionState sessionState) { try { LOG.info("Putting temp output to file " + sessionState.getTmpOutputFile().toString()); sessionState.in = null; // hive server's session input stream is not used // open a per-session file in auto-flush mode for writing temp results sessionState.out = new PrintStream(new FileOutputStream(sessionState.getTmpOutputFile()), true, "UTF-8"); // TODO: for hadoop jobs, progress is printed out to session.err, // we should find a way to feed back job progress to client sessionState.err = new PrintStream(System.err, true, "UTF-8"); } catch (IOException e) { LOG.error("Error in creating temp output file ", e); try { sessionState.in = null; sessionState.out = new PrintStream(System.out, true, "UTF-8"); sessionState.err = new PrintStream(System.err, true, "UTF-8"); } catch (UnsupportedEncodingException ee) { LOG.error("Error creating PrintStream", e); ee.printStackTrace(); sessionState.out = null; sessionState.err = null; } } }
/** * set current session to existing session object if a thread is running multiple sessions - it * must call this method with the new session object when switching from one session to another. * * @throws HiveException */ public static SessionState start(SessionState startSs) { tss.set(startSs); if (StringUtils.isEmpty(startSs.getConf().getVar(HiveConf.ConfVars.HIVESESSIONID))) { startSs.getConf().setVar(HiveConf.ConfVars.HIVESESSIONID, makeSessionId()); } if (startSs.hiveHist == null) { startSs.hiveHist = new HiveHistory(startSs); } if (startSs.getTmpOutputFile() == null) { // per-session temp file containing results to be sent from HiveServer to HiveClient File tmpDir = new File(HiveConf.getVar(startSs.getConf(), HiveConf.ConfVars.HIVEHISTORYFILELOC)); String sessionID = startSs.getConf().getVar(HiveConf.ConfVars.HIVESESSIONID); try { File tmpFile = File.createTempFile(sessionID, ".pipeout", tmpDir); tmpFile.deleteOnExit(); startSs.setTmpOutputFile(tmpFile); } catch (IOException e) { throw new RuntimeException(e); } } try { startSs.authenticator = HiveUtils.getAuthenticator(startSs.getConf()); startSs.authorizer = HiveUtils.getAuthorizeProviderManager(startSs.getConf(), startSs.authenticator); startSs.createTableGrants = CreateTableAutomaticGrant.create(startSs.getConf()); } catch (HiveException e) { throw new RuntimeException(e); } return startSs; }
private void cleanTmpFile() { resetResultReader(); SessionState sessionState = getParentSession().getSessionState(); File tmp = sessionState.getTmpOutputFile(); tmp.delete(); }