首页 技术 正文
技术 2022年11月21日
0 收藏 358 点赞 4,917 浏览 2180 个字

在hive中表的类型:管理表和托管表(外部表)。

内部表也称之为MANAGER_TABLE,默认存储在/user/hive/warehouse下,也可以通过location指定;删除表时,会删除表的数据以及元数据;

外部表称之为EXTERNAL_TABLE。在创建表时可以自己指定目录位置(LOCATION),数据存储所在的目录;删除表时,只会删除元数据不会删除表数据;

创建外部表实例

create external table if not exists default.emp_ext(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int
)
row format delimited fields terminated by '\t'
location '/opt/input/emp';

分区表实际上就是对应一个HDFS文件系统上的独立的文件夹,该文件夹下是该分区所以的数据文件。hive中的分区就是分目录,把一个大的数据集根据业务需要分割成更小的数据集。

在查询时通过WHERE子句中的表达来选择所需要的指定的分区,这样的查询效率会提高很多。

create external table if not exists default.emp_partition(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int
)
partitioned by(month string)
row format delimited fields terminated by '\t';

分区表注意事项:

修复表:msck repair table table_name;

可以写shell脚本

dfs -mkdir -p /user/hive/warehouse/dept_part/day=;
dfs -put /opt/weblog/log.log /user/hive/warehouse/dept_part/day=;alter table dept_part and partition('day=20171025');

查看表的分区数:show partitions dept_part;

导入数据进入hive表

load data [local] inpath ‘filepath’ [overwrite] into table tablename into tablename [partition (partcol1=val,…)];

参数带local意思是本地文件,不带就是HDFS文件

参数带overwrite意思是覆盖原本文件的内容,不带就追加内容

分区表加载,特殊性partition (partcol1=val,…)

1.加载本地文件到hive表

load  data  local  inpath '/root/emp.txt' into  table  default.emp

2.加载hdfs文件到hive表中

load  data  inpath '/root/emp.txt' into  table  default.emp

3.加载数据覆盖表中已有的数据

load  data  inpath '/root/emp.txt' overwrite into  table  default.emp

4.创建表是通过insert加载

create table    default.emp_ci like emp;
insert into table default.emp_ci select * from default.emp;

5.创建表的时候通过指定location指定加载

导出hive表数据

insert overwrite local directory '/opt/datas/hive/hive_exp_emp'  select * from default.emp row format delimited fields terminated by '\t';#bin/hive -e "select * from default.emp;" > /opt/datas/hive/exp_res.txt

hive表多重插入
假如有一个需求:
从t_4中筛选出不同的数据,插入另外两张表中;

insert overwrite table t_4_st_lt_200 partition(day='')
select ip,url,staylong from t_4 where staylong<;insert overwrite table t_4_st_gt_200 partition(day='')
select ip,url,staylong from t_4 where staylong>;

但是以上实现方式有一个弊端,两次筛选job,要分别启动两次mr过程,要对同一份源表数据进行两次读取
如果使用多重插入语法,则可以避免上述弊端,提高效率:源表只要读取一次即可

from t_4
insert overwrite table t_4_st_lt_200 partition(day='')
select ip,url,staylong where staylong<
insert overwrite table t_4_st_gt_200 partition(day='')
select ip,url,staylong where staylong>;
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,028
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,367
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,858