1 importcom.google.common.base.Joiner;2 importcom.google.common.collect.Lists;3 importcom.google.common.collect.Maps;4 importlombok.extern.slf4j.Slf4j;5 importorg.apache.commons.collections.CollectionUtils;6 importorg.apache.hadoop.hive.ql.lib.Node;7 importorg.apache.hadoop.hive.ql.parse.ASTNode;8 importorg.apache.hadoop.hive.ql.parse.ParseDriver;9
10 import java.util.*;11 importjava.util.stream.Collectors;12
13 import static org.apache.hadoop.hive.ql.parse.HiveParser.*;14
15 /**
16 *@authorchentiefeng17 * @date 2019/10/21 13:5118 */
19 @Slf4j20 public classHiveSqlParse {21 private ParseDriver pd = newParseDriver();22 /**
23 * 原始表(表名,别名)24 */
25 private List sourceTable =Lists.newArrayList();26 /**
27 * 插入表28 */
29 private List insertTables =Lists.newArrayList();30 /**
31 * 最外层列32 */
33 private List outermostColumns =Lists.newArrayList();34 /**
35 * 插入分区信息(分区列,分区值)36 */
37 private Map partitionMap =Maps.newHashMap();38 /**
39 * 最外层Sel节点40 */
41 private ASTNode outermostSelNode = null;42 /**
43 * 最外层Insert节点44 */
45 private ASTNode outermostInsertNode = null;46 /**
47 * 放置 解析表栈48 */
49 private Stack tableParseInfoSelStack = new Stack<>();50 private Stack tableParseInfoFromStack = new Stack<>();51 /**
52 * 表关系解析信息,不包含原始表53 */
54 private HiveTableParseInfo tableParseInfo = null;55
56 publicHiveSqlParse() {57 }58
59 publicHiveSqlParse(String sql) {60 parse(sql);61 }62
63 /**
64 * sql解析65 *66 *@paramsql67 */
68 public voidparse(String sql) {69 try{70 ASTNode ast =pd.parse(sql);71 log.info("hiveSql={},astTree={}", sql, ast.toStringTree());72 parseNode(ast);73 insert(outermostInse