一、数据导入
1、向表中装载数据(Load)
1)语法
hive>load data [local] inpath '/usr/local/data/student.txt' [overwrite] into table student [partition (partcol1=val1,…)];
(1)load data:表示加载数据
(2)local:表示从本地加载数据到hive表;否则从HDFS加载数据到hive表,当从hdfs导入时,会把数据剪切到 表的目录下。
(3)inpath:表示加载数据的路径
(4)into table:表示加载到哪张表
(5)student:表示具体的表
(6)overwrite:表示覆盖表中已有数据,否则表示追加
(7)partition:表示上传到指定分区
2)实操案例
(0)创建一张表
create table student01(id string, name string) row format delimited fields terminated by '\t';
(1)加载本地文件到hive
load data local inpath '/usr/local/data/student.txt' into table student01;
(2)加载HDFS文件到hive中
上传文件到HDFS
dfs -put /usr/local/data/student.txt /user/student.txt;
加载HDFS上数据
load data inpath '/user/student.txt' into table student01;
(也可以直接把文件上传到 表所在hdfs的目录下)
select * from student01;
(3)加载数据覆盖表中已有的数据
上传文件到HDFS
dfs -put /usr/local/data/student2.txt /user/student.txt;
加载数据覆盖表中已有的数据
load data inpath '/user/student.txt' overwrite into table student01;
select * from student01;
2、通过查询语句向表中插入数据(Insert)
1)创建一张分区表
create table student2(id string, name string) partitioned by (month string) row format delimited fields terminated by '\t';
2)基本插入数据,支持插入, 不支持删除和更新(可以插入到普通表和分区表)(启动MR任务)
insert into table student2 partition(month='201909') values('1004','superjean');
select * from student2;
3)基本模式插入(根据单张表查询结果,启动MR任务)
insert overwrite table student2 partition(month='201909')
select id, name from student;
select * from student2;
4)多插入模式(一次插入多个表中)
create table student3(id string, name string) partitioned by (month string) row format delimited fields terminated by '\t';
from student
insert overwrite table student2 partition(month='201707')
select id, name where id>2
insert overwrite table student3 partition(month='201706')
select id, name where id<=2;
3、查询语句中创建表并加载数据(as select)
根据查询结果创建表(查询的结果会添加到新创建的表中)
create table if not exists student31 as select id, name from student;
4、创建表时通过location指定加载数据路径
1)创建表,并指定在hdfs上的位置( 指定数据存放位置,默认位置就不会生成文件夹。如果没有指定,就会在hdfs的默认位置建立表文件。)
create table if not exists student03(
id int, name string
)
row format delimited fields terminated by '\t'
location '/usr/student';
需先进行上传数据:
dfs -mkdir /usr/student;
2)上传数据到hdfs上
dfs -put /usr/local/data/student2.txt /usr/student/student.txt;
3)查询数据
select * from student03;
二、数据导出
1、insert导出
1)将查询的结果导出到本地
insert overwrite local directory '/usr/local/data/student03'
select * from student03;
注意,不能通过 insert into 进行数据导出。
更加注意!导出的路径,不能这样写:/usr/local/data ,否则会把 module里所有的东西都覆盖掉!一定要指定一个不存在的 新的 目录,把结果放到这个 student03目录里。
'/usr/local/data/student03' student03是目录,导入到本地后,会在student目录下产生一个文件:000000_0,内容为:
导出到本地的数据,列之间的符号是 ^A (ascii码是\001)
2)将查询的结果格式化导出到本地
insert overwrite local directory '/usr/local/data/student04'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
select * from student03;
3)将查询的结果导出到HDFS上(没有local)
insert overwrite directory '/usr/student03'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
select * from student03;
2、hive shell 命令导出
基本语法:(hive -f/-e 执行语句或者脚本 > file),需退出hive客户端
hive -e 'select * from student;' > /usr/local/data/student40.txt;
文本中的数据为:
第一行显示了列的信息,是因为在 hive-site.xml 中配置了显示列信息,去掉即可。
3、export导出到HDFS上
export table student03 to '/usr/student04';
会在 student目录下生成如下文件:
最终结果放在 data目录下,其实就是把 student03表所在的 hdfs路径下的 数据文件 拷贝到 你指定的目录下。