QQ
交流群:335671559,欢迎交流
问题描述:
已有
(外部/内部)
表test,新建分区时指定数据位置,如下
alter table test add partition(day='20140101')
location '20140101';
这样会默认在表warehouse路径下生成/{warehouse}/test/20140101/这种格式目录
同时使用命令 desc formatted test partition(day='20140101')可以查看到相应的location为
hdfs://..:../{warehouse}/test/20140101/
然后使用insert overwrite向分区插入数据
insert overwrite table test partition (day='20140101')
select xx from xx....;
正常情况下一切正常,但是当设置属性
fs.hdfs.impl.disable.cache为
true时,会出现以下情况
desc formatted test partition(day='20140101')时发现location变成了以下格式
hdfs://..:../{warehouse}/test/day=20140101/
同时会在hdfs上
生成一个新的目录/{warehouse}/test/day=20140101/,而此
分区之前的location路径会被删掉,即
/{warehouse}/test/20140101/这个路径被删除
解决:
(1)先看hql的执行计划,大概如下
(2)找到 Move Operator对应的源码task类, org.apache.hadoop.hive.ql.exec.MoveTask.javaStage: Stage-1Map ReduceAlias -> Map Operator Tree:dualTableScanalias: dualSelect Operatorexpressions:expr: '1'type: stringexpr: '2'type: stringoutputColumnNames: _col0, _col1File Output Operatorcompressed: trueGlobalTableId: 1table:input format: org.apache.hadoop.mapred.TextInputFormatoutput format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormatserde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDename: default.testStage: Stage-4Move Operatorfiles:hdfs directory: truedestination: hdfs://hadoop_namenode/tmp/hive-root/hive_2015-01-07_18-07-13_120_2026314954951095577/-ext-10000Stage: Stage-0Move Operatortables:partition:day 20140101replace: true --overwritetable:input format: org.apache.hadoop.mapred.TextInputFormatoutput format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormatserde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDename: default. test
由执行计划能看出,前面的mapreduce过程不会影响到表分区路径的新建或删除,而真正影响到数据的操作是Move Operator
该类有个方法是move操作时执行的,public int execute(DriverContext driverContext) {...}方法较长,我们只找主要执行到的部分
// static partitions 对于静态分区,主要有以下方法执行操作
db.loadPartition(tbd.getSourcePath(), //来源数据的位置,即mapreduce计算结果的临时目录
tbd.getTable()
.getTableName(), //表的名字
tbd.getPartitionSpec(), //获得指定的表分区
tbd
.getReplace(), //是否采用覆盖的方式,
overwrite
tbd.getHoldDDLTime(), //if true, force [re]create the partition,没有分区则新建分区
tbd
.getInheritTableSpecs(), //修改的分区是否继承之前的属性,默认为true
isSkewedStoredAsDirs(tbd)); //表是否是分桶表
(3)跟踪方法进入到org.apache.hadoop.hive.ql.metadata.Hive.java类,找到对应的方法
loadPartition,找到相应代码段
Partition oldPart = getPartition(tbl, partSpec, false);Path oldPartPath = null;if(oldPart != null) {
oldPartPath = oldPart.getDataLocation(); //表分区定义的location,即我们例子中的 /{warehouse}/test/20140101/
}
Path newPartPath = null;
if (inheritTableSpecs) {//默认值为true
Path partPath = new Path(tbl.getDataLocation(),Warehouse.makePartPath(partSpec));newPartPath = new Path(loadPath.toUri().getScheme(), loadPath.toUri().getAuthority(),partPath.toUri().getPath());//值为由表的location信息和分区值组成的路径, 即我们例子中的/{warehouse}/test/day=20140101/
if(oldPart != null) {
/** If we are moving the partition across filesystem boundaries* inherit from the table properties. Otherwise (same filesystem) use the* original partition location.** See: HIVE-1707 and HIVE-2117 for background*//*fs.hdfs.impl.disable.cache 这个参数 就影响到以下两个操作,决定了oldPartPathFS与loadPathFS 是否指向同一个对象,进而影响到 newPartPath 的值到底取什么*/FileSystem oldPartPathFS = oldPartPath.getFileSystem(getConf());//分区的locationFileSystem loadPathFS = loadPath.getFileSystem(getConf());//来源数据if (oldPartPathFS.equals(loadPathFS)) {
newPartPath = oldPartPath;
}
}
}else {
newPartPath = oldPartPath;
(4)目标路径的取值}newPartPath 这个变量就是决定数据move操作时的目的路径,所以只要确定 newPartPath 的值,我们就知道数据是怎么移动的
我们嵌入一下org.apache.hadoop.fs.Path.java类的内容,找到方法
getFileSystem(
Configuration
),研究一下这个方法是怎么实现的
return createFileSystem(uri, conf); //如果设置了 fs.hdfs.impl.disable.cache=true,则每次FileSystem.get(...)时,都是获得一个 新FileSystem 对象,再执行上面的 oldPartPathFS.equals(loadPathFS)时,肯定为false
if(conf.getBoolean(disableCacheName, false))public FileSystem getFileSystem(Configuration conf)throws IOException{return FileSystem.get(toUri(), conf);}继续跟踪代码 FileSystem.get(toUri(), conf) ,跟到类org.apache.hadoop.fs. FileSystem.java,跟踪方法public static FileSystem get(URI uri, Configuration conf){...},看主要代码段:String disableCacheName = String.format("fs.%s.impl.disable.cache", new Object[] { scheme });
return createFileSystem(uri, conf); //如果设置了 fs.hdfs.impl.disable.cache=true,则每次FileSystem.get(...)时,都是获得一个 新FileSystem 对象,再执行上面的 oldPartPathFS.equals(loadPathFS)时,肯定为false
else
return CACHE.get(uri, conf); //如果设置了 fs.hdfs.impl.disable.cache=false,则从缓存CACHE中找相应的 FileSystem对象, 再执行上面 的 oldPartPathFS.equals(loadPathFS)时,为true
return CACHE.get(uri, conf); //如果设置了 fs.hdfs.impl.disable.cache=false,则从缓存CACHE中找相应的 FileSystem对象, 再执行上面 的 oldPartPathFS.equals(loadPathFS)时,为true
(5) 移动数据,回到类org.apache.hadoop.hive.ql.metadata.Hive.java根据这段的分析,再执行(3)中的代码时,如下if (oldPartPathFS.equals(loadPathFS)) {newPartPath = oldPartPath;}//如果设置了 fs.hdfs.impl.disable.cache=false,则 oldPartPathFS.equals(loadPathFS)返回true,newPartPath 取值为 oldPartPath, 值为上例中 的 /{warehouse}/test/20140101/ ;否则 newPartPath 的值保持不变,为 /{warehouse}/test/day=20140101/
由于我们在操作中设置了 fs.hdfs.impl.disab le.cache=true,所以导致 newPartPath 值为 /{warehouse}/test/day=20140101/
/* 由于我们使用的操作是insert overwrite ,所以 replace为true,最终数据就是移动到newPartPath*/
if (replace) { // 判断是否替换掉原来的数据
Hive.replaceFiles(loadPath, newPartPath, oldPartPath, getConf());
} else {
FileSystem fs = tbl.getDataLocation().getFileSystem(conf);Hive.copyFiles(conf, loadPath, newPartPath, fs);
}跟踪到方法 void replaceFiles(Path srcf, Path destf, Path oldPath, HiveConf conf){...},看下对数据的操作这个方法主要有两个操作1.删除原来的数据 , oldPath ,即我们例子中的/{warehouse}/test/day=20140101/
// use FsShell to move data to .Trash first rather than delete permanentlyif (fs2.exists(oldPath)) {
FsShell fshell = new FsShell();
fshell.setConf(conf);
fshell.run(new String[]{"-rmr", oldPath.toString()});
}
2.rename源数据到目标路径,完成数据移动, srcf->destf,上例中此时的destf为/{warehouse}/test/day=20140101/
boolean b = renameFile(conf, srcs[0].getPath(), destf, fs, true);
根据上面的分析,我们可以看出,由于设置了 fs.hdfs.impl.disab le.cache=true ,,无法在缓存中取到 FileSystem对象, 所以导致 newPartPath 的值无法取到 oldPartPath 的值,最终 为 /{warehouse}/test/day=20140101/ ,所以最终会在hdfs上面新建一个目录,然后删除了 oldPartPath 原来的数据,导致 /{warehouse}/test/20140101/ 目录及下面的文件都被删除掉,所以出现了上面的情况!