diff --git common/src/java/org/apache/hadoop/hive/conf/HiveConf.java common/src/java/org/apache/hadoop/hive/conf/HiveConf.java index 5c1b283..db7a9b6 100644 --- common/src/java/org/apache/hadoop/hive/conf/HiveConf.java +++ common/src/java/org/apache/hadoop/hive/conf/HiveConf.java @@ -715,6 +715,8 @@ HIVE_SERVER2_PLAIN_LDAP_BASEDN("hive.server2.authentication.ldap.baseDN", null), HIVE_SERVER2_CUSTOM_AUTHENTICATION_CLASS("hive.server2.custom.authentication.class", null), HIVE_SERVER2_ENABLE_DOAS("hive.server2.enable.doAs", true), + HIVE_SERVER2_SESSION("hive.server2.session", ""), + HIVE_SERVER2_REMOVE_RESOURCE_DIR("hive.server2.remove.resource.dir", false), HIVE_CONF_RESTRICTED_LIST("hive.conf.restricted.list", null), diff --git ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java index d8c91bd..bef0ce3 100644 --- ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java +++ ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java @@ -590,35 +590,15 @@ public static boolean canDownloadResource(String value) { private String downloadResource(String value, boolean convertToUnix) { if (canDownloadResource(value)) { getConsole().printInfo("converting to local " + value); - String location = getConf().getVar(HiveConf.ConfVars.DOWNLOADED_RESOURCES_DIR); - + File resourceDir = new File(getConf().getVar(HiveConf.ConfVars.DOWNLOADED_RESOURCES_DIR)); String destinationName = new Path(value).getName(); - String prefix = destinationName; - String postfix = null; - int index = destinationName.lastIndexOf("."); - if (index > 0) { - prefix = destinationName.substring(0, index); - postfix = destinationName.substring(index); - } - if (prefix.length() < 3) { - prefix += ".tmp"; // prefix should be longer than 3 - } - - File resourceDir = new File(location); - if (resourceDir.exists() && !resourceDir.isDirectory()) { - throw new RuntimeException("The resource directory is not a directory, " + - "resourceDir is set to " + resourceDir); + File destinationFile = new File(resourceDir, destinationName); + if ( resourceDir.exists() && ! resourceDir.isDirectory() ) { + throw new RuntimeException("The resource directory is not a directory, resourceDir is set to" + resourceDir); } - if (!resourceDir.exists() && !resourceDir.mkdirs()) { + if ( ! resourceDir.exists() && ! resourceDir.mkdirs() ) { throw new RuntimeException("Couldn't create directory " + resourceDir); } - - File destinationFile; - try { - destinationFile = File.createTempFile(prefix, postfix, resourceDir); - } catch (Exception e) { - throw new RuntimeException("Failed to create temporary file for " + value, e); - } try { FileSystem fs = FileSystem.get(new URI(value), conf); fs.copyToLocalFile(new Path(value), new Path(destinationFile.getCanonicalPath())); @@ -627,8 +607,7 @@ private String downloadResource(String value, boolean convertToUnix) { try { DosToUnix.convertWindowsScriptToUnix(destinationFile); } catch (Exception e) { - throw new RuntimeException("Caught exception while converting file " + - destinationFile + " to unix line endings", e); + throw new RuntimeException("Caught exception while converting to unix line endings", e); } } } catch (Exception e) { diff --git service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java index 8f0adb5..310456b 100644 --- service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java +++ service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java @@ -18,13 +18,19 @@ package org.apache.hive.service.cli.session; +import java.io.File; +import java.io.IOException; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import org.apache.commons.io.FileUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.conf.HiveConf.ConfVars; import org.apache.hadoop.hive.metastore.HiveMetaStoreClient; import org.apache.hadoop.hive.metastore.IMetaStoreClient; import org.apache.hadoop.hive.metastore.api.MetaException; @@ -63,6 +69,8 @@ private static final String FETCH_WORK_SERDE_CLASS = "org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe"; + private static final Log LOG = LogFactory.getLog(HiveSessionImpl.class); + private SessionManager sessionManager; private OperationManager operationManager; @@ -80,6 +88,8 @@ public HiveSessionImpl(String username, String password, Map ses } sessionState = new SessionState(hiveConf); + hiveConf.set(ConfVars.HIVE_SERVER2_SESSION.varname, + sessionHandle.getHandleIdentifier().toString()); } private SessionManager getSessionManager() { @@ -296,6 +306,7 @@ public void close() throws HiveSQLException { } opHandleSet.clear(); } finally { + removeResourceDir(); release(); } } @@ -367,4 +378,22 @@ public RowSet fetchResults(OperationHandle opHandle) throws HiveSQLException { protected HiveSession getSession() { return this; } + + // remove the resource directory if configured + private void removeResourceDir() { + if (hiveConf.getBoolVar(ConfVars.HIVE_SERVER2_REMOVE_RESOURCE_DIR)) { + try { + File resourceDir = + new File(hiveConf.getVar(HiveConf.ConfVars.DOWNLOADED_RESOURCES_DIR)); + if (resourceDir.exists()) { + FileUtils.deleteDirectory(resourceDir); + } + } catch (IOException e) { + LOG.info("Error removing session resource dir " + + HiveConf.ConfVars.DOWNLOADED_RESOURCES_DIR.varname, e); + } + } + } + + } \ No newline at end of file