1. 程式人生 > >HIVE一些小技巧和java操作hive

HIVE一些小技巧和java操作hive


這裡寫圖片描述
開啟微信掃一掃,關注微信公眾號【資料與演算法聯盟】

1.寫在前邊的話

        自己電腦上部署的hive版本是hive1.1.1,在終端執行hive 命令時,凡是涉及where語句時,就會出現異常退出,對於一個開發者怎麼能忍受這種情況,於是果斷升級到hive1.21
        同時需要注意的是在hive 1.1.1版本中建立table的時候,最後凡是新增一個stored as…的時候也會報錯退出,暫時不清楚原因,不過小主估計也是hive版本的問題。(因為版本換到1.2.1就正常了)

2.Hive中所謂的Derby單使用者

         其實我們好多人都知道hive裡預設配置一個derby資料庫,只支援單使用者操作,其實不然,只要保證在同一個資料夾下啟動hive資料庫,也是支援多使用者的,但是當有很多臺客戶一個例子端訪問hive時,並不能保證在同一目錄下,所以就成了單使用者操作,同樣對於同一個使用者來說,只要啟動hive資料庫的目錄不一樣,同樣看不到他在另外一個資料夾下建立的資料庫,因為derby在啟動hive的時候會在啟動目錄建立元資料庫,所以當環一個目錄時,元原先的元資料庫資訊並不會遷移

3.hive使用的一些小技巧

hadoop 的safenode

有時候我們會發現執行hive語句總會報錯,原因便是hadoop處於安全模式下,此時就需要離開safenode了,命令是

hadoop dfsadmin -safenamenode leave

顯示當前操作的資料庫

在hive中執行

set hive.cli.print.current.db=true

這裡寫圖片描述

查詢時顯示相應的欄位名

set hive.cli.print.header=true

這裡寫圖片描述

顯示錶具體資訊

正常人是這樣檢視的

desc table_name;
這裡寫圖片描述

             知道多點的是這樣檢視的

desc extended hot_word;
這裡寫圖片描述

             知道更多的人是這樣檢視的

desc formatted tablename; (太長了截圖不夠,就貼程式碼了)

hive (weibohotword)> desc formatted hot_word;
OK
col_name    data_type   comment
# col_name              data_type               comment             

day                     string                                      
hour                    string                                      
word                    string                                      
num                     int                                         

# Partition Information      
# col_name data_type comment timeday string # Detailed Table Information Database: weibohotword Owner: root CreateTime: Fri Sep 16 17:55:58 CST 2016 LastAccessTime: UNKNOWN Protect Mode: None Retention: 0 Location: hdfs://localhost:9000/user/hive/warehouse/weibohotword.db/hot_word Table Type: MANAGED_TABLE Table Parameters: comment this is hot words table transient_lastDdlTime 1474019758 # Storage Information SerDe Library: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe InputFormat: org.apache.hadoop.mapred.TextInputFormat OutputFormat: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat Compressed: No Num Buckets: -1 Bucket Columns: [] Sort Columns: [] Storage Desc Params: field.delim \t line.delim \n serialization.format \t Time taken: 0.184 seconds, Fetched: 37 row(s)

顯示建立表的語句

show create table hot_word;

hive (weibohotword)> show create table hot_word;
OK
createtab_stmt
CREATE TABLE `hot_word`(
  `day` string, 
  `hour` string, 
  `word` string, 
  `num` int)
COMMENT 'this is hot words table'
PARTITIONED BY ( 
  `timeday` string)
ROW FORMAT DELIMITED 
  FIELDS TERMINATED BY '\t' 
  LINES TERMINATED BY '\n' 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  'hdfs://localhost:9000/user/hive/warehouse/weibohotword.db/hot_word'
TBLPROPERTIES (
  'transient_lastDdlTime'='1474019758')
Time taken: 0.198 seconds, Fetched: 19 row(s)

4.自定義UDF(使用者自定義函式)

IDEA建立專案引入hive lib目錄下的jar包

需要引入hadoop的jar包和hive的jar包,引入後整個專案如圖:

這裡寫圖片描述

編寫UDF程式碼

/**
 * Created by thinkgamer on 16-9-17.
 */
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;

public class JDBCHive {
    private static String Driver ="org.apache.hive.jdbc.HiveDriver";
    private static String url = "jdbc:hive2://127.0.0.1:10000";
    private static String name = "root";
    private static String password = "root";

    public static void main(String[] args) {
        try {
            Class.forName(Driver);
            Connection conn = DriverManager.getConnection(url, name, password);
            Statement stat = conn.createStatement();
            String sql = "show databases";
            ResultSet rs = stat.executeQuery(sql);
            while (rs.next()) {
                System.out.println(rs.getString(1));//hive的索引從1開始
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

檢視效果

Picked up _JAVA_OPTIONS:   -Dawt.useSystemAAFontSettings=gasp
2016-09-17 01:26:13,988 INFO  jdbc.Utils (Utils.java:parseURL(310)) - Supplied authorities: 127.0.0.1:10000
2016-09-17 01:26:13,992 INFO  jdbc.Utils (Utils.java:parseURL(397)) - Resolved authority: 127.0.0.1:10000
2016-09-17 01:26:14,216 INFO  jdbc.HiveConnection (HiveConnection.java:openTransport(203)) - Will try to open client transport with JDBC Uri: jdbc:hive2://127.0.0.1:10000
default
weibohotword

Process finished with exit code 0

可以看出hive資料倉庫中有兩個資料庫分別是default和weibohotword

hiveserver2的官網介紹:點選閱讀