hive导入mysql

hive测试——HIVE数据分析02

题目:

4、处理结果入库:(在虚拟机安装mysql)   

将上述统计分析的结果数据保存到mySQL数据库中。

 

复制代码
#text3_1入库

#1.添加驱动,在hive的lib目录下,如果有则不需要添加

add jar /export/server/hive/lib/hive-contrib-3.1.2.jar

add jar /export/server/hive/lib/mysql-connector-java-5.1.32.jar

#2.创建临时函数,需要在mysql提前创建好表


CREATE TEMPORARY FUNCTION dboutput AS 'org.apache.hadoop.hive.contrib.genericudf.example.GenericUDFDBOutput'


select dboutput('jdbc:mysql://node1:3306/hive3?useSSL=false','root','pwd','INSERT INTO text3_1(day_id, sale_nbr, cnt, round) VALUES (?, ?, ?, ?)',day_id, sale_nbr, cnt, round) from text3_1

 
复制代码

 

 

#text3_2入库
CREATE TEMPORARY FUNCTION dboutput AS 'org.apache.hadoop.hive.contrib.genericudf.example.GenericUDFDBOutput' select dboutput('jdbc:mysql://node1:3306/hive3?useSSL=false','root','pwd','INSERT INTO text3_2(day_id, sale_nbr, cnt, round) VALUES (?, ?, ?, ?)',day_id, sale_nbr, cnt, round) from text3_2

 

 

复制代码
#text3_3入库

CREATE TEMPORARY FUNCTION dboutput AS 'org.apache.hadoop.hive.contrib.genericudf.example.GenericUDFDBOutput'

 

select dboutput('jdbc:mysql://node1:3306/hive3?useSSL=false','root','pwd','INSERT INTO text3_3(day_id, sale_nbr, cnt, round) VALUES (?, ?, ?, ?)',day_id, sale_nbr, cnt, round) from text3_3

复制代码

 

#profit入库
CREATE TEMPORARY FUNCTION dboutput AS 'org.apache.hadoop.hive.contrib.genericudf.example.GenericUDFDBOutput'

select dboutput('jdbc:mysql://node1:3306/hive3?useSSL=false','root','pwd','INSERT INTO profit(day_id,sale_nbr,incnt,inround,outcnt,outround,li) VALUES (?, ?, ?, ?, ?, ?, ?)',day_id, sale_nbr,incnt,inround,outcnt,outround,li) from profit

 profit表入库使用上述方法会很慢,毕竟4900000多条数据,

下面是第二张方法,先导出为.txt文件,在使用mysql可视化工具导入

#导出为.txt,在hive的bin目录下

hive -e "select * from dbtest.profit" >> /export/software/profit.txt;

然后将虚拟机的文件传到电脑桌面,直接使用navicat导入,速度非常快。

posted @ 2022-10-09 09:42  O_O似曾相识  阅读(367)  评论(0编辑  收藏  举报