美文网首页
Hive从入门到精通5:Hive数据导入

Hive从入门到精通5:Hive数据导入

作者: 金字塔下的小蜗牛 | 来源:发表于2020-04-02 10:51 被阅读0次

1.从localhost导入数据

使用load语句可以导入本地磁盘上的数据(.csv文件),Hive默认的分隔符是Tab键,需要在创建表的时候指定分隔符为逗号“,”。

[root@master ~]# cat /root/input/student.csv
1,Tom,23
2,Mary,24
3,Mike,22

[root@master ~]# hive
Logging initialized using configuration in jar:file:/root/trainings/apache-hive-1.2.2-bin/lib/hive-common-1.2.2.jar!/hive-log4j.properties
hive>

hive> create table student1(sid int,sname string,age int) row format delimited fields terminated by ',';
OK
Time taken: 0.18 seconds

hive> load data local inpath '/root/input/student.csv' into table student1;
Loading data to table default.student1
Table default.student1 stats: [numFiles=1, totalSize=29]
OK
Time taken: 0.505 seconds

hive> select * from student1;
OK
1 Tom 23
2 Mary 24
3 Mike 22
Time taken: 0.206 seconds, Fetched: 3 row(s)

2.从HDFS上导入数据

使用load语句也可以导入HDFS上的数据(.csv文件),也需要在创建表的时候指定分隔符为逗号“,”。

[root@master ~]# hdfs dfs -put /root/input/student.csv /input
[root@master ~]# hdfs dfs -cat /input/student.csv
1,Tom,23
2,Mary,24
3,Mike,22

[root@master ~]# hive
Logging initialized using configuration in jar:file:/root/trainings/apache-hive-1.2.2-bin/lib/hive-common-1.2.2.jar!/hive-log4j.properties
hive>

hive> create table student2(sid int,sname string,age int) row format delimited fields terminated by ',';
OK
Time taken: 1.728 seconds

hive> load data inpath '/input/student.csv' into table student2;
Loading data to table default.student2
Table default.student2 stats: [numFiles=1, totalSize=29]
OK
Time taken: 0.939 seconds

hive> select * from student2;
OK
1 Tom 23
2 Mary 24
3 Mike 22
Time taken: 0.688 seconds, Fetched: 3 row(s)

3.从RDBMS中导入数据

使用sqoop可以将RDBMS中的数据导入到Hive中,也可以将Hive中的数据导出到RDBMS中。

mysql> use test;
Database changed
mysql> select * from student;
+------+-------+------+
| sid | sname | age |
+------+-------+------+
| 1 | Tom | 23 |
| 2 | Mary | 24 |
| 3 | Mike | 22 |
+------+-------+------+
3 rows in set (0.00 sec)

(1)将RDBMS中的表结构复制到Hive中:

[root@master ~]# sqoop create-hive-table --connect jdbc:mysql://localhost:3306/test
--table tblUsers --username root --password 123456 --hive-table testHive

(2)从RDBMS中的表导入到Hive中:

[root@master ~]# sqoop import --connect jdbc:mysql://localhost:3306/test
--table tblUsers --username root --password 123456 --hive-import

(3)将Hive中的表导入到RDBMS中:

[root@master ~]# sqoop export --connect jdbc:mysql://localhost:3306/test
--table tblUsers --username root --password 123456 --export-dir /hive/warehouse/testHive

相关文章

网友评论

      本文标题:Hive从入门到精通5:Hive数据导入

      本文链接:https://www.haomeiwen.com/subject/cwrkdhtx.html