/*
http://wiki.apache.org/hadoop/Hive/HiveClient#Thrift_Java_Client
*/
importPackage(java.sql);
importPackage(java.lang);
Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");
var tableName = "testHiveDriverTable";
// make connection
var conn = DriverManager.getConnection("jdbc:hive://localhost:10000/default", "", "");
var stmt = conn.createStatement();
// init
stmt.executeQuery("drop table " + tableName);
stmt.executeQuery("create table " + tableName + " (key int, value string)");
// show tables
var sql = "show tables '" + tableName + "'";
print("Running: " + sql);
var resp = stmt.executeQuery(sql);
if (resp.next()) {
print(resp.getString(1));
}
// describe table
sql = "describe extended " + tableName;
print("Running: " + sql);
resp = stmt.executeQuery(sql);
while (resp.next()) {
print(resp.getString(1) + "\t" + resp.getString(2));
}
// load data into table
var filepath = "/tmp/a.txt";
sql = "load data local inpath '" + filepath + "' into table " + tableName;
print("Running: " + sql);
resp = stmt.executeQuery(sql);
// select * query
sql = "select * from " + tableName;
print("Running: " + sql);
resp = stmt.executeQuery(sql);
while (resp.next()) {
print(String(resp.getInt(1)) + "\t" + resp.getString(2));
}
// regular hive query
sql = "select count(1) from " + tableName;
print("Running: " + sql);
resp = stmt.executeQuery(sql);
while (resp.next()) {
print(resp.getString(1));
}
一応、走らせるときのbuild.xmlもサンプルとして。
<path id="clspath">
<fileset dir="build/jar">
<!-- メインの読込み -->
<include name="*.jar"/>
</fileset>
<fileset dir="${hive.dir}">
<!-- HIVEのjarを読込み -->
<include name="**/*.jar"/>
</fileset>
<fileset dir="${hadoop.dir}">
<!-- Hadoopのjarを読込み -->
<include name="*.jar"/>
</fileset>
</path>
<!-- blog-gae>ant rhino-run -Dfile.encoding=UTF-8 -Dscript="env0.js" -->
<target name="rhino-run">
<echo message="Running ${script}"/>
<java fork="yes"
classname="org.mozilla.javascript.tools.shell.Main"
failonerror="true">
<classpath>
<path refid="clspath" />
<pathelement path="js.jar" />
</classpath>
<arg value="${script}" />
</java>
</target>
HIVEの力なんだけど、とても簡単にM/Rが動くのは素晴らしい、と思います。