溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

為hive permanent function添加默認(rèn)database特性

發(fā)布時(shí)間:2020-07-06 07:06:07 來(lái)源:網(wǎng)絡(luò) 閱讀:2539 作者:麻吉magi 欄目:大數(shù)據(jù)

前言

hive 0.13開始增加了permanent function;允許用戶自定義的function無(wú)需往.hiverc文件中添加create temporary function,提高h(yuǎn)ive的啟動(dòng)時(shí)間(無(wú)需預(yù)先執(zhí)行創(chuàng)建臨時(shí)函數(shù)命令);并且可以將udf jar包放置于hdfs上,方便管理,無(wú)需向hive client端推送udf;但是permanent function有一個(gè)問(wèn)題,就是,需要在function名前添加database名稱,即[database].[function];如果不填寫database,這會(huì)取當(dāng)前database自動(dòng)補(bǔ)全function。

參照傳統(tǒng)的關(guān)系型數(shù)據(jù)庫(kù),一般存在默認(rèn)的schema,在搜索function時(shí),優(yōu)先搜索默認(rèn)的schema;如iopostgresql的pg_catalog等。因此想著為Hive添加default database這個(gè)特性。

設(shè)計(jì)

添加兩個(gè)參數(shù)

hive.function.default.function.enabled  默認(rèn)為:false,表示禁用此特性;設(shè)置為true,表示啟動(dòng)該特性;搜索函數(shù)時(shí),優(yōu)先查找默認(rèn)database。

hive.function.default.function 默認(rèn)為: default;當(dāng)hive.function.default.function.enabled=true時(shí)生效;默認(rèn)函數(shù)搜索路徑。

實(shí)現(xiàn)

需要添加這個(gè)功能,需要了解permanent function在什么時(shí)候會(huì)用當(dāng)前database,補(bǔ)全function。

FunctionRegistry.java

private static FunctionInfo getFunctionInfoFromMetastore(String functionName) {
    FunctionInfo ret = null;

    try {
      String dbName;
      String fName;
      if (FunctionUtils.isQualifiedFunctionName(functionName)) {
        String[] parts = FunctionUtils.splitQualifiedFunctionName(functionName);
        dbName = parts[0];
        fName = parts[1];
      } else {
        // otherwise, qualify using current db
        dbName = SessionState.get().getCurrentDatabase().toLowerCase();
        fName = functionName;
      }

      // Try looking up function in the metastore
      HiveConf conf = SessionState.get().getConf();
      Function func = Hive.get(conf).getFunction(dbName, fName);
      if (func != null) {
        // Found UDF in metastore - now add it to the function registry
        // At this point we should add any relevant jars that would be needed for the UDf.
        try {
          FunctionTask.addFunctionResources(func.getResourceUris());
        } catch (Exception e) {
          LOG.error("Unable to load resources for " + dbName + "." + fName + ":" + e.getMessage(), e);
          return null;
        }

        Class<?> udfClass = Class.forName(func.getClassName(), true, Utilities.getSessionSpecifiedClassLoader());
        if (registerTemporaryFunction(functionName, udfClass)) {
          ret = mFunctions.get(functionName);
        } else {
          LOG.error(func.getClassName() + " is not a valid UDF class and was not registered.");
        }
      }
    } catch (HiveException e) {
      if (!((e.getCause() != null) &amp;&amp; (e.getCause() instanceof MetaException)) &amp;&amp;
         (e.getCause().getCause() != null) &amp;&amp; (e.getCause().getCause() instanceof NoSuchObjectException))  {
         LOG.info("Unable to lookup UDF in metastore: " + e);
      }
    } catch (ClassNotFoundException e) {
      // Lookup of UDf class failed
      LOG.error("Unable to load UDF class: " + e);
    }

    return ret;
  }

 

  public static String getNormalizedFunctionName(String fn) {
    // Does the same thing as getFunctionInfo, except for getting the function info.
    fn = fn.toLowerCase();
    return (FunctionUtils.isQualifiedFunctionName(fn) || mFunctions.get(fn) != null) ? fn
        : FunctionUtils.qualifyFunctionName(
            fn, SessionState.get().getCurrentDatabase().toLowerCase());
  }

  private static <T extends CommonFunctionInfo> T getFunctionInfo(
      Map<String, T> mFunctions, String functionName) {
    functionName = functionName.toLowerCase();
    T functionInfo = null;
    if (FunctionUtils.isQualifiedFunctionName(functionName)) {
      functionInfo = getQualifiedFunctionInfo(mFunctions, functionName);
    } else {
      // First try without qualifiers - would resolve builtin/temp functions.
      // Otherwise try qualifying with current db name.
      functionInfo =  mFunctions.get(functionName);
      if (functionInfo == null &amp;&amp; !FunctionUtils.isQualifiedFunctionName(functionName)) {
        String qualifiedName = FunctionUtils.qualifyFunctionName(functionName,
            SessionState.get().getCurrentDatabase().toLowerCase());
        functionInfo = getQualifiedFunctionInfo(mFunctions, qualifiedName);
      }
    }
    return functionInfo;
  }

 

FunctionUtils.java

  public static String[] getQualifiedFunctionNameParts(String name) throws HiveException {
    if (isQualifiedFunctionName(name)) {
      return splitQualifiedFunctionName(name);
    }
    String dbName = SessionState.get().getCurrentDatabase();
    return new String[] { dbName, name };
  }

在這些代碼上添加一個(gè)判斷hive.function.default.function.enabled是否為true,如果為true,則將默認(rèn)dbName調(diào)整為hive.function.default.function。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI