Hive的安装配置和使用Java连接

Hadoop集群搭建可参考:http://blog.csdn.net/yanhang0610/article/details/51896545

1        系统环境

系统:CentOS 7.0。

版本:JDK 1.8.0_91,Hadoop 2.7.3,Hive 2.1.1。

2        Hive安装

官网:http://hive.apache.org/

安装Hive前先安装Hadoop。

Hive是单节点Server,不是分布式系统。

2.1 下载

    下载合适版本的Hive:

    http://mirrors.hust.edu.cn/apache/hive

2.2 安装

    解压即完成安装:

    # tar -xvf hive-x.y.z.tar.gz

    解压后可移动至合适位置。

2.3 设置环境变量

# vi /etc/profile.d/hive.sh

内容:

export HIVE_HOME=hive目录

export PATH=$HIVE_HOME/bin:$PATH

使配置生效

    # . /etc/profile

2.4 创建HDFS目录

# $HADOOP_HOME/bin/hadoop fs -mkdir       /tmp
# $HADOOP_HOME/bin/hadoop fs -mkdir       /user/hive/warehouse
# $HADOOP_HOME/bin/hadoop fs -chmod g+w   /tmp
# $HADOOP_HOME/bin/hadoop fs -chmod g+w   /user/hive/warehouse

“/user/hive/warehouse”为配置项“hive.metastore.warehouse.dir”的默认值。

2.5 配置

复制$HIVE_HOME/conf/hive-default.xml.template为hive-site.xml(不是hive-default.xml)。

2.5.1   元数据库MetaStore配置

拷贝java的mysql连接驱动jar包(如mysql-connector-java-5.1.8-bin.jar)到$HIVE_HOME/lib下。

    配置项参考:http://blog.csdn.net/aaa1117a8w5s6d/article/details/16884401

修改hive-site.xml相关配置项如下:

<property>
 <name>javax.jdo.option.ConnectionURL</name>
 <value>jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true</value>
</property>
<property>
 <name>javax.jdo.option.ConnectionDriverName</name>
 <value>com.mysql.jdbc.Driver</value>
</property>
<property>
 <name>javax.jdo.option.ConnectionUserName</name>
 <value>hive</value>
</property>
<property>
 <name>javax.jdo.option.ConnectionPassword</name>
  <value>hive</value>
</property>

将mysql作为hive的元数据库:

# schematool -dbType mysql -initSchema

2.5.2   设置java.io.tmpdir

在合适位置建议一个文件夹,做为hive的java.io.tmpdir值,如$HIVE_HOME/iotmp目录。

修改hive-site.xml配置文件,将所有的${system:java.io.tmpdir}/${system:user.name}改为上面建立的iotmp绝对路径。

3        运行

Hive日志文件默认存储在/tmp/{user.name}目录下。

3.1 运行Hive CLI

命令行输入:

# hive

正常的话将成功启动,进入hive提示符。

4        Java连接Hive

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

所需jar包(注意,有一个是hadoop的包):

    $HADOOP_HOME/share/hadoop/common/hadoop-common-2.7.3.jar
    $HIVE_HOME/lib/libthrift-0.10.0.jar
    $HIVE_HOME/lib/slf4j-log4j12-1.7.5.jar
    $HIVE_HOME/lib/slf4j-api-1.7.5.jar
    $HIVE_HOME/lib/log4j-1.2.15.jar
    $HIVE_HOME/lib/hive-exec-2.1.1.jar
    $HIVE_HOME/lib/hive-jdbc-2.1.1.jar
    $HIVE_HOME/lib/hive-metastore-2.1.1.jar
    $HIVE_HOME/lib/hive-service-2.1.1.jar
    $HIVE_HOME/lib/libfb303-0.9.3.jar
    $HIVE_HOME/lib/commons-logging-1.2.jar
    $HIVE_HOME/lib/httpclient-4.4.jar
    $HIVE_HOME/lib/httpcore-4.4.jar

4.1 Hadoop配置(集群各服务器需关闭防火墙)

参考:http://blog.csdn.net/fz1989/article/details/51489498

https://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/Superusers.html

使用Java远程连接HiveServer时,会出现

java.lang.RuntimeException:org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.authorize.AuthorizationException):Userroot is not allowed to impersonateanonymous 错误,此时应修改hadoop 配置文件:etc/hadoop/core-site.xml,加入如下配置项:

<property>
   <name>hadoop.proxyuser.root.hosts</name>
   <value>*</value>
</property>
<property>
   <name>hadoop.proxyuser.root.groups</name>
   <value>*</value>
</property>

其中红色部分值(User [用户名] is not allowed to …)应一致。

4.2 为Hive连接用户设置密码

可选,建议设置密码。若设置密码验证,则jdbc连接hiveserver2时需要填写正确密码才能连接成功。Hive一共提供了三种安全认证方式,我们通常采用的为第三种自定义的方式。

4.2.1   密码验证接口实现代码

自定义密码验证需要实现一个接口:org.apache.hive.service.auth.PasswdAuthenticationProvider,一个实现示例如下:

package hive.demo;
 
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
 
import javax.security.sasl.AuthenticationException;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configurable;
import org.apache.hadoop.conf.Configuration;
importorg.apache.hive.service.auth.PasswdAuthenticationProvider;
 
public class MyHiveServerPasswdAuthenticator implementsPasswdAuthenticationProvider, Configurable {
    private static final Log LOG = LogFactory.getLog(MyHiveServerPasswdAuthenticator.class);
    private Configurationconf =null;
    private static final String HIVE_SERVER2_COUSTOM_AUTH_JDBC_PASSWD_PREFIX ="hive.server2.custom_auth.jdbc_passwd.%s";
 
