Java 直连Hive Server运行Hive SQL

    最近在做个简单的hive 查询调度系统, 需要实现用java 连接hive server运行sql,并需要把结果自动导入到mysql中, 直接上代码:


import com.google.common.collect.Lists; import org.apache.commons.dbutils.DbUtils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.List; /** * Created with IntelliJ IDEA. * User: hotallen * Date: 2016/1/5 * Time: 17:23 * To change this template use File | Settings | File Templates. */ public class HiveContext { private static final String HIVE_DRIVER = "org.apache.hive.jdbc.HiveDriver"; private static final String HIVE_SERVER = "jdbc:hive2://10.0.0.1:10000/default"; private static final String HIVE_USER = "ab_user"; private static Connection connection = null; private static ThreadLocal<connection> threadLocal = new ThreadLocal<connection>(); static { try { Class.forName(HIVE_DRIVER); } catch (ClassNotFoundException e) { throw new ExceptionInInitializerError(e); } } public static List<string> shellContext() { List<string> shells = Lists.newArrayList(); shells.add("set mapred.job.priority = LOW"); shells.add("set hive.exec.compress.intermediate = true"); shells.add("set hive.exec.compress.output = true"); shells.add("set mapred.compress.map.output = true"); shells.add("set mapred.map.output.compression.type = BLOCK"); shells.add("set mapred.map.output.compression.codec = org.apache.hadoop.io.compress.SnappyCodec"); shells.add("set mapred.job.queue.name = " + HIVE_USER); shells.add("SET hive.auto.convert.join = false"); shells.add("SET mapreduce.job.name = ab_" + Thread.currentThread().getStackTrace()[4].getClassName()); shells.add("use fdm"); return shells; } public static Connection getConnection() throws SQLException { Connection conn = threadLocal.get(); if (conn == null) { conn = makeConnection(); threadLocal.set(conn); } return conn; } private static Connection makeConnection() throws SQLException { connection = DriverManager.getConnection(HIVE_SERVER, HIVE_USER, ""); return connection; } public void close() { try { Connection conn = threadLocal.get(); if (conn != null) { DbUtils.closeQuietly(conn); threadLocal.remove(); } } catch (Exception e) { throw new RuntimeException(e); } } }

你可能感兴趣的:(java,hive)