Hive(18):java jdbc client连接hiveserver2

1.官网地址

https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients#HiveServer2Clients-JDBC

2.代码

(1)四要素

driver:org.apache.hive.jdbc.HiveDriver

url:jdbc:hive2://bigdata.ibeifeng.com:10000/default

username:hadoop

password:hadoop

(2)代码

package com.ibeifeng.hive.jdbc;

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
 
public class HiveJdbcClient {
	//hiveserver2的驱动包
  private static final String DRIVERNAME= "org.apache.hive.jdbc.HiveDriver";
 
  /**
   * @param args
   * @throws SQLException
   */
  public static void main(String[] args) throws SQLException {
      //第1要素:driver
	  try {
      Class.forName(DRIVERNAME);
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      System.exit(1);
    }
	  //第2要素:url
	 String url="jdbc:hive2://bigdata.ibeifeng.com:10000/default";
	  //第3要素:user="root" ->hiveserver2不检查,所以随意!
	 String username="hadoop";
	 //第4要素:password ->hiveserver2不检查,所以随意!
    String password="hadoop";
	 //replace "hive" here with the name of the user the queries should run as
    Connection con = DriverManager.getConnection(url, username, "");
    Statement stmt = con.createStatement();
    String tableName = "emp";

    String sql = "select deptno,count(1) as count from emp group by deptno";
    System.out.println("Running: " + sql);
    ResultSet res = stmt.executeQuery(sql);
    while (res.next()) {
//      System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
    
    	System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getInt(2));
    }

  }
}

猜你喜欢

转载自blog.csdn.net/u010886217/article/details/83926355
今日推荐