有时根据需求,需要对hive中的表批量处理,这时可以到元数据库中进行一些查询操作,操作请慎重!! 【mysql】
1、查询某表的分区
在Spark-sql查询hive表时,会由于元数据中文件与hdfs文件不一致而出现TreeNodeException的异常。
比如说,在hive中show partitions时有分区pt=20160601,但是对应HDFS路径下并没有这个子文件夹时,在Spark-sql中就会出现该异常。
这时如果需要查询某表的分区,就可以使用如下语句:
SELECT p.* from PARTITIONS p
JOIN TBLS t
ON t.TBL_ID=p.TBL_ID
WHERE t.TBL_ID='6'
AND PART_NAME like '%deptno=10%';
#依靠tbl_id 和 分区名确认
2、查询指定库中stored as textfile类型的所有表名
select
d.NAME,
t.TBL_NAME,
s.INPUT_FORMAT,
s.OUTPUT_FORMAT
from TBLS t
join DBS d
join SDS s
where t.DB_ID = d.DB_ID
and t.SD_ID = s.SD_ID
and d.NAME='test'
and s.INPUT_FORMAT like '%TextInputFormat%';
#test是库名
3、查询指定库中的分区表
select
db.NAME,
tb.TBL_NAME,
pk.PKEY_NAME
from TBLS tb
join DBS db
join PARTITION_KEYS pk
where tb.DB_ID = db.DB_ID
and tb.TBL_ID=pk.TBL_ID
and db.NAME='test';
#test是库名
4、查询指定库的非分区表
select
db.NAME,
tb.TBL_NAME
from TBLS tb
join DBS db
where tb.DB_ID = db.DB_ID
and db.NAME='test'
and tb.TBL_ID not in (
select distinct TBL_ID from PARTITION_KEYS
#test是库名
5、查询指定库中某种存储类型的分区表
select
db.NAME,
tb.TBL_NAME,
pk.PKEY_NAME,
s.INPUT_FORMAT,
s.OUTPUT_FORMAT
from TBLS tb
join DBS db
join PARTITION_KEYS pk
join SDS s
where tb.DB_ID = db.DB_ID
and tb.TBL_ID=pk.TBL_ID
and tb.SD_ID = s.SD_ID
and db.NAME='test'
and s.INPUT_FORMAT like '%TextInputFormat%';
#test是库名
6、查询指定库中某种存储类型的非分区表
select
db.NAME,
tb.TBL_NAME,
s.INPUT_FORMAT,
s.OUTPUT_FORMAT
from TBLS tb
join DBS db
join SDS s
where tb.DB_ID = db.DB_ID
and tb.SD_ID = s.SD_ID
and db.NAME='test'
and s.INPUT_FORMAT like '%TextInputFormat%'
and tb.TBL_ID not in (select distinct TBL_ID from PARTITION_KEYS)
#test是库名