码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2020-08-26 17:20:53      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:log   slf4j   exception   druid   exce   over   div   计算   creat   

package com.cestc.dassets.service;

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());
                        });
                    }
                }));
            }
        }
    }

}

 

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

标签:log   slf4j   exception   druid   exce   over   div   计算   creat   

原文地址:https://www.cnblogs.com/changxy-codest/p/13535143.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!