1、hive表格式为TEXTFILE
由于 TEXTFILE 格式没有对数据进行压缩,因此直接 load 即可。
假设本地有一个文件 test_person.txt,内容为:
Gong 24
Tian 23
1
2
想要将其导入到 TEXTFILE 格式的 tmp_test_person_txt 表内,步骤如下:
1.1、创建目标表,格式为 TEXTFILE
在 hive 命令行下,输入建表语句:
CREATE TABLE tmp.tmp_test_person_txt(name string, age int)
row format delimited fields terminated by '\t'
STORED AS TEXTFILE
location '/user/recsys/srbi/tmp.db/tmp_test_person_txt';
1
2
3
4
1.2、加载本地文件
任选以下两种方法之一即可:
(1)在 linux 命令行下,将本地文件复制到 hive 表所在路径下:
$ hadoop fs -put ~/test_person.txt /user/recsys/srbi/tmp.db/tmp_test_person_txt
1
(2)在 hive 命令行下,将本地文件加载到 hive 表中:
hive> load data local inpath '~/test_person.txt' into table tmp.tmp_test_person_txt;
Loading data to table tmp.tmp_test_person_txt
Table tmp.tmp_test_person_txt stats: [numFiles=1, totalSize=16]
OK
Time taken: 1.536 seconds
1
2
3
4
5
1.3、导入成功
可以查看是否已经成功导入数据:
hive> select * from tmp.tmp_test_person_txt;
OK
Gong 24
Tian 23
Time taken: 0.544 seconds, Fetched: 2 row(s)
1
2
3
4
5
2、hive表格式为ORC
存储格式为 ORC 的 hive 表,不能直接 load 文件,因为 ORC 格式的数据是有压缩操作的,并不是常规的格式。
可以使用曲线救国方式,建立一个临时表,字段相同,但是存储格式是 TEXTFILE 的,这样就可以使用上面的方法加载本地文件了,然后使用 hive 表的操作命令 insert into table ... select ... 来实现两个表之间的数据复制。
操作如下:
2.1、创建两个表,格式为分别为 ORC 和 TEXTFILE
在 hive 命令行下,输入建表语句:
hive> CREATE TABLE tmp.tmp_test_person_txt(name string, age int)
> row format delimited fields terminated by '\t'
> STORED AS TEXTFILE
> location '/user/recsys/srbi/tmp.db/tmp_test_person_txt';
hive> CREATE TABLE tmp.tmp_test_person_orc(name string, age int)
> row format delimited fields terminated by '\t'
> STORED AS ORC
> location '/user/recsys/srbi/tmp.db/tmp_test_person_orc';
1
2
3
4
5
6
7
8
2.2、加载本地文件至 TEXTFILE
使用 1.2 节的方法,将本地文件导入到 TEXTFILE 格式的 tmp_test_person_txt 表中。
2.3、将 TEXTFILE 的数据复制到 ORC 中
在 hive 命令行下:
INSERT INTO TABLE tmp_test_person_orc SELECT * FROM tmp_test_person_txt;
1
2.4、导入成功
可以查看是否已经成功导入数据:
hive> select * from tmp.tmp_test_person_orc;
OK
Gong 24
Tian 23
Time taken: 0.544 seconds, Fetched: 2 row(s)
1
2
3
4
5
3、其他格式
其他存储格式如 SEQUENCEFILE、PARQUET 等,都可以按照 ORC 格式那样,先导入到 TEXTFILE 表,再进行两个表之间的数据转移即可。
————————————————
版权声明:本文为优快云博主「HappyRocking」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/HappyRocking/article/details/90178439