1

是否有与 Pig 的PigRunner类等效的 Hive 可以轻松地从 Java 程序中运行 HQL 脚本?

4

1 回答 1

1

Spring for Apache Hadoop框架具有Hive集成功能,查看源代码可能会让您了解如何从代码运行 hql 脚本。

另一方面,您也可以检查Hive源(尤其是CliSessionStateCliDriver)以查看Hive shell如何获取 hql 文件(即:) hive -f file.q

基于这些,这样的原始实现可以完成这项工作:

import java.io.PrintStream;
import org.apache.hadoop.hive.cli.CliDriver;
import org.apache.hadoop.hive.cli.CliSessionState;
import org.apache.hadoop.hive.common.LogUtils;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.ql.session.SessionState;

public class RunHQLScript {

    private static class MyCliSessionState extends CliSessionState {
        public MyCliSessionState(HiveConf conf, String host, int port) {
            super(conf);
            this.host = host;
            this.port = port;
        }
    }

    public static void main(String[] args) throws Exception {

        LogUtils.initHiveLog4j();
        CliSessionState ss = new MyCliSessionState(new HiveConf(SessionState.class),
                "localhost", 10000);

        ss.in = System.in;
        ss.out = new PrintStream(System.out, true, "UTF-8");
        ss.err = new PrintStream(System.err, true, "UTF-8");
        ss.fileName = "file.q";  //HQL file

        SessionState.start(ss);
        ss.connect();
        CliDriver cli = new CliDriver();
        int processFile = cli.processFile(ss.fileName);
        System.out.println("return code: " +processFile);
        ss.close();
    }
}

请注意,需要运行Thrift service(默认在端口 10000 上)才能执行脚本。

于 2012-10-07T13:12:15.610 回答