Java 通过jdbc连接HIVESERVER
2014-06-10 00:00
204 查看
import java.sql.SQLException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.sql.DriverManager; public class HiveJdbcClient { private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver"; /** * @param args * @throws SQLException */ public static void main(String[] args) throws SQLException { try { Class.forName(driverName); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.exit(1); } Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/default", "", ""); Statement stmt = con.createStatement(); String tableName = "testHiveDriverTable"; stmt.executeQuery("drop table " + tableName); ResultSet res = stmt.executeQuery("create table " + tableName + " (key int, value string)"); // show tables String sql = "show tables '" + tableName + "'"; System.out.println("Running: " + sql); res = stmt.executeQuery(sql); if (res.next()) { System.out.println(res.getString(1)); } // describe table sql = "describe " + tableName; System.out.println("Running: " + sql); res = stmt.executeQuery(sql); while (res.next()) { System.out.println(res.getString(1) + "\t" + res.getString(2)); } // load data into table // NOTE: filepath has to be local to the hive server // NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per line String filepath = "/tmp/a.txt"; sql = "load data local inpath '" + filepath + "' into table " + tableName; System.out.println("Running: " + sql); res = stmt.executeQuery(sql); // select * query sql = "select * from " + tableName; System.out.println("Running: " + sql); res = stmt.executeQuery(sql); while (res.next()) { System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2)); } // regular hive query sql = "select count(1) from " + tableName; System.out.println("Running: " + sql); res = stmt.executeQuery(sql); while (res.next()) { System.out.println(res.getString(1)); } } }
相关文章推荐
- Java代码通过JDBC连接Hiveserver2
- Java代码通过JDBC连接Hiveserver2
- Hive:用Java代码通过JDBC连接Hiveserver
- Hive:用Java代码通过JDBC连接Hiveserver
- Java 连接hive2 server 通过jdbc 出现了问题 Required field 'client_protocol' is unset! Struct:TOpenSessionReq
- java通过jdbc驱动连接hive
- java通过jdbc连接hive1.0.0(伪分布)的问题
- Hive:用Java代码通过JDBC连接Hiveserver
- [置顶] Hive:用Java代码通过JDBC连接Hiveserver
- ubuntu16.04+hadoop2.7.2+hive1.2.1 server2通过jdbc连接
- java通过JDBC驱动连接操作hive实例
- 基于CDH5.x 下面使用eclipse 操作hive 。使用java通过jdbc连接HIVESERVICE 创建表
- java通过JDBC驱动连接操作hive实例
- java通过jdbc驱动连接hive操作实例
- Hive编程入门总结-用Java代码通过JDBC连接Hiveserver
- java 通过jdbc驱动连接hive操作实例
- java 通过jdbc驱动连接hive操作
- Java通过JDBC连接Hive
- 如何使用Java程序通过JDBC访问HDInsight Hive Server
- java 通过jdbc驱动连接hive操作实例