快速使用hive-exec实现sql解析

coverImg

本章节源码

当前文档配套相关源码地址:

  • gitee:https://gitee.com/changluJava/demo-exer/tree/master/java-sqlparser/demo-hive
  • github:https://github.com/changluya/Java-Demos/tree/master/java-sqlparser/demo-hive

引入依赖

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>
<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-exec</artifactId>
    <version>2.1.0</version>
    <exclusions>
        <exclusion>
            <artifactId>log4j</artifactId>
            <groupId>log4j</groupId>
        </exclusion>
        <exclusion>
            <artifactId>log4j-1.2-api</artifactId>
            <groupId>org.apache.logging.log4j</groupId>
        </exclusion>
        <exclusion>
            <artifactId>log4j-slf4j-impl</artifactId>
            <groupId>org.apache.logging.log4j</groupId>
        </exclusion>
        <exclusion>
            <artifactId>log4j-api</artifactId>
            <groupId>org.apache.logging.log4j</groupId>
        </exclusion>
        <exclusion>
            <artifactId>log4j-core</artifactId>
            <groupId>org.apache.logging.log4j</groupId>
        </exclusion>
        <exclusion>
            <artifactId>slf4j-api</artifactId>
            <groupId>org.slf4j</groupId>
        </exclusion>
        <exclusion>
            <artifactId>calcite-core</artifactId>
            <groupId>org.apache.calcite</groupId>
        </exclusion>
        <exclusion>
            <artifactId>slf4j-log4j12</artifactId>
            <groupId>org.slf4j</groupId>
        </exclusion>
        <exclusion>
            <artifactId>hive-shims</artifactId>
            <groupId>org.apache.hive</groupId>
        </exclusion>
        <exclusion>
            <artifactId>hive-llap-tez</artifactId>
            <groupId>org.apache.hive</groupId>
        </exclusion>
        <exclusion>
            <artifactId>zookeeper</artifactId>
            <groupId>org.apache.zookeeper</groupId>
        </exclusion>
        <exclusion>
            <artifactId>curator-framework</artifactId>
            <groupId>org.apache.curator</groupId>
        </exclusion>
        <exclusion>
            <artifactId>calcite-avatica</artifactId>
            <groupId>org.apache.calcite</groupId>
        </exclusion>
        <exclusion>
            <artifactId>commons-httpclient</artifactId>
            <groupId>commons-httpclient</groupId>
        </exclusion>
        <exclusion>
            <artifactId>hive-ant</artifactId>
            <groupId>org.apache.hive</groupId>
        </exclusion>
        <exclusion>
            <artifactId>gson</artifactId>
            <groupId>com.google.code.gson</groupId>
        </exclusion>
        <exclusion>
            <artifactId>ivy</artifactId>
            <groupId>org.apache.ivy</groupId>
        </exclusion>
        <exclusion>
            <artifactId>ST4</artifactId>
            <groupId>org.antlr</groupId>
        </exclusion>
        <exclusion>
            <artifactId>stax-api</artifactId>
            <groupId>stax</groupId>
        </exclusion>
        <exclusion>
            <artifactId>groovy-all</artifactId>
            <groupId>org.codehaus.groovy</groupId>
        </exclusion>
        <exclusion>
            <artifactId>apache-curator</artifactId>
            <groupId>org.apache.curator</groupId>
        </exclusion>
    </exclusions>
</dependency>

SQL解析

简单解析sql案例

/**
 * 使用hive来完成sql解析
 */
public class Demo1 {
    public static void main(String[] args) {
        ParseDriver parseDriver = new ParseDriver();
        String sql = "select * from test";
        ASTNode root = null;
        try {
            // 解析sql为node语法树
            root = parseDriver.parse(sql);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        System.out.println(root);
    }
}

评论区请在客户端页面查看