【Hive】(三)Hive数据导入,导出,分区

一、导入数据

load data

1、语法

load data (local) inpath '文件路径' into|overwrite table students;

local:表示从本地加载数据,不加local则是从HDFS中加载数据
overwrite:覆盖表中的原有数据
2、向stutest表加载数据
源数据

1 zhangsan yw 83
2 lisi sx 60
3 wangwu yw 84
4 zhaoliu sx 95
1 zhangsan sx 96
2 lisi yw 88
3 wangwu sx 80
4 zhaoliu yw 87

建一个内部表

hive> create table stutest(
    > stid  string,
    > stname string,
    > stpro string,
    > scores int
    > )
    > row format delimited fields terminated by ' ';

(1)加载本地文件到表中
导入后会在stutest的目录下多个文件

load data local inpath '/opt/soft/data/i.txt' into table stutest;
## 参数介绍1load data:表示加载数据 
(2local:表示从本地加载数据到 hive 表(复制);否则从 HDFS 加载数据到 hive 表(移动) 
(3)inpath:表示加载数据的路径 
(4)overwrite into:表示覆盖表中已有数据,否则表示追加 
(5into table:表示加载到哪张表 
(6)student:表示具体的表 
(7partition:表示上传到指定分区

在这里插入图片描述
(2)加载HDFS中的文件到表中
文件放到hdfs中

hdfs dfs -put /opt/soft/data/i.txt /student

加载数据

load data inpath '/student/i.txt' into table student;

(3)覆盖原有数据

load data inpath '/student/i.txt' overwrite into table student;

insert

1、语法 与mysql相似

insert into [table_name] values(field1,field2,...);

示例
创建一个外部表

hive> create external table student_ex(
    > stid string,
    > stname string,
    > stpro string,
    > score int
    > )
    > row format delimited fields terminated by ' ';

插入数据

hive> insert into student_ex values('1','test','yw',88);

通过查询一张表向另一张表插入数据

hive> insert overwrite table student_ex 
    > select stid,stname,stpro,scores from stutest;

import

导入数据

import table student from '文件路径';

location

创建表的时候指定数据路径,加载数据

create table if not exists stutest(
id int,name string,scores int
)
row format delimited fieds terminated by ','
location '文件路径'

三、导出数据

导出数据export

 export table mydemo.stutest to '文件路径'

查询结果导出到本地insert

insert overwrite local directory '/opt/data' 
	select * from student;

清空表数据
truncate 只能清空元数据表,不会删除外部表中的数据

truncate table student

清空元数据表,

drop table [table_name]

分区

  • 主要用于提高性能
      分区列的值将表划分为segments(文件夹)
      查询时使用“分区”列和常规列类似
      查询时Hive自动过滤掉不用于提高性能的分区
  • 分为静态分区和动态分区
    分区操作

1.建立一个外表

hive> create external table origninfos(
    > id string,
    > name string,
    > sex string,
    > age int
    > )
    > row format delimited fields terminated by ' '
    > location '/orign';

查询除表数据

hive> select * from origninfos;
OK
1	zhangsan	male	30
2	lisi	female	20
3	wangwu	male	26
4	zhaoliu	female	44
5	qiqi	unknown	42

2.建立分区表

hive> create external table partinfos(
    > id string,
    > name string,
    > age int
    > )partitioned by (sex string)
    > row format delimited fields terminated by ' ';

3.实现分区操作

hive> set hive.exec.dynamic.partition=true;
hive> set hive.exec.dynamic.partition.mode=nonstrict;
hive> insert into partinfos partition(sex) select id,name,age,sex from origninfos;

分区结果
在这里插入图片描述

例子

源数据

1 h01 zhangsan male 30
2 h03 lisi female 20
3 h01 wangwu male 26
4 h03 zhaoliu female 25
5 h03 qiqi unknown 25
6 h01 huahua female 24
7 h01 didi unknown 27
8 h03 haha male 26

创建元数据表(外表) 用来对比

create external table allinfos(
id int,
cla string,
name string,
sex string,
age int
)row format delimited fields terminated by ' ';
# 加载数据
load data local inpath '/opt/soft/data/td.txt' into table allinfos;

1、静态分区

1、创建一个分区表

create external table partinfos(
id int,
cla string,
name string,
age int
)
partitioned by(sex string)
row format delimited fields terminated by ' '

向分区表插入数据

load data local inpath '/opt/soft/data/part.txt' into table partinfos partition(sex='male');

2、动态分区

创建一个内部表

设置动态分区

set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict

插入数据

insert into table part_ partition(sex) select id,name,age,sex from origninfos;
发布了27 篇原创文章 · 获赞 19 · 访问量 1283

猜你喜欢

转载自blog.csdn.net/weixin_42804692/article/details/103568151