1. 程式人生 > >官方jdbc方式訪問hive伺服器

官方jdbc方式訪問hive伺服器

說明

Hive某種意義上來說是一個數據庫,也叫做資料倉庫,只不過資料最終儲存在hdfs上。而且sql最終都被翻譯成mapreduce而已,當然查詢效率也因此比較低。比較適合資料分析場合,實時性要求不高。訪問hive客戶端方式很多種,今天說一下jdbc方式訪問hive。為了更好表達官網使用原意義,在這裡程式碼部分只做紅色備註,但是不做翻譯。這樣會更加準確。

實戰

JDBC

This document describes the JDBC client for the original  (sometimes called Thrift server or HiveServer1

). For information about the HiveServer2 JDBC client, see . HiveServer2 use is recommended; the original HiveServer has several concurrency issues and lacks several features available in HiveServer2.

Version information

The original Hive Server was removed from Hive releases starting in 

version 1.0.0. See HIVE-6977.

For embedded mode, uri is just "jdbc:hive://". For standalone server, uri is "jdbc:hive://host:port/dbname" where host and port are determined by where the Hive server is run. For example, "jdbc:hive://localhost:10000/default". Currently, the only dbname supported is "default".

JDBC Client Sample Code

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"; 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: "