|NO.Z.00010|——————————|BigDataEnd|——|Hadoop&Hive.V10|——|Hive.v10|Hive数据库操作|load命令|

一、 数据导入
~~~     [Hive数据库操作之load命令]   
~~~     [Hive数据库操作之Insert命令]
~~~     [Hive数据库操作之数据导出]
### --- 装载数据(Load):基本语法:

LOAD DATA [LOCAL] INPATH 'filepath'
[OVERWRITE] INTO TABLE tablename [PARTITION (partcol1=val1,partcol2=val2 ...)]
~~~     LOCAL:
~~~     LOAD DATA LOCAL ... 从本地文件系统加载数据到Hive表中。本地文件会拷贝到Hive表指定的位置
~~~     LOAD DATA ... 从HDFS加载数据到Hive表中。HDFS文件移动到Hive表指定的位置
~~~     INPATH:加载数据的路径
~~~     OVERWRITE:覆盖表中已有数据;否则表示追加数据
~~~     PARTITION:将数据加载到指定的分区
二、准备工作:
### --- 准备工作:

~~~     # 创建表
hive (mydb)> CREATE TABLE tabA (
id int
,name string
,area string
) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' ;
~~~     # 数据文件(~/data/sourceA.txt):
[root@linux123 ~]# vim /home/hadoop/data/sourceA.txt
1,fish1,SZ
2,fish2,SH
3,fish3,HZ
4,fish4,QD
5,fish5,SR
 
~~~     # 拷贝文件到 HDFS
[root@linux123 ~]# hdfs dfs -put /home/hadoop/data/sourceA.txt data/
### --- 装载数据:

~~~     # 加载本地文件到hive(tabA)
hive (mydb)> LOAD DATA LOCAL INPATH '/home/hadoop/data/sourceA.txt' INTO TABLE tabA;

~~~     # 检查本地文件还在
hive (mydb)> select * from tabA;
taba.id taba.name   taba.area
1   fish1   SZ
2   fish2   SH
3   fish3   HZ
4   fish4   QD
5   fish5   SR
~~~     # 加载hdfs文件到hive(tabA)
LOAD DATA INPATH 'data/sourceA.txt' INTO TABLE tabA;

~~~     # 检查HDFS文件,已经被转移
~~~     # 加载数据覆盖表中已有数据
LOAD DATA INPATH 'data/sourceA.txt' OVERWRITE INTO TABLE tabA;
~~~     # 创建表时加载数据

[root@linux123 data]# pwd
/home/hadoop/data
[root@linux123 data]# hdfs dfs -mkdir /user/hive/tabB
[root@linux123 data]# hdfs dfs -put sourceA.txt /user/hive/tabB

hive (mydb)> CREATE TABLE tabB (
id INT
,name string
,area string
) ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
Location '/user/hive/tabB';
### --- 插入数据(Insert)

~~~     # 创建分区表
hive (mydb)> CREATE TABLE tabC (
id INT
,name string
,area string
)
partitioned by (month string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

~~~     # 插入数据
hive (mydb)> insert into table tabC
partition(month='202001')
values (5, 'wangwu', 'BJ'), (4, 'lishi', 'SH'), (3,'zhangsan', 'TJ');
~~~     # 插入查询的结果数据
hive (mydb)>  insert into table tabC partition(month='202002') select id, name, area from tabC where month='202001';

id  name    area
Time taken: 3.332 seconds

~~~     # 多表(多分区)插入模式
hive (mydb)> from tabC
insert overwrite table tabC partition(month='202003') select id, name, area where month='202002'
insert overwrite table tabC partition(month='202004') select id, name, area where month='202002';

id  name    area
Time taken: 3.525 seconds
### --- 创建表并插入数据(as select)

~~~     # 根据查询结果创建表
hive (mydb)> create table if not exists tabD as select * from tabC;
~~~     # 使用import导入数据
hive (mydb)> import table student2 partition(month='201709') from '/user/hive/warehouse/export/student';

三、数据导出
~~~     # 将查询结果导出到本地
hive (mydb)>  insert overwrite local directory '/home/hadoop/data/tabC' select * from tabC;

~~~     # 将查询结果格式化输出到本地
hive (mydb)>  insert overwrite local directory '/home/hadoop/data/tabC2'
row format delimited fields terminated by ' '
select * from tabC;
~~~     # 将查询结果导出到HDFS

hive (mydb)>  insert overwrite directory '/user/hadoop/data/tabC3'
row format delimited fields terminated by ' '
select * from tabC;
~~~     # dfs 命令导出数据到本地。本质是执行数据文件的拷贝
dfs -get /user/hive/warehouse/mydb.db/tabc/month=202001/home/hadoop/data/tabC4;

~~~     # hive 命令导出数据到本地。执行查询将查询结果重定向到文件
hive -e "select * from tabC" > a.log
~~~     # export 导出数据到HDFS。使用export导出数据时,不仅有数还有表的元数据信息
export table tabC to '/user/hadoop/data/tabC4';

~~~     # export 导出的数据,可以使用 import 命令导入到 Hive 表中
~~~     # 使用 like tname创建的表结构与原表一致。create ... as select ...结构可能不一致
create table tabE like tabc;
import table tabE from ''/user/hadoop/data/tabC4';
~~~     # 截断表,清空数据。(注意:仅能操作内部表)
truncate table tabE;

~~~     # 以下语句报错,外部表不能执行 truncate 操作
alter table tabC set tblproperties("EXTERNAL"="TRUE");
truncate table tabC;
### --- 小结:

~~~     数据导入:load data / insert / create table .... as select ..... / import table
~~~     数据导出:insert overwrite ... diretory ... / hdfs dfs -get / hive -e "select ..." >a.log / export table ...
~~~     Hive的数据导入与导出还可以使用其他工具:Sqoop、DataX等;

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

posted on   yanqi_vip  阅读(16)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示