使用druid对sql进行血缘解析

  实体类:

@Data
public class SqlFlowEntity {

    /*
    表名称
     */
    private String tableName;

      /*
      表操作类型
     */
    private String type;

     /*
     涉及字段
     */
    private List<String> columnList;
}

 核心代码:

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 com.alibaba.fastjson.JSON;

import java.util.*;


public class test {

    public static void main(String args[])  {


        String sql = "select name from student union select name from employees;";


        List<SQLStatement> sqlStatements = SQLUtils.parseStatements(sql.toLowerCase(), JdbcConstants.HIVE);


        List<SqlFlowEntity> sqlFlowEntityList = new ArrayList<>();

        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) -> {
                    SqlFlowEntity sqlFlowEntity = new SqlFlowEntity();
                    sqlFlowEntity.setTableName(name.toString());
                    sqlFlowEntity.setType(tableStat.toString());

                    List<String> columnList = new ArrayList<>();

                    if (tableStat.getCreateCount() > 0 || tableStat.getInsertCount() > 0) {
                        columns.stream().filter(column -> Objects.equals(column.getTable().toLowerCase(), name.getName().toLowerCase())).forEach(column -> {

                            columnList.add(column.getName().toLowerCase());
                        });
                    } else if (tableStat.getSelectCount() > 0) {
                        columns.stream().filter(column -> Objects.equals(column.getTable().toLowerCase(), name.getName().toLowerCase())).forEach(column -> {

                            columnList.add(column.getName().toLowerCase());
                        });
                    }
                    sqlFlowEntity.setColumnList(columnList);
                    sqlFlowEntityList.add(sqlFlowEntity);
                }));
            }

            System.out.println(JSON.toJSON(sqlFlowEntityList));
        }
    }

}

 返回的结果集:

[{
	"columnList": ["name"],
	"type": "select",
	"tableName": "student"
}, {
	"columnList": ["name"],
	"type": "select",
	"tableName": "employees"
}]

相关推荐

  1. 使用druidsql进行血缘

    2024-07-14 12:24:02       25 阅读
  2. hivehook 表血缘与字段血缘

    2024-07-14 12:24:02       26 阅读
  3. SQL超详细

    2024-07-14 12:24:02       26 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 12:24:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 12:24:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 12:24:02       58 阅读
  4. Python语言-面向对象

    2024-07-14 12:24:02       69 阅读

热门阅读

  1. Spring Boot项目的控制器貌似只能get不能post问题

    2024-07-14 12:24:02       33 阅读
  2. Django是干什么的?好用么?

    2024-07-14 12:24:02       25 阅读
  3. HTTPS 加密流程全解析

    2024-07-14 12:24:02       27 阅读
  4. mysql快速精通(四)多表查询

    2024-07-14 12:24:02       24 阅读
  5. 如何隐藏 Ubuntu 顶部状态栏

    2024-07-14 12:24:02       31 阅读
  6. springboot2——功能和原理

    2024-07-14 12:24:02       22 阅读
  7. Oracle 数据清理

    2024-07-14 12:24:02       22 阅读
  8. win32:第一个窗口程序-初始化实例(part.5)

    2024-07-14 12:24:02       26 阅读
  9. Flask与Celery实现Python调度服务

    2024-07-14 12:24:02       22 阅读
  10. `speech_recognition` 是一个流行的库

    2024-07-14 12:24:02       21 阅读
  11. 致十年后的自己

    2024-07-14 12:24:02       14 阅读
  12. 25秋招面试算法题 (Go版本)

    2024-07-14 12:24:02       25 阅读