使用load data形式导入数据
总结
测试案例
从linux上正常load data
从HDFS上正常 load data
普通表指定分区的情况下load data
分区表未指定分区的情况下load data
字段缺少的情况下load data
字段过多的情况下load data
字段类型不一致的情况下load data

这里我们重点关注在HIVE中使用load data形式导入数据可能出现的一些情况。避免踩坑。
总结
这里我们根据实际情况作出一些总结,并针对不同的情况进行简单测试。

使用load data形式往hive表中装载数据时,如果使用关键字local,表示从本地文件系统中导入,这里是文件被拷贝到HDFS上。
使用load data形式往hive表中装载数据时,如果不使用关键字local,表示从HDFS文件系统中导入,这里是文件的移动。
使用load data形式往hive表中装载数据时,如果使用关键字overwrite,表示执行数据覆盖操作,原有数据会被全部覆盖(如果是分区表,则覆盖指定分区)。
使用load data形式往hive表中装载数据时,如果不使用关键字overwrite,表示执行数据追加操作,原有数据不会被覆盖。
使用load data形式往hive表中装载数据时,不会检查字段类型,如果字段类型不一致,使用null值填充。如果字段过多则会丢弃,缺失则会使用null值填充。
测试案例
下面是具体实现以及简单测试。
先新建两张测试表test_01和test_02,其中test_01为普通表,test_02为分区表。

CREATE TABLE IF NOT EXISTS `test_01`(
  `id` STRING,`name` STRING,`age` INT,`score` FLOAT)
ROW FORMAT DELIMITED  FIELDS TERMINATED BY ',' STORED AS TEXTFILE;

CREATE TABLE IF NOT EXISTS `test_02`(
  `id` STRING,`name` STRING,`age` INT,`score` FLOAT)
PARTITIONED BY (`dataday` STRING)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE;

这里分情况分别进行测试

从linux上正常load data
测试数据a.txt
[root@nd2 wh]# cat a.txt
1,lucy,20,90
2,Marry,21,95
3,Tom,22,100

这里我们将a.txt文件内容分别导入到test_01和test_02表中
--使用overwrite覆盖操作
hive> load data local inpath '/usr/wh/a.txt' overwrite into table test_01; 
hive> select * from test_01;
1    lucy    20    90.0
2    Marry    21    95.0
3    Tom    22    100.0
--这里我们不使用overwrite,数据会被直接追加
hive> load data local inpath '/usr/wh/a.txt'  into table test_01; 
hive> select * from test_01;
1    lucy    20    90.0
2    Marry    21    95.0
3    Tom        22    100.0
1    lucy    20    90.0
2    Marry    21    95.0
3    Tom        22    100.0
--再次使用overwrite 验证数据是否被覆盖
hive> load data local inpath '/usr/wh/a.txt' overwrite into table test_01; 
hive> select * from test_01;
1    lucy    20    90.0
2    Marry    21    95.0
3    Tom        22    100.0

--导入分区表需要指定分区
hive> load data local inpath '/usr/wh/a.txt' overwrite into table test_02 
partition (dataday='20190501');
hive> select * from test_02;
1    lucy    20    90.0    20190501
2    Marry    21    95.0    20190501
3    Tom        22    100.0    20190501

从HDFS上正常 load data
测试数据a.txt
[root@nd2 wh]# hadoop fs -cat /wh/test/a.txt
1,lucy,20,90
2,Marry,21,95
3,Tom,22,100

这里我们将a.txt文件内容分别导入到test_01和test_02表中
hive> load data inpath 'hdfs://nameservice1/wh/test/a.txt' overwrite into table test_01; 
hive> select * from test_01;
OK
1    lucy    20    90.0
2    Marry    21    95.0
3    Tom        22    100.0

--需要注意的是,从HDFS上导入数据到HIVE表,此处是文件的移动,所以这里我们需要重新放一份文件到HDFS上
--[root@nd2 wh]# hadoop fs -ls /wh/test/
--[root@nd2 wh]# hadoop fs -put a.txt /wh/test/

hive> load data inpath 'hdfs://nameservice1/wh/test/a.txt' overwrite into table test_02 partition (dataday='20190501');
hive> select * from test_02;
1    lucy    20    90.0    20190501
2    Marry    21    95.0    20190501
3    Tom        22    100.0    20190501

普通表指定分区的情况下load data
这里我们将a.txt文件内容导入到test_01,这里直接报错
hive> load data local inpath '/usr/wh/a.txt' overwrite into table test_01 partition (dataday='20190501');
FAILED: ValidationFailureSemanticException table is not partitioned but partition spec exists: {dataday=20190501}

分区表未指定分区的情况下load data
这里我们将a.txt文件内容导入到test_01,这里直接报错
hive> load data local inpath '/usr/wh/a.txt' overwrite into table test_02; 
FAILED: SemanticException [Error 10062]: Need to specify partition columns because the destination table is partitioned

字段缺少的情况下load data
测试数据b.txt
[root@nd2 wh]# cat b.txt
1,lucy,20
2,Marry,21
3,Tom,22

这里我们将a.txt文件内容导入到test_01
hive> load data local inpath '/usr/wh/b.txt' overwrite into table test_01; 
hive> select * from test_01;
1    lucy    20    NULL
2    Marry    21    NULL
3    Tom        22    NULL

字段过多的情况下load data
测试数据c.txt
[root@nd2 wh]# cat c.txt 
1,lucy,20,90,aa
2,Marry,21,95,bb
3,Tom,22,100,cc


这里我们将c.txt文件内容导入到test_01
hive> load data local inpath '/usr/wh/c.txt' overwrite into table test_01;
hive> select * from test_01;
1    lucy    20    90.0
2    Marry    21    95.0
3    Tom        22    100.0


字段类型不一致的情况下load data
测试数据d.txt
[root@nd2 wh]# cat d.txt 
1,lucy,aa,bb
2,Marry,cc,dd
3,Tom,xx,yy


这里我们将d.txt文件内容导入到test_01
hive> load data local inpath '/usr/wh/d.txt' overwrite into table test_01;
hive> select * from test_01;
1    lucy    NULL    NULL
2    Marry    NULL    NULL
3    Tom        NULL    NULL
 

Logo

更多推荐