利用Sqoop导出Hive分析数据到MySQL

 

一,将Hive表中数据,导入到MySQL

1.首先在Hive中,创建一张表,命名为lenum,用于存储统计结果。

 

create table lenum(  
  year string,  
  num int  
)  
row format delimited  
fields terminated by '\t'  
stored as textfile; 

 

2.Hive中,统计每年信件数量,并将结果临时存储在hive中的lenum表中。

 

insert into table lenum  
select  
 substr(shijian,0,4as dt,  
 count(1as num  
from govdata  
group by substr(shijian,0,4)  
order by num; 

 

3.新打开一个命令行终端,连接Mysql

 

CREATE DATABASE IF NOT EXISTS edu4out DEFAULT CHARSET utf8 COLLATE utf8_general_ci; 

 

4.再新开启一个终端模拟器,使用Sqoop命令将Hive中的lenum表导入到Mysqllenumsql中。

 

sqoop export \  
--connect jdbc:mysql://localhost:3306/edu4out?characterEncoding=UTF-8 \  
--username root \  
--password strongs \  
--table lenumsql \  
--export-dir /user/hive/warehouse/edu4.db/lenum/000000_0 \  
--input-fields-terminated-by '\t';

 

5.在执行导数据之前,可以进行一个测试,验证Sqoop是否可用

 

sqoop list-databases \  
--connect jdbc:mysql://localhost:3306/ \  
--username root \  
--password strongs;