Apache Calcite進行SQL解析(java代碼實例)
背景
當一個項目分了很多模塊,很多個服務(wù)的時候,一些公共的配置就需要統(tǒng)一管理了,于是就有了元數(shù)據(jù)驅(qū)動!
簡介
什么是Calcite?
是一款開源SQL解析工具, 可以將各種SQL語句解析成抽象語法樹AST(Abstract Syntax Tree), 之后通過操作AST就可以把SQL中所要表達的算法與關(guān)系體現(xiàn)在具體代碼之中。
Calcite能做啥?
- SQL 解析
- SQL 校驗
- 查詢優(yōu)化
- SQL 生成器
- 數(shù)據(jù)連接
實例
今天主要是貼出一個java代碼實例,實現(xiàn)了:解析SQL語句中的表名
上代碼:
SQL語句轉(zhuǎn)化:
public static SqlNode parseStatement(String sql) {
SqlParser parser = SqlParser.create(sql, config.getParserConfig());
try {
return parser.parseQuery();
} catch (Exception e) {
e.printStackTrace();
throw new UnsupportedOperationException("operation not allowed");
}
}解析Select中的表名:
private static Set<String> extractSourceTableInSelectSql(SqlNode sqlNode, boolean fromOrJoin) {
if (sqlNode == null) {
return new HashSet<>();
}
final SqlKind sqlKind = sqlNode.getKind();
if (SqlKind.SELECT.equals(sqlKind)) {
SqlSelect selectNode = (SqlSelect) sqlNode;
Set<String> selectList = new HashSet<>(extractSourceTableInSelectSql(selectNode.getFrom(), true));
selectNode.getSelectList().getList().stream().filter(node -> node instanceof SqlCall)
.forEach(node -> selectList.addAll(extractSourceTableInSelectSql(node, false)));
selectList.addAll(extractSourceTableInSelectSql(selectNode.getWhere(), false));
selectList.addAll(extractSourceTableInSelectSql(selectNode.getHaving(), false));
return selectList;
if (SqlKind.JOIN.equals(sqlKind)) {
SqlJoin sqlJoin = (SqlJoin) sqlNode;
Set<String> joinList = new HashSet<>();
joinList.addAll(extractSourceTableInSelectSql(sqlJoin.getLeft(), true));
joinList.addAll(extractSourceTableInSelectSql(sqlJoin.getRight(), true));
return joinList;
if (SqlKind.AS.equals(sqlKind)) {
SqlCall sqlCall = (SqlCall) sqlNode;
return extractSourceTableInSelectSql(sqlCall.getOperandList().get(0), fromOrJoin);
if (SqlKind.IDENTIFIER.equals(sqlKind)) {
Set<String> identifierList = new HashSet<>();
if (fromOrJoin) {
SqlIdentifier sqlIdentifier = (SqlIdentifier) sqlNode;
identifierList.add(sqlIdentifier.toString());
}
return identifierList;
Set<String> defaultList = new HashSet<>();
if (sqlNode instanceof SqlCall) {
SqlCall call = (SqlCall) sqlNode;
call.getOperandList()
.forEach(node -> defaultList.addAll(extractSourceTableInSelectSql(node, false)));
return defaultList;
}解析Insert語句中的表名:
private static Set<String> extractSourceTableInInsertSql(SqlNode sqlNode, boolean fromOrJoin) {
SqlInsert sqlInsert = (SqlInsert) sqlNode;
Set<String> insertList = new HashSet<>(extractSourceTableInSelectSql(sqlInsert.getSource(), false));
final SqlNode targetTable = sqlInsert.getTargetTable();
if (targetTable instanceof SqlIdentifier) {
insertList.add(((SqlIdentifier) targetTable).toString());
}
return insertList;
}執(zhí)行效果
private static final String sql0 = "SELECT MIN(relation_id) FROM tableA JOIN TableB GROUP BY account_instance_id, follow_account_instance_id HAVING COUNT(*)>1";
private static final String sql1 = "SELECT * FROM blog_user_relation a WHERE (a.account_instance_id,a.follow_account_instance_id) IN (SELECT account_instance_id,follow_account_instance_id FROM Blogs_info GROUP BY account_instance_id, follow_account_instance_id HAVING COUNT(*) > 1)";
private static final String sql2 = "select name from (select * from student)";
private static final String sql3 = "SELECT * FROM Student LEFT JOIN Grade ON Student.sID = Grade.gID\n" +
"UNION\n" +
"SELECT * FROM Student RIGHT JOIN Grade ON Student.sID = Grade.gID";
private static final String sql4 = "SELECT *\n" +
"FROM teacher\n" +
"WHERE birth = (SELECT MIN(birth)\n" +
" FROM employee)";
private static final String sql5 = "SELECT sName\n" +
"FROM Student\n" +
"WHERE '450' NOT IN (SELECT courseID\n" +
" FROM Course\n" +
" WHERE sID = Student.sID)";
final SqlNode sqlNode0 = parseStatement(sql0);
System.out.println("sqlNode0: " + extractSourceTableInSelectSql(sqlNode0, false));結(jié)果為:

到此這篇關(guān)于Apache Calcite進行SQL解析的文章就介紹到這了,更多相關(guān)Apache Calcite解析內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Elasticsearch如何實現(xiàn)簡單的腳本排序
Elasticsearch?是位于?Elastic?Stack?核心的分布式搜索和分析引擎,可以為所有類型的數(shù)據(jù)提供近乎實時的搜索和分析。本文主要介紹了Elasticsearch如何實現(xiàn)簡單的腳本排序,感興趣的可以了解一下2023-01-01
javaweb中Filter(過濾器)的常見應(yīng)用
這篇文章主要介紹了javaweb中Filter的常見應(yīng)用,過濾器的使用方法,感興趣的小伙伴們可以參考一下2015-12-12
Java讀取resources目錄下文件路徑的九種代碼示例教程
在Java開發(fā)中經(jīng)常需要讀取項目中resources目錄下的文件或獲取資源路徑,這篇文章主要給大家介紹了關(guān)于Java讀取resources目錄下文件路徑的九種代碼示例教程,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-07-07
Spring Boot實現(xiàn)分布式鎖的自動釋放的示例代碼
在實際開發(fā)中,我們可以使用 Redis、Zookeeper 等分布式系統(tǒng)來實現(xiàn)分布式鎖,本文將介紹如何使用 Spring Boot 來實現(xiàn)分布式鎖的自動釋放,感興趣的朋友跟隨小編一起看看吧2023-06-06
解決MyEclipse下啟動項目時JBoss內(nèi)存溢出的問題
下面小編就為大家?guī)硪黄鉀QMyEclipse下啟動項目時JBoss內(nèi)存溢出的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07

