pom依赖参考以下博文
java 通过 IMetaStoreClient 取 hive 元数据信息-CSDN博客
1 节点处理器类
import lombok.Getter;
import org.apache.hadoop.hive.ql.lib.Dispatcher;
import org.apache.hadoop.hive.ql.lib.Node;
import org.apache.hadoop.hive.ql.parse.ASTNode;
import org.apache.hadoop.hive.ql.parse.HiveParser;
import org.apache.hadoop.hive.ql.parse.SemanticException;
import java.util.Stack;/*** 节点处理器*/
public class SelectAllDispatcher implements Dispatcher {@Getterprivate boolean isContainsSelectstar = false;@Overridepublic Object dispatch(Node node, Stack<Node> stack, Object... objects) throws SemanticException {//强转成 ASTNodeASTNode astNode = (ASTNode) node;if (astNode.getType() == HiveParser.TOK_ALLCOLREF) {isContainsSelectstar = true;}return null;}}
2 sql语法树解析类
import org.apache.hadoop.hive.ql.lib.DefaultGraphWalker;
import org.apache.hadoop.hive.ql.lib.Dispatcher;
import org.apache.hadoop.hive.ql.lib.GraphWalker;
import org.apache.hadoop.hive.ql.parse.ASTNode;
import org.apache.hadoop.hive.ql.parse.HiveParser;
import org.apache.hadoop.hive.ql.parse.ParseDriver;
import org.springframework.util.StringUtils;
import java.util.Collections;public class SqlParser {/*** 遍历到每个节点时,对节点的处理。* 我们可以通过 自定义 Dispatcher 完成对节点的处理。** @param dispatcher 自定义节点处理器* @param sql 待分析的sql*/public static void sqlParse(Dispatcher dispatcher, String sql) {if (!StringUtils.hasText(sql)) {return;}try {// 解析 Hive SQLParseDriver parseDriver = new ParseDriver(); //创建解析引擎//执行解析 得到根节点ASTNode ast = parseDriver.parse(sql);// 找到查询的根节点下的TOK_QUERY节点,视为有效根节点while (ast.getToken() == null || ast.getToken().getType() != HiveParser.TOK_QUERY) {ast = (ASTNode) ast.getChild(0);}// 把节点处理器注入一个图形遍历器 使用自定义处理器遍历抽象语法树GraphWalker ogw = new DefaultGraphWalker(dispatcher);// 用遍历器遍历整个语法树ogw.startWalking(Collections.singletonList(ast), null);} catch (Exception e) {throw new RuntimeException(e);}}
}
3 测试
public class Test_12345 {public static void main(String[] args) {String sql1 = "SELECT * FROM table_name";String sql2 = "SELECT column1, column2 FROM table_name";String sql3 = "SELECT * FROM (SELECT * FROM another_table) t";String sql4 = "SELECT aa,bb FROM (SELECT * FROM another_table) t";String sql5 = "SELECT * FROM (SELECT bb FROM another_table) t";String sql6 = "SELECT a.* FROM table_a a JOIN table_b b ON a.id = b.id";String sql7 = "SELECT distinct * FROM table_a ";SelectAllDispatcher ggsdgsg = new SelectAllDispatcher();SqlParser.sqlParse(ggsdgsg,sql1);System.out.println("是否包括select * :" + ggsdgsg.isContainsSelectstar());System.out.println();ggsdgsg = new SelectAllDispatcher();SqlParser.sqlParse(ggsdgsg,sql2);System.out.println("是否包括select * :" + ggsdgsg.isContainsSelectstar());System.out.println();ggsdgsg = new SelectAllDispatcher();SqlParser.sqlParse(ggsdgsg,sql3);System.out.println("是否包括select * :" + ggsdgsg.isContainsSelectstar());System.out.println();ggsdgsg = new SelectAllDispatcher();SqlParser.sqlParse(ggsdgsg,sql4);System.out.println("是否包括select * :" + ggsdgsg.isContainsSelectstar());System.out.println();ggsdgsg = new SelectAllDispatcher();SqlParser.sqlParse(ggsdgsg,sql5);System.out.println("是否包括select * :" + ggsdgsg.isContainsSelectstar());System.out.println();ggsdgsg = new SelectAllDispatcher();SqlParser.sqlParse(ggsdgsg,sql6);System.out.println("是否包括select * :" + ggsdgsg.isContainsSelectstar());System.out.println();ggsdgsg = new SelectAllDispatcher();SqlParser.sqlParse(ggsdgsg,sql7);System.out.println("是否包括select * :" + ggsdgsg.isContainsSelectstar());}
}