基于UGI鉴权的Hive Metastore Client 并发访问方式

定义一个基类:

package org.jeff.r.tools;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.metastore.IMetaStoreClient;
import org.apache.hadoop.hive.metastore.RetryingMetaStoreClient;
import org.apache.hadoop.hive.metastore.api.MetaException;
import org.apache.hadoop.hive.ql.metadata.Hive;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.security.UserGroupInformation;

/**
 * @author Jeff.R 2020/1/1
 */
public class MSBase {
  public static String jdbcUri;
  public static String userName = System.getenv("HADOOP_USER_NAME");
  public static String password = System.getenv("HADOOP_USER_PASSWORD");
  public static String hints;
  public static UserGroupInformation ugi;

  public static UserGroupInformation getUgi() throws Exception {
    if(hints == null || hints.isEmpty()){
      hints = String.format("%s%s%s", userName, "##", userPassword);
    }
    if(ugi == null){
      ugi = UserGroupInformation.createRemoteUser(userName,
          password, hints);
    }
    return ugi;
  }

  public static IMetaStoreClient getMSClient() throws MetaException {
    if(jdbcUri == null || jdbcUri.isEmpty()){
      throw new MetaException("jdbcUri must not be empty...");
    }
    HiveConf hiveConf = new HiveConf();
    if (StringUtils.isNotBlank(jdbcUri)) {
      hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, jdbcUri);
    }
    // 下面两种常规构建MS Client的方式, 不支持并发
    //return new HiveMetaStoreClient(hiveConf);
    //return RetryingMetaStoreClient.getProxy(hiveConf);
    try {
      // 通过下面构建支持并发,该并发访问patch自HIVE-10956引入
      return Hive.get(hiveConf).getMSC();
    } catch (HiveException e) {
      e.printStackTrace();
      return null;
    } 
  }
}

定义一个并发测试:

package org.jeff.r.tools;

import org.apache.hadoop.hive.metastore.IMetaStoreClient;
import org.apache.thrift.TException;

import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Jeff.R 2020/1/1
 */
public class HIVE_10956_Test extends MSBase{
  private IMetaStoreClient metaStoreClient;

  public static void main(String[] args) throws Exception {
    if(args.length < 1){
      System.out.println("Usage: <jdbc_url> <thread_num>");
      System.exit(1);
    }
    jdbcUri = args[0];
    int thread_num = Integer.parseInt(args[1]);
    try {
      getUgi().doAs(new PrivilegedExceptionAction<Object>() {
        @Override public Object run() throws Exception {
          internal(thread_num);
          return null;
        }
      });
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void internal(int thread_num) throws Exception {
    HIVE_10956_Test test = new HIVE_10956_Test();
    test.metaStoreClient = getMSClient();

    List<Thread> threads = new ArrayList();
    for(int i = 0; i < thread_num; i++){
      threads.add(new MyThread("Thread-" + i, test.metaStoreClient));
    }
    threads.forEach(o -> o.start());
    threads.forEach(o-> {
      try {
        o.join();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    });
  }
}

class MyThread extends Thread {
  private String threadName;
  private IMetaStoreClient metaStoreClient;
  public MyThread(String threadName, IMetaStoreClient metaStoreClient){
    this.threadName = threadName;
    this.metaStoreClient = metaStoreClient;
  }

  @Override public void run() {
    try {
      System.out.println(threadName + " running...");
      metaStoreClient.getAllDatabases();
    } catch (TException e) {
      System.out.println(threadName + " throw Exception...");
      e.printStackTrace();
    }
  }
}

说明: 如果你的hive没有UGI鉴权,可以忽略或去除UGI鉴权部分.

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • hive.ddl.output.format:hive的ddl语句的输出格式,默认是text,纯文本,还有json...
    博弈史密斯阅读 6,062评论 0 6
  • “不可夺者,祐然之节;莫之致者,自然之名。”比起用诗人、思想家等来形容苏东坡,我更愿意用“天才”二字来形容。天才与...
    漱墨亭主人阅读 5,625评论 0 5
  • 古诗飘香溢校园, 满园花朵绽笑颜。 朗读背涌李杜篇, 古体文化代代传。
    旖旎i阅读 2,523评论 3 5
  • 【原诗】 登乐游原 唐 - 李商隐 向晚意不适,驱车登古原。 夕阳无限好,只是近黄昏。 【赏析概要】 1.思想内容...
    状元大人阅读 4,249评论 0 1
  • Rust 是一个由Mozilla主导开发的通用编译型编译语言。它的设计准则为"安全,并发,实用",支持函数式,并发...
    0xSen阅读 25,507评论 2 12

友情链接更多精彩内容