Home > Database > Mysql Tutorial > body text

利用udf函数将Hive统计结果直接插入到MySQL

WBOY
Release: 2016-06-07 17:28:16
Original
1230 people have browsed it

result为mysql数据库名,dc为数据库result中的表名 dc(code,size)括号中的字段为mysql表dc字段,values(?,?)对应hive统计结果的值

大部分利用hive做数据分析的步骤是先用hive将统计结果导出到本地文件或者Hive的其他表中,再将本地文件导入到mysql或者利用sqoop将Hive表导入到mysql中。
 
今天同事给推荐了一个利用udf函数直接将统计结果导入mysql的方法。
 
步骤为
 
hive>add jar /usr/lib/hive/lib/hive-contrib-0.9.0-cdh4.1.2.jar;
 
Added /usr/lib/hive/lib/hive-contrib-0.9.0-cdh4.1.2.jar to class path
 Added resource: /usr/lib/hive/lib/hive-contrib-0.9.0-cdh4.1.2.jar
 
hive>add jar /usr/share/java/mysql-connector-java-5.1.17.jar;
 
Added /usr/share/java/mysql-connector-java-5.1.17.jar to class path
 Added resource: /usr/share/java/mysql-connector-java-5.1.17.jar
 

hive>CREATE TEMPORARY FUNCTION dboutput AS 'org.apache.Hadoop.hive.contrib.genericudf.example.GenericUDFDBOutput';
 
hive>select dboutput('jdbc:mysql://localhost/result','root','123456','INSERT INTO dc(code,size) VALUES (?,?)',code,size) from accesslog limit 10;
 
注:result为mysql数据库名,dc为数据库result中的表名 dc(code,size)括号中的字段为mysql表dc字段,,values(?,?)对应hive统计结果的值 后面的code,size为hive表中的字段,accesslog表示hive中的表名称。
 
通过以上步骤即可将hive统计结果直接导入到mysql数据库中。

linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template