HIVE常用操作以及函数

一、常用操作

1. 查看数据表列表

show tables [like '*name*'];

2. 创建表

内部表和外部表区别:内部表即完全交给hive管理表,在创建时会将数据移动到数据仓库所 在的路径,删除时会删除数据源文件。外部表即增加hive管理的数据文件,创建时需要记录 数据所在的路径,不会移动数据源文件,删除时不会删除数据源文件

  • 创建内部表
create table {tableName}(
    {columnName} {columnType},
    {columnName} {columnType}
) [row format delimited fields terminated by '\t'];
  • 创建外部表
create external table {tableName}(
    {columnName} {columnType},
    {columnName} {columnType}
) [row format delimited fields terminated by '\t' location '{HDFS_path}'];
  • 创建分区表
create [external] table {tableName}(
    {columnName} {columnType},
    {columnName} {columnType}
) 
partitioned by(
    {columnName} {columnType},
    {columnName} {columnType}
) [row format delimited fields terminated by '\t' location '{HDFS_path}'];

默认分隔符为'\001'

3. 查看表信息

  • 查看表结构
desc {tableName};
或
{desc formatted}/{describe} {tableName};
  • 查看分区信息
show partitions {tableName};

4. 数据导入

  • 文件导入
// 从hdfs中导入,是否覆盖
load data inpath '{HDFSPath}' [overwrite] into table {tableName};
// 从linux本地导入,是否覆盖
load data local inpath '{localPath}' [overwrite] into table {tableName};
  • hive数据查询导入
// insert into与sql语法一致
insert [overwrite] into table {dstTableName} select {needColumns} from {srcTableName};

5. 查看可用函数

// 可用函数列表
show functions;
// 查看函数描述信息
desc function {functionName};

二、常用函数

1. 取整函数

  • 四舍五入:round({value}[,precision])

1~4:舍,5~9:进,传入两个参数时可指定精度

  • 银行家舍入法:bround({value}[,precision])

1~4:舍,6~9:进,5->前一位是偶数:舍,前一位是奇数:进,传入两个参数时可指定精 度

2. 向下取整函数

floor({value})

3. 向上取整函数

ceil/ceiling({value})

4. 生成随机数

rand([{seed}]):返回一个0到1范围内的随机数,传入参数时可生成稳定的随机数

5. 自然指数函数

自然指数e的n次方:exp({n})

6. 对数函数

  • 以10为底的对数函数:log10({value})
  • 以2为底的对数函数:log2({value})
  • 以e为底的对数函数:ln({value})
  • 对数函数:log({base},{value})

7. 获取日期函数

unix_timestamp() //获取的是时间戳

8. 时间戳转换函数

  • UNIX时间戳转日期:from_unixtime({unixTime}[,{formatString}])
  • 日期转UNIX时间戳:unix_timestamp({timeString}[,{formatString}])

9. 日期截取函数

  • 返回日期部分:to_date({timeString})
  • 返回日期的年:year({timeString})
  • 返回日期的月:month({timeString})
  • 返回日期的天:day({timeString})
  • 返回日期的时:hour({timeString})
  • 返回日期的分:minute({timeString})
  • 返回日期的秒:second({timeString})
  • 返回日期的周:weekofyear({timeString})

10. 日期计算函数

  • 日期比较函数:datediff({endDate},{startDate})
  • 日期增加函数:date_add({startData},{days})
  • 日期减少函数:date_sub({startData},{days})

11. 字符串函数

  • 字符串长度函数:length({stringValue})
  • 字符串反转函数:reverse({stringValue})
  • 字符串连接函数

无分隔符连接函数:concat({stringValues})

分隔符连接函数:concat_ws({separator},{stringValues})

12. 字符串截取函数

  • substr/substring({stringValue},{index}):当index为正数时,截取从index至结尾的字符 串,当index为负数时,截取后index个字符,index的值不能超过字符串长度
  • substr/substring({stringValue},{index},{length}):截取从index开始,长度为length的字 符,index为正数时,索引从左边开始,index为负数时,索引从右边开始

13. 大小写转换函数

  • 转大写函数:upper/ucase ({stringValue})
  • 转小写函数:lower/lcase({stringValue})

14. 去空格函数

  • 两边去空格函数:trim({stringValue})
  • 左边去空格函数:ltrim({stringValue})
  • 右边去空格函数:rtrim({stringValue})

转载于:https://my.oschina.net/yulang/blog/2051626

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值