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 @   O_O似曾相识  阅读(371)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示