hive 基本操作之数据的导入导出、分区分桶

本文详细介绍了HDFS的基本操作,包括文件夹的创建、删除、上传、查看等,以及如何使用Hive进行表的创建、数据的加载与查询。涵盖了Hive表的创建、数据加载、数据导出、分区分桶等核心功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hdfs操作:

1)Hdfs上新建文件夹:-mkdir

例如:hdfs dfs -mkdir /db2_dir

2)删除文件夹:-rmr

hdfs dfs -rm -r /db2_dir

3)上传文件到hdfs指定文件夹:-put

hdfs dfs -put  file_name  destination_dir_name

4)查看hdfs上的文件:-ls

hdfs dfs  -ls  destination_dir_name

5)查看hdfs某文件的内容:-cat

hdfs dfs  -cat  /destination_dir_name/file_name   (对于大文件不建议直接查看)

6)将文件复制到本地文件系统:-get

hdfs  dfs  -get  file_name  local_des_dir_name

7)复制文件:-copyFromLocal

hdfs  dfs  -copyFromLocal  source_dir_name  des_dir_name

8)将两个文件合并起来:-getmerge

Hive 操作:

1) 创建表

建表语法:

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, col_name, ...) [SORTED BY (col_name [ASC|DESC], ...)] 

INTO num_buckets BUCKETS] [ROW FORMAT row_format] [STORED AS file_format] 

[LOCATION hdfs_path]

字段解释说明:

(1)CREATE TABLE 创建一个指定名字的表。如果相同名字的表已经存在,则抛出异常;用户可以用 IF NOT EXISTS 选项来忽略这个异常。

(2)EXTERNAL关键字可以让用户创建一个外部表,在建表的同时指定一个指向实际数据的路径(LOCATION),Hive创建内部表时,会将数据移动到数据仓库指向的路径;若创建外部表,仅记录数据所在的路径,不对数据的位置做任何改变。在删除表的时候,内部表的元数据和数据会被一起删除,而外部表只删除元数据,不删除数据。

(3)COMMENT:为表和列添加注释。

(4)PARTITIONED BY创建分区表

(5)CLUSTERED BY创建分桶表

(6)SORTED BY : 设置存储格式,我们指定的是ORC列存储格式,当正式表采用ORC格式时,原DB2导入数据表结构中的varchar字段类型,更改为string

(7)ROW FORMAT DELIMITED [FIELDS TERMINATED BY char] [COLLECTION ITEMS TERMINATED BY char] [MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char]| SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, ...)]

用户在建表的时候可以自定义SerDe或者使用自带的SerDe。如果没有指定ROW FORMAT 或者ROW FORMAT DELIMITED,将会使用自带的SerDe。在建表的时候,用户还需要为表指定列,用户在指定表的列的同时也会指定自定义的SerDe,Hive通过SerDe确定表的具体的列

的数据。

FIELDS TERMINATED BY:字段与字段之间的分隔符

COLLECTION ITEMS TERMINATED BY:一个字段各个item的分隔符

(8)LOCATION :指定表在HDFS上的存储位置。

注:如果表在创建时指定location。那么我们只需要直接把数据put到相应的location下就行。hdfs层面上load操作的本质就是移动。但是指定的location下只能有文件,不能在里面建文件夹,不然会报错:**** not a file 。hive表查询时会遍历location下的所有文件,hive表的内容,也是其location下所有文件的总和。更改表location的语句为:alter table table_name set location 'hdfs://****:9000/user/hive/warehouse/des_name'

1)查看表的类型
hive (default)> desc formatted student2;

2)加载数据
hive(default)>load data [local] inpath '/dta1/module/datas/student.txt' [overwrite] into table student [partition (partcol1=val1,…)];

(1)load data:表示加载数据

(2)local:表示从本地加载数据到hive表;否则从HDFS加载数据到hive表

(3)inpath:表示加载数据的路径

(4)into table:表示加载到哪张表

(5)student:表示具体的表

(6)overwrite:表示覆盖表中已有数据,否则表示追加

(7)partition:表示上传到指定分区

案例:

1) 加载本地文件到hive
hive (default)> load data local inpath '/dta1/module/datas/student.txt' into table default.student;

2) 加载HDFS上数据
hive (default)>load data inpath '/user/hadoop/hive/student.txt' into table default.student;

3) 加载数据覆盖表中已有的数据
hive(default)>load data inpath '/user/hadoop/hive/student.txt' overwrite into table default.student;

注:如果是load data local 模式,其实它是从本地copy一份数据,加载到表中,如果是load data,它是从原文件夹内,把数据移动到指定的表内,表的位置,在自己指定的(location)位置下,如果没指定location,那么就在默认位置/user/hive/warehouse/下。

2) 通过查询语句向表中插入数据(Insert)

1) 创建一张分区表
hive (default)> create table student(id string, name string) partitioned by (month string) row format delimited fields terminated by '\t';

2) 基本插入数据
hive(default)>insert into table student partition(month='201902') values('1004','wangle');

3) 基本模式插入(根据单张表查询结果)
hive (default)> insert overwrite table student partition(month='201902') select id, name from student where month='201811';

4) 多插入模式(根据多张表查询结果)
hive (default)> from student

                insert overwrite table student partition(month='201901')

                select id, name where month='201811'

                insert overwrite table student partition(month='201902')

                select id, name where month='201811';

5) 查询语句中创建表并加载数据(as select)
Hive(default)>create table if not exists student3 as select id, name from student;


3) 数据导出

(1)Insert导出

