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目录下命令不执行

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

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

 

posted on 2022-10-08 10:53  跨越&尘世  阅读(221)  评论(0编辑  收藏  举报