基于UGI鑒權(quán)的Hive Metastore Client 并發(fā)訪問方式

定義一個基類:

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);
    }
    // 下面兩種常規(guī)構(gòu)建MS Client的方式, 不支持并發(fā)
    //return new HiveMetaStoreClient(hiveConf);
    //return RetryingMetaStoreClient.getProxy(hiveConf);
    try {
      // 通過下面構(gòu)建支持并發(fā),該并發(fā)訪問patch自HIVE-10956引入
      return Hive.get(hiveConf).getMSC();
    } catch (HiveException e) {
      e.printStackTrace();
      return null;
    } 
  }
}

定義一個并發(fā)測試:

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鑒權(quán),可以忽略或去除UGI鑒權(quán)部分.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • hive.ddl.output.format:hive的ddl語句的輸出格式,默認是text,純文本,還有json...
    博弈史密斯閱讀 2,094評論 0 6
  • “不可奪者,祐然之節(jié);莫之致者,自然之名?!北绕鹩迷娙恕⑺枷爰业葋硇稳萏K東坡,我更愿意用“天才”二字來形容。天才與...
    漱墨亭主人閱讀 1,759評論 0 5
  • 古詩飄香溢校園, 滿園花朵綻笑顏。 朗讀背涌李杜篇, 古體文化代代傳。
    旖旎i閱讀 349評論 3 5
  • 【原詩】 登樂游原 唐 - 李商隱 向晚意不適,驅(qū)車登古原。 夕陽無限好,只是近黃昏。 【賞析概要】 1.思想內(nèi)容...
    狀元大人閱讀 880評論 0 1
  • Rust 是一個由Mozilla主導開發(fā)的通用編譯型編譯語言。它的設計準則為"安全,并發(fā),實用",支持函數(shù)式,并發(fā)...
    0xSen閱讀 25,611評論 2 12

友情鏈接更多精彩內(nèi)容