JDBC Connection Hive

Source: Internet
Author: User

    1. Start the service on hive

      Hive--service Hiveserver

      650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/3A/0D/wKiom1O7S-jCADlJAAEOpAeTovk287.jpg "title=" Hive123.jpg "alt=" Wkiom1o7s-jcadljaaeopaetovk287.jpg "/>

    2. Developing in Eclipse

      Import the required jar packages (I'm importing the jar packages required by UDF and JDBC to connect hive, which is basically the simplest)

      650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/3A/0D/wKioL1O7TBqgdcMAAAXp5Qnlzdk218.jpg "title=" Hive124.jpg "alt=" Wkiol1o7tbqgdcmaaaxp5qnlzdk218.jpg "/>


My Code, hive's syntax is not said, you can modify the example of SQL to do their own business. My hive does not have a username or password set. So

Connection con = new Hivejdbc (). getconnection ("Jdbc:hive://192.168.192.138:10000/default", "", "") ;

The latter two parameters I was set to empty


package com.hive.jdbc;import java.sql.connection;import java.sql.drivermanager;import  java.sql.resultset;import java.sql.sqlexception;import java.sql.statement;/** *  Connecting Hive  *  @author  liqi * */public class HiveJDBC {     public static final string drivername =  "Org.apache.hadoop.hive.jdbc.HiveDriver" ;        /**     *  Getting Connections        */    public connection getconnection (String url, String username,string password) {        try {             class.forname (DriverName);             Connection conn =  Drivermanager.getconnection (url, username, pAssword);            return conn;         } catch  (classnotfoundexception e)  {             e.printstacktrace ();         }catch  (sqlexception e)  {             e.printstacktrace ();        }         return null;    }         public static void main (string args[]) {         connection con = new hivejdbc (). getconnection (              "Jdbc:hive://192.168.192.138:10000/default",  ", " "             );        try {             Statement stmt =  Con.createstatement ();             string sql  =  "Show tables";                         ResultSet res =  Stmt.executequery (SQL);             while (Res.next ( ))  {                sql  =  "select * from "  + res.getstring (1);                 SYSTEM.OUT.PRINTLN ("Tables:"  +  Res.getstring (1));                                  resultset restable = stmt.executequery (SQL);                 while ( Restable.next ()) {                     system.out.println (restable.getstring (2));                 }             }        } catch  (sqlexception  e)  {            e.printstacktrace ();         }    }}


This article is from the "Stud Programmer's Counter attack" blog, make sure to keep this source http://cdelliqi.blog.51cto.com/9028667/1435695

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.