是否有一个与Pig的PigRunner类等效的Hive,可以很容易地从Java程序中运行HQL脚本?
答案 0 :(得分:1)
Spring for Apache Hadoop
框架有一个Hive
集成,请查看
source code可能会让您知道如何从代码运行hql脚本。
另一方面,您也可以查看Hive
来源(尤其是CliSessionState和CliDriver)
看看 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)才能执行脚本。