java through JDBC database connection hive

(1) First, open the command box hadoop,

(2) Then cd ~; hive --service hiveserver2 &

Permission to open hive connection

 

 (3) Create a java project, create a lib folder in the project root directory, and mysql pack into two, then right - "build path -" add to library (because I've imported so the following this option is not Ituri)

Since the hive is associated with the installation of mysql, so these two packages must be imported.

 

(4) Right-click the project build path - "configue build path

 

(5) Select add external jars jar package lib find all the directories hadoop hive and all introduced, it hive lib root directory is the directory in hadoop share / hadoop / common / lib in

 

 (6) a new java class enter the following code

package hivetest;

import java.sql.*;
import java.sql.SQLException;

public class test {

	private static String driverName = "org.apache.hive.jdbc.HiveDriver";
	public static void main(String[] args) throws SQLException {
	    try {
	      Class.forName(driverName);
	    }catch (ClassNotFoundException e) {
	      // TODO Auto-generated catch block
	      e.printStackTrace();
	      System.exit(1);
	    }
	    Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "hadoop", "hadoop");//后两个参数是用户名密码
	    if(con==null)
	        System.out.println("连接失败");
	    else {
	    Statement stmt = con.createStatement();
	    String sql = "SELECT * FROM action limit 10";	   
	    System.out.println("Running: " + sql);
	    ResultSet res = stmt.executeQuery(sql);
	   int a=0;
	    while (res.next()) {
	      System.out.println(res.getString(1));
	    }
	    }
	  }


}

 

结果如下

 

Guess you like

Origin www.cnblogs.com/837634902why/p/11502800.html