hive 建表语句 解析为 List<SQLStatement>
时间: 2024-05-12 15:14:24 浏览: 227
可以使用阿里巴巴的开源项目Druid来解析Hive建表语句,具体代码如下:
```java
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.dialect.hive.parser.HiveStatementParser;
import java.util.List;
public class HiveDDLParser {
public static List<SQLStatement> parse(String ddl) {
HiveStatementParser parser = new HiveStatementParser(ddl);
List<SQLStatement> statementList = parser.parseStatementList();
return statementList;
}
public static void main(String[] args) {
String ddl = "CREATE TABLE test_table (\n" +
" col1 string,\n" +
" col2 int,\n" +
" col3 double\n" +
") ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE;";
List<SQLStatement> statementList = HiveDDLParser.parse(ddl);
for (SQLStatement statement : statementList) {
if (statement instanceof SQLCreateTableStatement) {
SQLCreateTableStatement createTableStatement = (SQLCreateTableStatement) statement;
System.out.println(createTableStatement.getTableSource().getExpr().toString());
}
}
}
}
```
其中,`HiveStatementParser`是Druid提供的Hive语法解析器,`parseStatementList()`方法可以将Hive建表语句解析为多个`SQLStatement`对象,然后可以通过`instanceof`判断其具体类型,再进行相关操作。以上代码演示了如何解析Hive建表语句,并获取表名。
阅读全文