Javaapi operation of Hive __java

Source: Internet
Author: User
Tags odbc stmt
The Hive Data Warehouse based on Hadoop Javaapi simple Invocation instance, the brief introduction of hive. Hive provides three user interfaces: the CLI,JDBC/ODBC and WebUI CLI, the shell command line JDBC/ODBC is Hive Java, similar to the traditional database JDBC WebGui is accessed through the browser Hive

This article mainly introduces the second type of user interface, directly into the business.

1, Hive Installation:

1 Hive Installation Please refer to the relevant articles on the Web, test only in a Hadoop node installed hive can be.

2 test data file ' \ t ' delimited:

1 Zhangsan

2 Lisi

3 Wangwu

3 The test data will be uploaded to the Linux directory, I placed in:/home/hadoop01/data

2. When using JDBC to develop the Hive program, you must first turn on the Hive remote service interface. Open using the following command:

Java code hive--service hiveserver >/dev/null 2>/dev/null &

3. Test code:

  Java code    import java.sql.connection;   import java.sql.drivermanager;    import java.sql.resultset;   import java.sql.sqlexception;   import  java.sql.statement;      import org.apache.log4j.logger;     /**    * hive javaapi   *    *  start hive Remote Service interface command line execution: Hive --service  hiveserver >/dev/null 2>/dev/null &   *    * @ author  brother    *    */   public class hivejdbccli {           private static String driverName =  " Org.apache.hadoop.hive.jdbc.HiveDriver ";       private static string  url =  "Jdbc:hive://hadoop3:10000/default";       private  static string user =  "Hive";       private static string password  =  "MySQL";       private static String sql =  "";       private static ResultSet res;        private static final logger log = logger.getlogger (HiveJdbcCli.class );          public static void main (String[] args)  {           Connection conn = null;            Statement stmt = null;           try {                conn = getconn ();            &Nbsp;   stmt = conn.createstatement ();                   //  First step: The existence of the first delete                 string tablename = droptable (stmt);                  //  Step Two: Create   if not present               createtable (stmt, tableName );                  //  Step Three: View the tables created                showtables (stmt,  tablename);                   //  perform describe table operations                 desCribetables (stmt, tablename);                   //  perform load data into table operations                 loaddata (stmt, tablename);                   //  Executive  select * query   Operations                selectdata (stmt,  tablename);                   //  perform  regular hive query  statistic operations                 countdata (stmt, tablename);               } catch  (classnotfoundexception e)  {                 e.printstacktrace ();                log.error (drivername +  " not found!",  e);                system.exit (1);            } catch  (sqlexception e)  {                e.printstacktrace ();               log.error ("Connection error!",  e);               system.exit (1);            } finally {                try {                    if  (conn != null)  {                         Conn.close (); &

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.