最近遇到一个需求
需求就是,给你一段sql,然后解析这段sql里面用了哪些表。
然后将这些表,跟数据库里面的表做匹配,数据库里面的表,会记录哪些表你有权限查询,哪些表没有权限查询
需求重点:
找出这段sql中使用了哪些表
使用 org.apache.hadoop.hive.ql.tools.LineageInfo 即可找出sql使用到哪些表
mvn 依赖配置
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>3.1.2</version>
</dependency>
</dependencies>
SQL示例
with tempa as (
select
*
from
tablea
where
col1 = 'abc')
insert
into
tabled
select
*
from
tableb b
inner join tempa a on
b.col3 = a.col3
where
b.col4 in (
select
col4
from
tablec)
Java代码示例
InputTable 为查了哪些表
OutputTable 为修改了哪些表
import org.apache.hadoop.hive.ql.parse.ParseException;
import org.apache.hadoop.hive.ql.parse.SemanticException;
import org.apache.hadoop.hive.ql.tools.LineageInfo;
public class Demo1 {
public static void main(String[] args) throws ParseException, SemanticException {
String query = "with tempa as (select * from tablea where col1 = 'abc') insert into tabled select * from tableb b inner join tempa a on b.col3 = a.col3 where b.col4 in (select col4 from tablec)";
LineageInfo pares = new LineageInfo();
pares.getLineageInfo(query);
for (String tab : pares.getInputTableList()) {
System.out.println("InputTable= "+tab);
}
for (String tab : pares.getOutputTableList()) {
System.out.println("OutputTable= "+tab);
}
}
}
结果显示