• HIVE内置函数map_keys -- 源码解析


    HIVE内置函数map_keys


    所在的全类名

    org.apache.hadoop.hive.ql.udf.generic.GenericUDFMapKeys
    
    • 1

    一、initialize方法

    @Override
      public ObjectInspector initialize(ObjectInspector[] arguments)
      throws UDFArgumentException {
        // 传入的参数为1个
        if (arguments.length != 1) {
          throw new UDFArgumentLengthException("The function MAP_KEYS only accepts one argument.");
        } else if (!(arguments[0] instanceof MapObjectInspector)) {
          // 参数类型应该是MAP类型
          throw new UDFArgumentTypeException(0, "\""
              + Category.MAP.toString().toLowerCase()
              + "\" is expected at function MAP_KEYS, " + "but \""
              + arguments[0].getTypeName() + "\" is found");
        }
    		// 参数强转为MapObjectInspector
        mapOI = (MapObjectInspector) arguments[0];
        // 返回一个map的key检查器
        ObjectInspector mapKeyOI = mapOI.getMapKeyObjectInspector();
        // 返回一个列表
        return ObjectInspectorFactory.getStandardListObjectInspector(mapKeyOI);
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在上面的源码之后,需要知道返回的列表是怎么执行的,点进去ObjectInspectorFactory类看getStandardListObjectInspector方法

    public static StandardListObjectInspector getStandardListObjectInspector(ObjectInspector listElementObjectInspector) {
        StandardListObjectInspector result = (StandardListObjectInspector)cachedStandardListObjectInspector.get(listElementObjectInspector);
        if (result == null) {
            result = new StandardListObjectInspector(listElementObjectInspector);
            StandardListObjectInspector prev = (StandardListObjectInspector)cachedStandardListObjectInspector.putIfAbsent(listElementObjectInspector, result);
            if (prev != null) {
                result = prev;
            }
        }
    
        return result;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    其中cachedStandardListObjectInspector的声明如下

    static ConcurrentHashMap<ObjectInspector, StandardListObjectInspector> cachedStandardListObjectInspector = new ConcurrentHashMap();
    
    • 1

    二、evaluate方法

    @Override
      public Object evaluate(DeferredObject[] arguments) throws HiveException {
        retArray.clear();
        Object mapObj = arguments[0].get();
        Map<?,?> mapVal = mapOI.getMap(mapObj);
        if (mapVal != null) {
          retArray.addAll(mapVal.keySet());
        }
        return retArray;
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    getDisplayString方法

    @Override
      public String getDisplayString(String[] children) {
        assert children.length == 1;
        return getStandardDisplayString("map_keys", children);
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    getStandardDisplayString的方法:

    protected String getStandardDisplayString(String name, String[] children) {
        return getStandardDisplayString(name, children, ", ");
      }
    
    • 1
    • 2
    • 3

    调用了重载方法,一点点的把数组内的数据append到字符中

    protected String getStandardDisplayString(String name, String[] children, String delim) {
      StringBuilder sb = new StringBuilder();
      sb.append(name);
      sb.append("(");
      if (children.length > 0) {
        sb.append(children[0]);
        for (int i = 1; i < children.length; i++) {
          sb.append(delim);
          sb.append(children[i]);
        }
      }
      sb.append(")");
      return sb.toString();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    10.3使用Servlet写一个表白墙网站
    30天Python入门(第十二天:深入了解Python中的模块)
    快速搭建PHP管理后台
    如何在分面中添加数学表达式标签?
    【附源码】计算机毕业设计JAVA校园社团管理平台
    即时通讯WebSocket
    机器学习算法——K近邻算法详解
    RH850P1X芯片学习笔记-Generic Timer Module -ATOM
    使用route的reject拒绝境外ip通信
    基于卷积神经网络的图像识别-案例实施1
  • 原文地址:https://blog.csdn.net/weixin_46429290/article/details/125610603