zoukankan      html  css  js  c++  java
  • java通过JDBC连接hive数据库

    (1)首先在命令框中打开hadoop,

    (2)然后 cd ~;hive --service hiveserver2 &

    打开hive连接的权限

     (3)新建一个java项目,在项目根目录下新建一个lib文件夹,并将mysql的两个包放入,然后右键——》build path——》add to library(由于我已经导入了所以下面的图里没这个选项)

    由于hive在安装时是关联着mysql的,所以这两个包必须导入。

    (4) 右击项目build path——》configue build path

    (5)选择 add external jars 找到hadoop和hive目录下lib中的所有jar包全部导入,hive是在根目录下的lib中hadoop是在share/hadoop/common/lib的目录下

     (6)新建一个java类输入以下代码

    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));
    	    }
    	    }
    	  }
    
    
    }
    

    结果如下

  • 相关阅读:
    python函数--isalpha()方法
    python函数--isdigit()方法
    python函数--isalnum()方法
    python函数--range()方法
    python函数--len()方法
    python函数--介绍
    Linux命令总结--awk命令
    Linux命令总结--pwd命令
    Linux命令总结--rm命令
    Linux命令总结--cp命令
  • 原文地址:https://www.cnblogs.com/837634902why/p/11502800.html
Copyright © 2011-2022 走看看