• 使用Druid解析SQL实现血缘关系计算


    import com.alibaba.druid.sql.SQLUtils;
    import com.alibaba.druid.sql.ast.SQLStatement;
    import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
    import com.alibaba.druid.stat.TableStat;
    import com.alibaba.druid.util.JdbcConstants;
    import lombok.extern.slf4j.Slf4j;
    
    import java.util.Collection;
    import java.util.List;
    import java.util.Map;
    import java.util.Objects;
    
    @Slf4j
    public class Test {
    
        public static void main(String args[]) throws Exception {
    
            String sql = "insert overwrite table kf_zt.account2 SELECT swjg_dm,swjgmc FROM (SELECT swjg_dm AS swjg_dm, swjgmc AS swjgmc FROM (SELECT swjg_dm,swjgmc FROM ( SELECT swjg_dm,swjgmc FROM yuangongs) table_350 UNION SELECT swjg_dm,swjgmc FROM (SELECT swjg_dm2,swjgmc2 FROM yuangong ) table_295) union_013) udf_882";
    
            List<SQLStatement> sqlStatements = SQLUtils.parseStatements(sql.toLowerCase(), JdbcConstants.HIVE);
    
            for (SQLStatement sqlStatement : sqlStatements) {
                SchemaStatVisitor schemaStatVisitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.HIVE);
                sqlStatement.accept(schemaStatVisitor);
                Map<TableStat.Name, TableStat> tables = schemaStatVisitor.getTables();
                Collection<TableStat.Column> columns = schemaStatVisitor.getColumns();
                if (Objects.nonNull(tables)) {
                    tables.forEach(((name, tableStat) -> {
                        if (tableStat.getCreateCount() > 0 || tableStat.getInsertCount() > 0) {
                            log.info("to: table[{}]", name.getName().toLowerCase());
                            columns.stream().filter(column -> Objects.equals(column.getTable().toLowerCase(), name.getName().toLowerCase())).forEach(column -> {
                                log.info("to: table[{}] column[{}]", column.getTable().toLowerCase(), column.getName().toLowerCase());
                            });
                        } else  if (tableStat.getSelectCount() > 0) {
                            log.info("from: table[{}]", name.getName().toLowerCase());
                            columns.stream().filter(column -> Objects.equals(column.getTable().toLowerCase(), name.getName().toLowerCase())).forEach(column -> {
                                log.info("from: table[{}] column[{}]", column.getTable().toLowerCase(), column.getName().toLowerCase());
                            });
                        }
                    }));
                }
            }
        }
    
    }
  • 相关阅读:
    iOS开发 | 自定义不规则label
    监控redis的操作命令
    HTML常用标签
    前端学习【第一篇】: HTML内容
    MySQL数据库修改字段的长度
    python模块之:paramiko
    使用pymysql操作mysql数据库
    Python开发【第九篇】: 并发编程
    JNI调用实例
    JVM性能调优入门
  • 原文地址:https://www.cnblogs.com/changxy-codest/p/13535143.html
Copyright © 2020-2023  润新知