Apache Hive--DDL--创建表--分桶表创建&分桶数据导入| 学习笔记

开发者学堂课程【大数据Hive教程精讲Apache Hive--DDL--创建表--分桶表创建&分桶数据导入】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/90/detail/1371


Apache Hive--DDL--创建表--分桶表创建&分桶数据导入


内容介绍:

一、分装桶

二、课堂笔记

一、分装桶

CREATE[EXTERNAL] TABLE [IF NOT EXISTS] table_name

[(col_name data_type [COMMENT col_comment], .….)]

[COMMENT table_comment]

[PARTITIONED BY (col_name data_type

[COMMENT col_comment],.….)]

[CLUSTERED BY (col_name, cor_name,...)

[SORTED BY (col_name [ASC|DESC], .….)]INTO num_buckets BUCKETS]

[ROW FORMAT row_format]

[STORED AS file_format][LOCATION hdfs_path]

分桶表(cluster byinto num buckets)

#指定开启分桶

set hive.enforce.bucketing = true;

set mapreduce.job.reduces=4;

TRUNCATETABLEstu_buck;

drop table stu_buck;

create table stu_buck(Sno int,Sname string,Sex string,Sage int,sdept string

clustered by (sno)

sorted by (sno DESC)

into 4 buckets

row format delimited

fields terminated by ',';

 


二、课堂笔记

1、hive 建立一张表跟已经存在的结构化的数据文件产生映射关系

映射成功之后,就可以通过写sql来分析这结构化的数据避免了写mr程序的麻烦

2、数据库---》/user/hive/warehouse 下的一个文件夹对应

表---》数据库文件夹下面的子文件夹/user/hive/warehouse/itcast

.db/t_t1

表的数据位置目前不能随便存放一定要在指定的数据库表的文件夹下面

建立表的时候可能还需要指定分隔符否则有可能映射不成功

3、建表的时候一定要根据结构化数据文件的分隔符类型指定分隔符

建表的字段个数和字段类型要跟结构化数据中的个数类型一致

分隔符一般使用内置的来指定 ROW FORMAT DELIMITED 分割字段﹐还是分割集合等等

4、分区表字段不能够在表中已经存在

分区字段是一个虚拟的字段不存放任何数据

分区字段的数据来自于装载分区表数据的时候指定的

分区表的字段﹐在hdfs上的效果就是在建立表的文件夹下面又创建了子文件

这样的目的把数据的划分更加细致减少了查询时候全表扫描的成本只需要按照指定的分区扫描数据并显示结果即可

5、分桶表创建之前需要开启分桶功能

分桶表(分簇表)创建的时候分桶字段必须是表中已经存储的字段也就是说你要按照表中那个字段进行分开

针对分桶表的数据导入: load data 方式不能够导成分桶表的数据没有分桶效果

原因在于 load 本质上相当于 hive 去帮我们执行 hadoop fs -put

zhangsanbeijing ,shanghai ,tianjin,hangzhouwangwushanghai ,chengdu , wuhan ,haerbin

create table complex_array (name string ,work_locations array<string>)

ROw FORMAT DBLIMITED FIELDS TERMINATED BY '\t​​’COLLECTION ITEMS TERMINATED BY ', ' ;

create table t_user(id int​​,name string) partitioned by (country string) row format delimited fields terminat&

LOAD DAnA 1ocal INPATH '/root/hivedata/5.txt​​’INWO ABLE

day _hour_table partition(dt=120170101',hour='08');

create table stu_buck(Sno int,Sname string,Sex string ,Sage int ,sdept string)

clustered by (sno)

into 4 buckets

row format delimited

fields terminated by ' , ';

LOAD DaTA local INPATH '/root/hivedata/students.txt' INTO TABEE stu_buck;

insert overwrite table stu buck

select * from student cluster by (sno) ; I

create table student(Sno int,Sname string ,Sex string,sage int ,sdept string)row format delimited

fields terminated by ', ';

LOAD DAPA local INPATH '/root/hivedata/students.txt ​​’ INTO PABLE student;

Apache Hive--DDL--创建表--分桶表创建&amp;分桶数据导入| 学习笔记

上一篇:JavaScript使用DeviceOne开发实战(三)仿微信应用


下一篇:小程序项目里各文件的内容和关系