    @Override
    public void Authenticate(String userName, String passwd) throwsAuthenticationException {
       LOG.info("User:" + userName +" try to login.");
 
       String passwdMD5 = getConf().get(String.format(HIVE_SERVER2_COUSTOM_AUTH_JDBC_PASSWD_PREFIX, userName));
 
       if (passwdMD5 ==null) {
           String message = "User'sJDBC password configration is not found. User:" + userName;
           LOG.info(message);
           throw new AuthenticationException(message);
       }
 
       String md5 = MD5.md5(passwd);
 
       if (!md5.equalsIgnoreCase(passwdMD5)) {
           String message = "Username and password is mismatch. User:" + userName;
           throw new AuthenticationException(message);
       }
 
       LOG.info("User " + userName +"login successfully.");
    }
 
    @Override
    public Configuration getConf() {
       if (conf ==null) {
           this.conf =new Configuration();
       }
 
       returnconf;
    }
 
    @Override
    public void setConf(Configuration arg0) {
       this.conf = arg0;
    }
 
    // MD5加密工具类
    static class MD5 {
       private static MessageDigest digest;
       private static char hexDigits[] = {'0','1','2', '3', '4', '5','6','7', '8', '9', 'a','b','c', 'd', 'e', 'f'};
 
       static {
           try {
              digest = MessageDigest.getInstance("MD5");
           } catch (NoSuchAlgorithmException e) {
              throw new RuntimeException(e);
           }
       }
 
       public static String md5(String str) {
           byte[] btInput = str.getBytes();
           digest.reset();
           digest.update(btInput);
           byte[] md =digest.digest();
           // 把密文转换成十六进制的字符串形式
           int j = md.length;
           char strChar[] =newchar[j * 2];
           int k = 0;
           for (int i = 0; i < j; i++) {
              byte byte0 = md[i];
              strChar[k++] = hexDigits[byte0 >>> 4 &0xf];
              strChar[k++] = hexDigits[byte0 & 0xf];
           }
          
           return new String(strChar);
       }
    }
   
}

将以上代码导出jar包后上传到$HIVE_HOME/lib下。

4.2.2   修改配置

修改配置文件hive-site.xml,启用密码验证:

<property>
 <name>hive.server2.authentication</name>
 <value>CUSTOM</value>
</property>
<property>
 <name>hive.server2.custom.authentication.class</name>
 <value>hive.demo.MyHiveServerPasswdAuthenticator</value>
</property>

再添加密码配置到hive-site.xml中:

<property>
  <name>hive.server2.custom_auth.jdbc_passwd.root</name>
  <value>e99a18c428cb38d5f260853678922e03</value>
  <description/>
</property>

红色部分为登录用户名,value为密码,可以添加多组用户密码配置。

4.3 启动HiveServer2

启动HiveServer或HiveServer2可使得Java、Python连接Hive服务器提交请求并获得返回值。HiveServer只支持单客户端连接,HiveServer2支持多客户端连接。

先启动metastore:

# hive --service metastore &

再启动hiveserver2:

# hive --service hiveserver2 [hive端口] &

HiveServer默认端口为10000。

4.4 示例连接代码

package hive.demo;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
 
public class HiveDemo {
 
    public static void main(String[] args) {
       Connection connection = null;
       PreparedStatement pstmt = null;
       ResultSet rs = null;
 
       try {
           // 加载驱动
           Class.forName("org.apache.hive.jdbc.HiveDriver");
 
           // 建立连接,其中user和password为具有hdfs读写权限的linux账号密码,否则会报Permission denied错误
           connection = DriverManager.getConnection("jdbc:hive2://192.168.1.210:10000/default","root","abc123");
 
           // 建表
           pstmt = connection.prepareStatement(" create table hive_test(id int, name string) rowformat delimited fields terminated by '\t' ");
           pstmt.execute();
 
           // 打印表结构
           pstmt = connection.prepareStatement(" describe hive_test ");
           rs = pstmt.executeQuery();
           while (rs.next()) {
              System.out.println(rs.getString(1) +"\t" + rs.getString(2));
           }
          
           // 导入数据
           // pstmt =connection.prepareStatement(" load data localinpath '/root/bigdata/apache-hive-2.1.1-bin/testData.txt'into table hive_test ");  //加载本地数据(位于linux集群服务器本地)
           pstmt = connection.prepareStatement(" load data inpath '/hive/testData2.txt' into tablehive_test "); //加载HDFS上的数据
           pstmt.execute();
          
           // 查询
           pstmt = connection.prepareStatement(" select * from hive_test ");
           rs = pstmt.executeQuery();
           while (rs.next()) {
              System.out.println(rs.getString(1) +"\t" + rs.getString(2));
           }
          
           // 统计
           pstmt = connection.prepareStatement(" select distinct(name) from hive_test ");
           rs = pstmt.executeQuery();
           while (rs.next()) {
              System.out.println(rs.getString(1));
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
    }
 
}

测试数据:

testData.txt

111     str111

222     str222

333     str333

444     ddd

555     yyy

testData2.txt

2111    str1112

2222    str222

2333    str3332

2444    ddd

2555    yyy2

 

运行结果(distinct统计结果):

ddd

str111

str1112

str222

str333

str3332

yyy

yyy2

5        常见问题

参考:http://blog.csdn.net/freedomboy319/article/details/44828337

    http://www.cnblogs.com/junle/p/6347637.html

    http://www.jianshu.com/p/d9cb284f842d



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值