编写Java代码,使用druid实现sql解析血缘关系,包括表、字段血缘关系分析
时间: 2024-02-27 19:55:34 浏览: 179
durineage:解析SQL语句,分析血缘。不是table-table的关系,是column-column关系
以下是一个使用Druid实现SQL解析血缘关系的Java代码示例:
```
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.sql.parser.SQLParserUtils;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.stat.TableStat.Column;
import com.alibaba.druid.stat.TableStat.Name;
import java.util.*;
public class SqlLineageParser {
private Map<String, List<String>> tableMap = new HashMap<>();
private Map<String, List<String>> columnMap = new HashMap<>();
public void parseSqlLineage(String sql) {
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, "mysql");
for (SQLStatement stmt : stmtList) {
if (stmt instanceof SQLSelectStatement) {
SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) selectStmt.getSelect().getQuery();
// 解析表血缘关系
List<SQLTableSource> fromList = queryBlock.getFrom();
for (SQLTableSource tableSource : fromList) {
if (tableSource instanceof SQLExprTableSource) {
SQLExprTableSource exprTableSource = (SQLExprTableSource) tableSource;
String tableName = getTableName(exprTableSource.getExpr());
if (tableName != null) {
addTableDependency(tableName);
}
} else if (tableSource instanceof SQLJoinTableSource) {
SQLJoinTableSource joinTableSource = (SQLJoinTableSource) tableSource;
SQLTableSource left = joinTableSource.getLeft();
SQLTableSource right = joinTableSource.getRight();
if (left instanceof SQLExprTableSource) {
SQLExprTableSource exprTableSource = (SQLExprTableSource) left;
String tableName = getTableName(exprTableSource.getExpr());
if (tableName != null) {
addTableDependency(tableName);
}
}
if (right instanceof SQLExprTableSource) {
SQLExprTableSource exprTableSource = (SQLExprTableSource) right;
String tableName = getTableName(exprTableSource.getExpr());
if (tableName != null) {
addTableDependency(tableName);
}
}
}
}
// 解析字段血缘关系
List<SQLSelectItem> selectList = queryBlock.getSelectList();
for (SQLSelectItem selectItem : selectList) {
SQLExpr expr = selectItem.getExpr();
if (expr instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) expr;
String columnName = identifierExpr.getName();
String tableName = getColumnTableName(identifierExpr);
if (tableName != null) {
addColumnDependency(tableName, columnName);
}
}
}
}
}
}
private String getTableName(SQLExpr expr) {
if (expr instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) expr;
return identifierExpr.getName();
} else if (expr instanceof SQLPropertyExpr) {
SQLPropertyExpr propertyExpr = (SQLPropertyExpr) expr;
SQLExpr owner = propertyExpr.getOwner();
String tableName = getTableName(owner);
if (tableName != null) {
return tableName + "." + propertyExpr.getName();
}
}
return null;
}
private String getColumnTableName(SQLIdentifierExpr identifierExpr) {
String columnName = identifierExpr.getName();
Name name = identifierExpr.name();
Map<Name, Column> columns = TableStat.getColumns();
for (Map.Entry<Name, Column> entry : columns.entrySet()) {
Column column = entry.getValue();
if (column.getName().equals(columnName)) {
Name tableName = entry.getKey().getParent();
return tableName.getName();
}
}
return null;
}
private void addTableDependency(String tableName) {
if (!tableMap.containsKey(tableName)) {
tableMap.put(tableName, new ArrayList<>());
}
}
private void addColumnDependency(String tableName, String columnName) {
if (!columnMap.containsKey(columnName)) {
columnMap.put(columnName, new ArrayList<>());
}
List<String> tableList = tableMap.get(tableName);
if (tableList != null) {
for (String table : tableList) {
String column = table + "." + columnName;
if (!columnMap.containsKey(column)) {
columnMap.put(column, new ArrayList<>());
}
columnMap.get(column).add(tableName);
}
}
}
public Map<String, List<String>> getTableMap() {
return tableMap;
}
public Map<String, List<String>> getColumnMap() {
return columnMap;
}
public static void main(String[] args) {
String sql = "select t1.id, t2.name from table1 t1 join table2 t2 on t1.id = t2.id where t1.age > 18";
SqlLineageParser parser = new SqlLineageParser();
parser.parseSqlLineage(sql);
System.out.println("Table Map: " + parser.getTableMap());
System.out.println("Column Map: " + parser.getColumnMap());
}
}
```
在以上代码中,我们通过Druid提供的SQL解析工具解析SQL语句,并通过解析结果获取表和字段的元数据信息。通过分析SQL语句中的表和字段引用关系,我们可以构建出表和字段的血缘关系。最后,我们可以通过getTableMap()和getColumnMap()方法获取解析结果。
阅读全文