Use the udf function to insert Hive statistical results directly to MySQL.

Source: Internet
Author: User

Most of the steps to use hive for data analysis are to use hive to export the statistical results to a local file or other Hive tables, import the local file to mysql or use sqoop to import the Hive table to mysql.
 
Today, my colleague recommended a method to directly import the statistical results to mysql using udf functions.
 
Step:
 
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', '123', 'insert INTO dc (code, size) VALUES (?,?) ', Code, size) from accesslog limit 10;
 
Note: The result is the mysql database name, And the dc is the dc field in the brackets of the table name dc (code, size) in the database result. values (?,?) The code corresponding to the value following the hive statistical result. size is the field in the hive table, and accesslog represents the table name in hive.
 
After performing the preceding steps, you can import the hive statistical results to the mysql database.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.