hive Context

Driver:
public int compile(String command) {
ctx = new Context(conf); //


}


public Context(Configuration conf) throws IOException {
this(conf, generateExecutionId());
}

/**
* Generate a unique executionId. An executionId, together with user name and
* the configuration, will determine the temporary locations of all intermediate
* files.
*
* In the future, users can use the executionId to resume a query.
*/
public static String generateExecutionId() {
Random rand = new Random();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss_SSS");
String executionId = "hive_" + format.format(new Date()) + "_"
+ Math.abs(rand.nextLong());
return executionId;
}
executionId的例子是:hive_2011-08-21_00-02-22_445_7799135143086468923
hive_yyyy-MM-dd_HH-mm-ss_SSS_长整型随机数

/**
* Create a Context with a given executionId. ExecutionId, together with
* user name and conf, will determine the temporary directory locations.
*/
public Context(Configuration conf, String executionId) throws IOException {
this.conf = conf;
this.executionId = executionId; //hive_2011-08-21_00-02-22_445_7799135143086468923

// non-local tmp location is configurable. however it is the same across
// all external file systems
nonLocalScratchPath =
new Path(HiveConf.getVar(conf, HiveConf.ConfVars.SCRATCHDIR),
executionId); // /tmp/hive-tianzhao/hive_2011-08-21_00-02-22_445_7799135143086468923
// HiveConf SCRATCHDIR("hive.exec.scratchdir", "/tmp/" + System.getProperty("user.name") + "/hive"),
hive-default.xml文件中默认的配置是:
<property>
<name>hive.exec.scratchdir</name>
<value>/tmp/hive-${user.name}</value>
<description>Scratch space for Hive jobs</description>
</property>


// local tmp location is not configurable for now
localScratchDir = System.getProperty("java.io.tmpdir")
+ Path.SEPARATOR + System.getProperty("user.name") + Path.SEPARATOR
+ executionId; // /tmp/tianzhao/hive_2011-08-21_00-02-22_445_7799135143086468923
}
// 本地临时目录 System.getProperty("java.io.tmpdir") = /tmp
// System.getProperty("user.name") = tianzhao


/**
* Get a tmp path on local host to store intermediate data.
*
* @return next available tmp path on local fs
*/
public String getLocalTmpFileURI() {
return getLocalScratchDir(true) + Path.SEPARATOR + LOCAL_PREFIX +
nextPathId(); // file:/tmp/tianzhao/hive_2011-09-06_03-39-09_416_8623373648168603461/-local-10003
}

private String [b]getScratchDir[/b](String scheme, String authority,
boolean mkdir, String scratchDir) {

String fileSystem = scheme + ":" + authority; // file:null
String dir = fsScratchDirs.get(fileSystem); //

if (dir == null) {
Path dirPath = new Path(scheme, authority, scratchDir);
if (mkdir) { // true
try {
FileSystem fs = dirPath.getFileSystem(conf);
dirPath = new Path(fs.makeQualified(dirPath).toString());
if (!fs.mkdirs(dirPath)) {
throw new RuntimeException("Cannot make directory: "
+ dirPath.toString());
}
} catch (IOException e) {
throw new RuntimeException (e);
}
}
dir = dirPath.toString(); // file:/tmp/tianzhao/hive_2011-09-06_03-39-09_416_8623373648168603461
fsScratchDirs.put(fileSystem, dir); // {file:null=file:/tmp/tianzhao/hive_2011-09-06_03-39-09_416_8623373648168603461, hdfs:localhost:54310=hdfs://localhost:54310/tmp/hive-tianzhao/hive_2011-09-06_03-39-09_416_8623373648168603461}
}
return dir; //file:/tmp/tianzhao/hive_2011-09-06_03-39-09_416_8623373648168603461
}


/**
* Get a path to store map-reduce intermediate data in.
*
* @return next available path for map-red intermediate data
*/
public String getMRTmpFileURI() {
return getMRScratchDir() + Path.SEPARATOR + MR_PREFIX +
nextPathId(); // hdfs://localhost:54310/tmp/hive-tianzhao/hive_2011-09-06_03-39-09_416_8623373648168603461/-mr-10004
}


public String [b]getMRScratchDir[/b]() {

// if we are executing entirely on the client side - then
// just (re)use the local scratch directory
if(isLocalOnlyExecutionMode()) {
return getLocalScratchDir(!explain);
}

try {
// nonLocalScratchPath=/tmp/hive-tianzhao/hive_2011-09-06_03-39-09_416_8623373648168603461
Path dir = FileUtils.makeQualified(nonLocalScratchPath, conf); // hdfs://localhost:54310/tmp/hive-tianzhao/hive_2011-09-06_03-39-09_416_8623373648168603461
URI uri = dir.toUri();// hdfs://localhost:54310/tmp/hive-tianzhao/hive_2011-09-06_03-39-09_416_8623373648168603461
return getScratchDir(uri.getScheme(), uri.getAuthority(),
!explain, uri.getPath());

} catch (IOException e) {
throw new RuntimeException(e);
} catch (IllegalArgumentException e) {
throw new RuntimeException("Error while making MR scratch "
+ "directory - check filesystem config (" + e.getCause() + ")", e);
}
}


/**
* Get a tmp directory on specified URI
*
* @param scheme Scheme of the target FS
* @param authority Authority of the target FS
* @param mkdir create the directory if true
* @param scratchdir path of tmp directory
*/
private String getScratchDir(String scheme, String authority,
boolean mkdir, String scratchDir) {

String fileSystem = scheme + ":" + authority; //hdfs:localhost:54310
String dir = fsScratchDirs.get(fileSystem); // hdfs://localhost:54310/tmp/hive-tianzhao/hive_2011-09-06_03-39-09_416_8623373648168603461

if (dir == null) {
Path dirPath = new Path(scheme, authority, scratchDir);
if (mkdir) {
try {
FileSystem fs = dirPath.getFileSystem(conf);
dirPath = new Path(fs.makeQualified(dirPath).toString());
if (!fs.mkdirs(dirPath)) {
throw new RuntimeException("Cannot make directory: "
+ dirPath.toString());
}
} catch (IOException e) {
throw new RuntimeException (e);
}
}
dir = dirPath.toString();
fsScratchDirs.put(fileSystem, dir);
}
return dir; //hdfs://localhost:54310/tmp/hive-tianzhao/hive_2011-09-06_03-39-09_416_8623373648168603461
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值