1
是否有一個與Pig的PigRunner類相當的Hive,可以很容易地從Java程序中運行HQL腳本?豬的PigRunner的蜂巢等價物?
是否有一個與Pig的PigRunner類相當的Hive,可以很容易地從Java程序中運行HQL腳本?豬的PigRunner的蜂巢等價物?
的Spring for Apache Hadoop
框架有一個Hive
融合,在看看 的source code可能給你一些想法如何從代碼中運行HQL腳本。
在另一方面,你也可以檢查Hive
源以及(尤其是CliSessionState和CliDriver) 怎麼看蜂巢外殼拿起一個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默認值)需要能夠執行腳本。