做艺术品的网站/免费友链互换
使用sqoop将mysql数据导入至hive的本质,其实是数据先导入至hdfs,然后会有一个脚本去执行将hdfs的数据load至hive中。所以我们将数据导入到hive时,其实hdfs中也会有这个数据。
使用sqoop将数据导入至hive时,需要用到一个hive-common-xxx.jar下面的一个类,所以我们需要先将这个jar包上传至sqoop的lib目录下:
- 添加jar
[root@hadoop01 ~]# cp $HIVE_HOME/lib/hive-common-2.3.9.jar $SQOOP_HOME/lib
- 启动hive
[root@hadoop02 ~]# hive --service metastore &
- 导入数据
sqoop import --connect jdbc:mysql://hadoop03:3306/test_sqoop --username root --password 123456 --table emp --hive-import --hive-overwrite --fields-terminated-by ',' --hive-table 'test_data.emp';#导入数据时没有指定在hive中表的名字,那么会直接使用原来在mysql中的表的名字,所以这里数据导入到hive时表名还为emp
#--hive-table 'test_data.emp'指定hive中的库ming.表明,如果不指定,默认导入至default库中
- 在hive客户端查看数据
hive> use test_data;
OK
Time taken: 0.105 seconds
hive> show tables;
OK
dynamic_partition1
emp
grade
map_grade
mixed_partition
partition1
partition2
test_regex
tmp_dynamic_partition1
tmp_mixed_partition
Time taken: 0.038 seconds, Fetched: 10 row(s)
hive> select * from emp;
OK
2201 Bob salesman 2205 2018-10-14 660.0 200.0 10
2202 Tina manager 2206 2013-09-10 3100.0 400.0 20
2203 Alice president NULL 2004-05-25 6000.0 1500.0 10
2204 Tony salesman 2202 2019-07-06 1200.0 300.0 20
2205 Tom manager 2203 2012-02-18 4200.0 900.0 10
2206 Alex president NULL 2006-01-28 5400.0 1400.0 10
Time taken: 0.197 seconds, Fetched: 6 row(s)
- 查看hdfs中的数据
[root@hadoop01 ~]# hdfs dfs -cat /user/hive/warehouse/test_data.db/emp/*;
- 将数据导入至hive的指定分区
sqoop import --connect jdbc:mysql://hadoop03:3306/test_sqoop --username root --password 123456 --table emp --hive-import --hive-overwrite --fields-terminated-by ',' --hive-table 'test_data.emp_partition' --hive-partition-key 'test_date' --hive-partition-value '2022-04-11' -m 1;#--hive-partition-key指定分区字段
#--hive-partition-value指定分区的至
#-m 1指定启动一个map进程
- 查看数据
hive中的数据最后一列加上了分区字段:
HDFS中也建好了分区目录:
查看HDFS中的数据:
[root@hadoop01 ~]# hdfs dfs -cat /user/hive/warehouse/test_data.db/emp_partition/test_date=2022-04-11/*;
以上就是使用sqoop实现mysql数据导入至hive的简单案例。