一 导入数据
注意 —— 使用本地模式运行MR : set hive.exec.mode.local.auto=true;
1.方式一 :
load data [local] inpath ‘数据的path’ [overwrite]
into table 表名 [partition (partcol1=val1,…)];
#[local] :如果加上该字段表示从本地导入数据,如果不加表示从hdfs上导入数据
#[overwrite] :如果加上该字段那么会将原表中的数据覆盖掉,如果不加则将该内容追加到表中
#[partition (partcol1=val1,…)] :将数据导入到该表中的哪个分区中
2.方式二:
insert overwrite/into table 表名
#任意查询结果 - 将该结果插入到表中(注意:字段的个数和数据的类型)
select id, name from student where id < 1006;
注意:①insert into [table] .....
②insert overwrite table ......
3.方式三:
create table 表名
as select … #根据查询的结果创建成一张新表
4.方式四:
create table stu2(
id int,
name string
)
row format delimited fields terminated by ‘\t’
location ‘/data’
5.方式五:import要导入的表不可以存在
Import数据到指定Hive表中
hive (default)> import table 表名 from ‘hdfs路径’
二 导出数据
1.方式一
1)将查询的结果导出到本地
insert overwrite
[local] directory ‘/opt/module/hive/datas/export/student’ #本地路径
[row format delimited fields terminated by ‘\t’] #让导出的数据有分割符
select * from student;
#[local]-如果加上该字符数据导入到本地,如果不加该字符数据导入到hdfs上
2.方式二:
Hadoop命令导出到本地
hive (default)> dfs -get ‘hdfs上表的数据的路径’ ‘本地路径’
hadoop fs -get 'hdfs上表的数据的路径' '本地路径'
3.方式三:
Hive Shell 命令导出: hive -e ‘sql语句’ > ‘本地路径’
4.方式四:
Export导出到HDFS上
hive (default)> export table 表名 to ‘hdfs路径’