大神总结分享:Hive学会这些操作完全可以解决大部分问题(1)

大神总结分享:Hive学会这些操作完全可以解决大部分问题(1)

大神总结分享:Hive学会这些操作完全可以解决大部分问题

大神总结分享:Hive学会这些操作完全可以解决大部分问题(1)

  • 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学会这些操作完全可以解决大部分问题(1)

  1. hive -e 'select * from table'//执行完就shell就完了 加上-S可以去掉不必要的说明:比如OK等这类信息。

  2. hive -e 'select * from table' > /tmp/file/talbe //将输出信息重定向本地文件中

  3. hive -S -e "set" | grep warehouse //进行模糊搜索

  4. hive -f /path/s.hql//执行hql文件

  5. 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


分享到:


相關文章: