十二、Hive之JavaAPI操作(Test Record)

以下仅为我在正式通过javaapi来使用hive前的实验记录

Hive-Hadoop-MySQL的安装,请阅读这里
hive-site.xml新添加(以下都是默认配置):


  hive.server2.thrift.port
  10000                              //HiveServer2远程连接端口


  hive.server2.thrift.bind.host
  
  //**.**.**.**                         //hive所在集群的IP地址

一、启动元数据库

启动元数据库,& 表示后台运行,执行该命令后命令行会卡住,回车一下就可以,没此符号通过ctrl+c来结束服务

hive --service metastore &

通过jps来发现多了一个进程RunJar(Hadoop伪分布式):
十二、Hive之JavaAPI操作(Test Record)_第1张图片
进入/tmp/ljj/hive.log(/tmp//hive.log)查看日志:
十二、Hive之JavaAPI操作(Test Record)_第2张图片
一切正常,说明metastore已经启动了,
接着同上方式启动hiveserver2:

hive --service hiveserver2 &

这是再jps一下就会存在两个名字一样的线程RunJar,只是名字一样而已
十二、Hive之JavaAPI操作(Test Record)_第3张图片
接着查看hive.log,结果报了个没发现class的异常,这个异常我暂时跳过,不影响,因为我根本没有安装tez这玩意,暂时也用不上,应该是某个默认设置导致的
十二、Hive之JavaAPI操作(Test Record)_第4张图片

二、beeline命令行客户端连接hive

通过上面命令将matestore和hiveservice2启动,启动成功会有两个相同的Runjar线程
1、通过Beeline连接hive

beeline -u jdbc:hive2://master:10000/hive1 -n ljj -p

解析:-u代表URL;
jdbc:hive2://hostname:10000/databaseName
-n代表hive服务器登录的的用户名,默认为root
-p密码,可以不用输入

2、若出现异常:
在这里插入图片描述

原因:User: ljj is not allowed to impersonate root (state=08S01,code=0)
解决:
在hadoop下的core-site.xml中添加:


    	hadoop.proxyuser.ljj.hosts
    	*


    	hadoop.proxyuser.ljj.groups
    	*

自己想要设置什么管理用户就在配置文件设置什么管理用户,栗子:hadoop.proxyuser.(自定义uname).host
3、解决异常后重登数据库(不用输入密码,默认为空,不作验证)

beeline -u jdbc:hive2://master:10000 -n ljj -p

十二、Hive之JavaAPI操作(Test Record)_第5张图片
十二、Hive之JavaAPI操作(Test Record)_第6张图片

可能抛出的某异常:通过Beeline缺省的方式登录会存在的问题,我也是后续才发现,缺省就是默认匿名用户,不用验证账号和密码,但是因为hadoop的安全机制就算hive不校验用户的合法性hadoop也会校验用户的合法性,需要我们在core-site.xml配置了白名单用户,因此在缺省的情况下登录时还是需要输入一个在core-site.xml中配置的用户好让hadoop让配置的用户通过。问题来了,即使是在core-site.xml配置了用户,但该用户在hive中也属于匿名用户,这样的登录看似没问题,通过Beeline连接到hive后,普通的查询操作时被允许的,当进行删除以及创建这些关键操作就不允许匿名用户了,会抛出相应的异常,因此core-site.xml和core-site.xml需要同步配置用户,密码可以设置也可以留空,NONE模式下设置密码没有意义

需要在hive-site.xml中修改的默认配置:


    hive.server2.authentication
    NONE
    
      Expects one of [nosasl, none, ldap, kerberos, pam, custom].
      Client authentication types.
        NONE: no authentication check
        LDAP: LDAP/AD based authentication
        KERBEROS: Kerberos/GSSAPI authentication
        CUSTOM: Custom authentication provider		自定义的账号密码无效,请不要使用
                (Use with property hive.server2.custom.authentication.class)
        PAM: Pluggable authentication module
        NOSASL:  Raw transport
    



    hive.cli.print.current.db
    true
    Whether to include the current database in the Hive prompt.



    hive.server2.thrift.client.user
    ljj		//原anonymous
    Username to use against thrift client



    hive.server2.thrift.client.password
    123456	//原anonymous
    Password to use against thrift client

三、Hive的Java API使用(window10的eclipse下)

我这里用的是主机名用的是CentOS7下的hostname:master,因此在window10下需要修改hosts文件,
在hosts添加(文件地址在C:\Windows\System32\drivers\etc),window10的新版可能不能直接获取管理员权限打开文件,导致保存会被refuse,你可以将hosts复制到桌面修改完后覆盖回去会提示是否获取管理员权限

serverip	master	
package com.ljj.hive;

import java.sql.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class HiveDAO {
	
	private static String driverName = "org.apache.hive.jdbc.HiveDriver";
	private static String url = "jdbc:hive2://master:10000/hive1";
	private static String userName = "ljj";
	private static String password = "";
	
	private static Connection conn = null;
	private static Statement  stmt = null;
	private static ResultSet rs = null;
	
	@Before
	public void init() throws ClassNotFoundException, SQLException {
		Class.forName(driverName);
		conn = DriverManager.getConnection(url, userName, password);
		stmt = conn.createStatement();
	}
	
	@Test
    public void showDatabases() throws SQLException {
        String sql = "show databases";
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1));
        }
    }
    @After
    public void destory() throws SQLException {
        if ( rs != null) {
            rs.close();
        }
        if (stmt != null) {
            stmt.close();
        }
        if (conn != null) {
            conn.close();
        }
    }
}

截图:
在这里插入图片描述
,至此,流程全部打通了,终于可以进入正式的开发过程了。
因为测试用的是org.junit,可能在测试时提示找不到class的异常,看是不是找不到这个东西:hamcrest-core-1.3.jar,看你自己的java是哪个版本的,java1.8就用这个,其它版本的下载多两个不同版本的hamcrest-core放到lib中测试一下能用就对了(没有maven和springboot的情况下)
maven下:


    org.hamcrest
    hamcrest-core
    1.3
    test

PS:Hive的深入配置和开发在后续会继续挖坑和填坑

你可能感兴趣的:(Java,Hadoop,hive)