版权
修改HIVE-SITE.XML,每个sql在执行之前都会去执行这个类,这个类实现
ExecuteWithHookContext接口
<property>
<name>hive.exec.pre.hooks</name>
<value>com.wending.hive.HivePreHook</value>
<description>
Comma-separated list of pre-execution hooks to be invoked for each statement.
A pre-execution hook is specified as the name of a Java class which implements the
org.apache.hadoop.hive.ql.hooks.ExecuteWithHookContext interface.
</description>
</property>
2.示例代码,然后打包放在hive lib/路径下,不管从哪个路径访问的,命令行,JDBC等等都能把sql采集到。
public class HivePreHook implements ExecuteWithHookContext {
public void run(HookContext hookContext) throws Exception {
final HookContext hc = hookContext;
try {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(("执行了hive hook"));
final QueryPlan queryPlan = hc.getQueryPlan();
String querySql = queryPlan.getQueryStr();
System.out.println(querySql);
}
}).start();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}