在hiveserver2中使用了org.apache.hadoop.hive.ql.parse.BaseSemanticAnalyzer对抽象语法树(AST)进行语义分析,分析的过程可以得出hive查询的表及其字段

于是我利用相关的类去做测试:

package com.baidu.waimai;

import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.ql.Context;
import org.apache.hadoop.hive.ql.parse.*;
import org.apache.hadoop.hive.ql.session.SessionState; public class TestHiveParser {
public static void main(String[] args) throws Throwable {
// String sql = "select * from (select name from test.test1 where id = 1) a";
String sql = "select * from test.test1"; HiveConf hiveConf = new HiveConf();
hiveConf.set("hive.stats.collect.tablekeys", "true");
hiveConf.set("hive.stats.collect.scancols", "true"); SessionState sessionState = SessionState.start(hiveConf);
sessionState.initTxnMgr(hiveConf);
SessionState.setCurrentSessionState(sessionState); Context ctx = new Context(hiveConf);
ctx.setTryCount(Integer.MAX_VALUE);
ctx.setCmd(sql);
ctx.setHDFSCleanup(true); ParseDriver pd = new ParseDriver();
ASTNode tree = pd.parse(sql);
tree = ParseUtils.findRootNonNullToken(tree);
System.out.println(tree);
BaseSemanticAnalyzer baseSemanticAnalyzer = SemanticAnalyzerFactory.get(hiveConf, tree);
System.out.println(baseSemanticAnalyzer);
baseSemanticAnalyzer.analyze(tree, ctx);
TableAccessInfo tableAccessInfo = baseSemanticAnalyzer.getTableAccessInfo();
System.out.println(tableAccessInfo); ColumnAccessInfo columnAccessInfo = baseSemanticAnalyzer.getColumnAccessInfo();
System.out.println(columnAccessInfo); sessionState.close();
ctx.clear();
}
}

最后可以根据columnAccessInfo去得到查询的表及其字段。

但这么做不太满足我们的需求,比如说下面这句sql,我想要的结果是查询了test.test1表的name字段,但是它连id字段也拿出来了。

select * from (select name from test.test1 where id = 1) a

先记录一下,之后如果找到源码有解决方法的话再更新

利用hive源码解析sql查了哪些表哪些字段的更多相关文章

  1. Hive源码解析

    date: 2020-07-08 15:12:00 updated: 2020-08-21 17:38:00 Hive源码解析 入口:hive-cli-1.1.0-cdh5.14.4.jar!/org ...

  2. Spark SQL源码解析(五)SparkPlan准备和执行阶段

    Spark SQL原理解析前言: Spark SQL源码剖析(一)SQL解析框架Catalyst流程概述 Spark SQL源码解析(二)Antlr4解析Sql并生成树 Spark SQL源码解析(三 ...

  3. Spark SQL源码解析(三)Analysis阶段分析

    Spark SQL原理解析前言: Spark SQL源码剖析(一)SQL解析框架Catalyst流程概述 Spark SQL源码解析(二)Antlr4解析Sql并生成树 Analysis阶段概述 首先 ...

  4. Spark SQL源码解析(四)Optimization和Physical Planning阶段解析

    Spark SQL原理解析前言: Spark SQL源码剖析(一)SQL解析框架Catalyst流程概述 Spark SQL源码解析(二)Antlr4解析Sql并生成树 Spark SQL源码解析(三 ...

  5. [源码解析] GroupReduce,GroupCombine 和 Flink SQL group by

    [源码解析] GroupReduce,GroupCombine和Flink SQL group by 目录 [源码解析] GroupReduce,GroupCombine和Flink SQL grou ...

  6. jQuery2.x源码解析(设计篇)

    jQuery2.x源码解析(构建篇) jQuery2.x源码解析(设计篇) jQuery2.x源码解析(回调篇) jQuery2.x源码解析(缓存篇) 这一篇笔者主要以设计的角度探索jQuery的源代 ...

  7. EventBus源码解析 源码阅读记录

    EventBus源码阅读记录 repo地址: greenrobot/EventBus EventBus的构造 双重加锁的单例. static volatile EventBus defaultInst ...

  8. 【转】Java HashMap 源码解析(好文章)

    ­ .fluid-width-video-wrapper { width: 100%; position: relative; padding: 0; } .fluid-width-video-wra ...

  9. 给jdk写注释系列之jdk1.6容器(4)-HashMap源码解析

    前面了解了jdk容器中的两种List,回忆一下怎么从list中取值(也就是做查询),是通过index索引位置对不对,由于存入list的元素时安装插入顺序存储的,所以index索引也就是插入的次序. M ...

随机推荐

  1. UVA1583-Digit Generator(紫书例题3.5)

    For a positive integer N , the digit-sum of N is defined as the sum of N itself and its digits. When ...

  2. hive初体验

    --创建表 create table t_order(id int,name string,phone string) row format delimited fields terminated b ...

  3. where和having

    where可以不能使用别名作为过滤条件,而having可以使用别名作为过滤条件. 在ORACLE中,select 语句的执行顺序是: 1. from语句 2. where语句(结合条件) 3. sta ...

  4. PHP读取XML数据中CDATA内数值

    // 在开发过程中遇到对XML获取时候加载 CDATA 无法读取内部的数值(例如微信平台的返回值) $content = simplexml_load_string('<content>& ...

  5. mysql-5.7.10-winx64 安装

    安装ZIP中的EXE文件后,找到安装目录中的my-default.ini加入代码 1 2 3 4 5 6 #新设置的 [mysql] default-character-set=utf8 #新设置的 ...

  6. WinServer-IIS-FTP服务器

    在设置FTP服务器的时候,注意SSL的设置,简单尝试的话,不需要启用SSL 常用FTP命令 ftp   进入FTP命令模式 open 192.168.10.6     (连接到FTP主机,或者open ...

  7. HDU 4300 Contest 1

    扩展KMP很容易就明白过来了. 注意的是,后面明文的长度要少于密文,而且当前K+Extend[k]>=L 输出时犯了很多次二,后来人注意吧. #include <cstdio> #i ...

  8. 在fragment中显示对话框出现异常

    异常类型为:android.view.WindowManager$BadTokenException: Unable to add window 我在fragment中public View onCr ...

  9. 110_leetcode_Best Time to Buy and sell Stock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  10. CoreData的介绍和使用

    一.CoreData是什么? CoreData是iOS SDK里的一个很强大的框架,允许程序员以面向对象的方式存储和管理数据.使用CoreData框架,程序员可以轻松有效地通过面向对象的接口管理数据 ...