大神總結分享:Hive學會這些操作完全可以解決大部分問題
hive --help//hive幫助命令
hive>dfs -lsr /;//在hive中直接調用hadoop命令
hive>! shell命令//利用!在hive中調用shell命令
hive> ! ls / 在hive中可以執行shell命令 ,在前面加上!並不支持管道和文件自動補全。
>hive set;//可以顯示很多的變量
>hive set hive.cli.print.header=true; //設置顯示字段名
>hive source /path/s.hql //在客戶端中執行hql文件
hive 參數
hive -e 'select * from table'//執行完就shell就完了 加上-S可以去掉不必要的說明:比如OK等這類信息。
hive -e 'select * from table' > /tmp/file/talbe //將輸出信息重定向本地文件中
hive -S -e "set" | grep warehouse //進行模糊搜索
hive -f /path/s.hql//執行hql文件
hive -i file //允許用戶在啟動hive之前,執行一些配置文件,在執行時,hive會在當前目錄下找.hiverc文件。
hive的數據類型
tinyint,smalint,int,bigint,boolean,float,double,timestamp(),binary() //binary()表示字節數組
集合數據類型
STRUCT
MAP
ARRAY
修改表註釋 列名
-
alter table 表名 set tblproperties('comment'='註釋');
alter table 表名 change column column_old column_new varchar(2000) comment '註釋';
增長列
alter table table_name add columns (col_name data_type [comment col_comment], ...)
alter table table_name partition partition_spec add columns (col_name data_type [comment col_comment], ...)
刪除分區
alter table dwa_cs_host_d drop partition (acct_day='20140601',xieyi='1');
cast(s AS INT)//轉換類型函數
show locks; //查看鎖表
注意:如果一個表的表結構指定的是3列,而實際數據文件每行記錄包含有5個字段的話,那麼在hive中最後2列數據將會被省略掉。
hive中的正則表達式
1.regexp
語法: A REGEXP B
操作類型: strings
描述: 功能與RLIKE相同
select count(*) from olap_b_dw_hotelorder_f where create_date_wid not regexp '\\d{8}'
與下面查詢的效果是等效的:
select count(*) from olap_b_dw_hotelorder_f where create_date_wid not rlike '\\d{8}';
2.regexp_extract
語法: regexp_extract(string subject, string pattern, int index)
返回值: string
說明:將字符串subject按照pattern正則表達式的規則拆分,返回index指定的字符。
hive> select regexp_extract('IloveYou','I(.*?)(You)',1) from test1 limit 1;
love
hive> select regexp_extract('IloveYou','I(.*?)(You)',2) from test1 limit 1;
You
hive> select regexp_extract('IloveYou','(I)(.*?)(You)',1) from test1 limit 1;
I
hive> select regexp_extract('IloveYou','(I)(.*?)(You)',0) from test1 limit 1;
IloveYou
hive> select regexp_replace("IloveYou","You","") from test1 limit 1;
Ilove
3.regexp_replace
語法: regexp_replace(string A, string B, string C)
返回值: string
說明:將字符串A中的符合Java正則表達式B的部分替換為C。
注意,在有些情況下要使用轉義字符,類似Oracle中的regexp_replace函數。
hive> select regexp_replace("IloveYou","You","") from test1 limit 1;
Ilove
hive> select regexp_replace("IloveYou","You","lili") from test1 limit 1;
Ilovelili
閱讀更多 IT大數據科技 的文章