1. 向数据表中载入|出数据:
1). 使用load关键字:
LOAD DATA [LOCAL] INPATH 'file_path' //inpath路径下不能有任何的文件夹。
[OVERWRITE] INTO TABLE table_name //overwrite:会将以前文件夹的内容删除再复制。
[PARTITION (col_name=col_value,...)] //如果分区不存在将会创建新的分区。
2).通过查询插入数据:
insert [overwrite] table table_name
[partition(col_name=col_value,...)]
select col_name,... from table_name
[where ...]
下面的方法可以写入到不同的分区中(静态的,因为需要指定具体分区):
from table_name_src
insert [overwrite] table table_name_des
[partition(col_name=col_value,...)
select ...
insert [overwrite] table table_name_des
[partition(col_name=col_value,...)]
select ...
动态分区导入:查询一条记录对应一个分区。
insert overwrite table table_name
partition(col_name_1,col_name_2,..)
select ...,src.col_name_1,src.col_name_2,...
from table_name_2 src;
e.g.
insert overwrite table backup_page_view
partition(country,language)
select uid,view_time,page_url,referrer_url,
country,language
from page_view
3).单个语句创建并导入数据:查询的列就作为新表的列。
create table table_name as
select col_name,col_name2,..
from table_name where ...
4).导出数据:
insert overwrite [local] directory 'path'
select col_name1,col_name2,..
from table_name
where ...
from table_name
insert overwrite [local] directory 'path1'
select col_name1,col_name2,...
where ...
insert overwrite [local] directory 'path2'
select col_name1,col_name2,...
where ...
2. 常用函数
1)算术运算符:
A%B :A除以B的余数
A&B :A和B按位取与
A|B :A和B按位取或
A^B :A和B按位取亦或
~B :A按位取反
2).常用函数:
round(Double d) : 返回d的近似值(bigint)
round(Double d,int n) : 返回d 的近似值,保留n位小数(double)
floor(Double d) : 返回 <=d 的最大bigint型值
ceil(Double d)|ceiling(Double d) : 返回 >=d 的最小bigint型值。
count(…)….和sql比较类似。
强制类型转换: cast(var as type)–>cast(0.1 as float):表示将0.1强制转换为float类型。
可以设置hive.map.aggr=true可以提高聚合的性能,会触发在map阶段进行顶级聚合过程
表生成函数:explode():可以将单列扩展为多列或多行,只能是collecion和map类型。
select explode(deductions) as (name, value) from employees
name value
grsds 12.3
zzs 32.3
grsds 38.0
zzs 48.0
css 73.0
- 对于join 进行连接时,应该将大表放到链接的最后位置。