1) 将查询的结果导出到本地
hive (default)> insert overwrite local directory '/dta1/module/datas/export/student' select * from student;

2) 将查询的结果格式化导出到本地
hive (default)> insert overwrite local directory '/dta1/module/datas/export/student1'

                row format delimited fields terminated by '\t' 
              
                collection items terminated by '\n'

                select * from student;

3) 将查询的结果导出到HDFS上(没有local)
hive (default)> insert overwrite directory '/user/hadoop/hive/warehouse/student2'

                row format delimited fields terminated by '\t' 
                
                collection items terminated by '\n'

                select * from student;



(2) Hadoop命令导出
hive(default)>dfs -get /user/hive/warehouse/student /dta1/module/datas/export/student3.txt;

(3) Hive Shell 命令导出
[hadoop@**** hive]$bin/hive  -e  'select * from default.student;'  >/dta1/module/datas/export/student4.txt;

(4) Export导出到HDFS上
hive (default)> export table default.student to '/user/hive/warehouse/export/student';

4、分区分桶

  • 分区针对的是数据的存储路径;分桶针对的是数据文件
  • 分区提供一个隔离数据和优化查询的便利方式。不过,并非所有的数据集都可形成合理的分区,特别是之前所提到过的要确定合适的划分大小这个疑虑。
  • 分桶是将数据集分解成更容易管理的若干部分的另一个技术。

     

(1) 创建分区表
hive(default)>create table dept_partition(

              deptno int, dname string, loc string

              )

              partitioned by (month string)
 
              row format delimited fields terminated by '\t';

--注:分区的字段名和表内的字段。不能一样,partitioned by 语句要放在row format ···语句之前。

(2) 加载数据到分区表
--从本地加载
hive (default)> load data local inpath '/dta1/module/datas/dept.txt' into table dept_partition partition(month='201901');
--从hdfs加载
hive (default)> load  data  inpath  '/dta1/module/datas/dept.txt'  into  table dept_partition partition(month='201902');
--insert 数据
hive (default)> insert into table jjd_ partition(year='2019',month='7',day='5') values(1,'jjd');

(3) 查询分区表中数据
--单分区查询
hive (default)> select * from dept_partition where month='201901';
--多分区查询
hive (default)> select * from dept_partition where month='201901'

                union

                select * from dept_partition where month='201902'

                union

                select * from dept_partition where month='201812';

(4) 增加分区
--增加单个分区
hive (default)> alter  table  dept_partition add partition(month='201809') ;
--增加多个分区
hive (default)> alter  table  dept_partition add partition(month='201810') partition(month='201811');

(5) 删除分区
--删除单个分区 
hive (default)> alter table dept_partition drop partition (month='201809');
--删除多个分区
hive (default)> alter table dept_partition drop partition (month='201810'), partition (month='201811');
--注:外部表删除分区,不会删除数据,内部表删除分区时,会删除表内数据

(6) 查看分区数量
hive(default)>show partitions dept_partition;

(7) 查看分区表结构
hive(default)>desc formatted dept_partition;

​​​​​​​(8) 创建二级分区表
hive (default)> create table dept_partition2(

                deptno int, dname string, loc string

                )

                partitioned by (month string, day string)

                row format delimited fields terminated by '\t';
--1) 加载数据
hive(default)>load data local inpath '/dta1/module/datas/dept.txt' into table default.dept_partition2 partition(month='201809', day='22');
--2) 查询分区数据
hive (default)> select * from dept_partition2 where month='201809' and day='22';
--同理可以创建三级分区。

(9) 创建分桶
hive (default)> create table stu_buck(id int, name string)

                clustered by(id) into 4 buckets

                row format delimited fields terminated by '\t';
--1) 查看表结构
hive (default)> desc formatted stu_buck;

--2) 导数据到分通表
hive (default)> load data local inpath '/dta1/module/datas/student.txt' into table stu_buck;

 创建分桶表时,需要先设置一个参数hive (default)>set hive.enforce.bucketmapjoin=true;

这个参数也可以在hive配置项里直接设置好。Hive的参数信息可以在客户端直接修改,语法是 set hive.***.***=*** 例如:set hive.exec.compress.intermediate=true;这里是开启hive中间传输数据压缩功能,也可以直接set hive.***.***;查看hive的默认参数信息。

       1)分桶抽样查询

对于非常大的数据集,有时用户需要使用的是一个具有代表性的查询结果而不是全部结果。Hive可以通过对表进行抽样来满足这个需求

       2)查询表stu_buck中的数据

hive (default)> select * from stu_buck tablesample(bucket 1 out of 4 on id);

注:tablesample是抽样语句,语法:TABLESAMPLE(BUCKET x OUT OF y) 。

    y必须是table总bucket数的倍数或者因子。hive根据y的大小,决定抽样的比例。例如,table总共分了4份,当y=2时,抽取(4/2=)2个bucket的数据,当y=8时,抽取(4/8=)1/2个bucket的数据。

x表示从哪个bucket开始抽取。例如,table总bucket数为4,tablesample(bucket 4 out of 4),表示总共抽取(4/4=)1个bucket的数据,抽取第4个bucket的数据。

       3)数据块抽样

Hive的一种按照百分比进行抽样的方式,这种是基于行数的,按照输入路径下的数据块百分比进行的抽样。

hive (default)> select * from stu tablesample(0.1 percent) ;

提示:这种抽样方式不一定适用于所有的文件格式。另外,这种抽样的最小抽样单元是一个HDFS数据块。因此,如果表的数据大小小于普通的块大小128M的话,那么将会返回所有行。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JYWWABF

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值