Hive的jdbc/thrift方法连接

1、引入pom文件



  4.0.0

  com.zuk
  szu-hive
  0.0.1-SNAPSHOT
  
  
		UTF-8
		2.7.3
		2.1.1
  

  
	
		junit
		junit
		3.8.1
		test
	
	
	
		org.apache.hive
		hive-jdbc
		2.1.1
	
	
	
        jdk.tools
        jdk.tools
        1.8
        system
        ${JAVA_HOME}/lib/tools.jar
    
	
 
	

2. java代码

package com.szu.hive;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class HiveTest {
	private static String driverName = "org.apache.hive.jdbc.HiveDriver";

    /**
     * @param args
     * @throws SQLException
     */
    public static void main(String[] args) throws SQLException {
        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }
        Connection con = DriverManager.getConnection("jdbc:hive2://192.168.133.139:10000/mydb1");
        Statement stmt = con.createStatement();
        String sql = "select * from userinfo";
        System.out.println("Running: " + sql);
        ResultSet res = stmt.executeQuery(sql);
        if (res.next()) {
            System.out.println(res.getString(1) + "," + res.getInt(2));
        }

    }
}

如果出现用户权限问题:【通过以下方式处理】

[bug] Hive:User root is not allowed to impersonate anonymous
修改hadoop 配置文件 etc/hadoop/core-site.xml,加入如下配置项:


  hadoop.proxyuser.root.hosts
  *



  hadoop.proxyuser.root.groups
  *

 

你可能感兴趣的:(hive)