public static Map getMapAttribute()

in emr-dynamodb-hive/src/main/java/org/apache/hadoop/hive/dynamodb/util/DynamoDBDataParser.java [68:128]


  public static Map<String, AttributeValue> getMapAttribute(Object data,
      ObjectInspector objectInspector, boolean nullSerialization) {
    Map<String, AttributeValue> itemMap = new HashMap<>();
    switch (objectInspector.getCategory()) {
      case MAP:
        MapObjectInspector mapOI = (MapObjectInspector) objectInspector;
        Map<?, ?> dataMap = mapOI.getMap(data);

        if (dataMap == null) {
          return null;
        }

        StringObjectInspector mapKeyOI = (StringObjectInspector) mapOI.getMapKeyObjectInspector();
        ObjectInspector mapValueOI = mapOI.getMapValueObjectInspector();
        HiveDynamoDBType valueType = HiveDynamoDBTypeFactory.getTypeObjectFromHiveType(mapValueOI);

        // borrowed from HiveDynamoDBItemType
        for (Map.Entry<?, ?> entry : dataMap.entrySet()) {
          String attributeName = mapKeyOI.getPrimitiveJavaObject(entry.getKey());

          Object valueData = entry.getValue();
          AttributeValue attributeValue = valueData == null
              ? getNullAttribute(nullSerialization)
              : valueType.getDynamoDBData(valueData, mapValueOI, nullSerialization);

          if (attributeValue == null) {
            throw new NullPointerException("Null field found in map: " + dataMap);
          }

          itemMap.put(attributeName, attributeValue);
        }

        break;
      case STRUCT:
        StructObjectInspector structOI = (StructObjectInspector) objectInspector;
        List<? extends StructField> fields = structOI.getAllStructFieldRefs();

        for (StructField field : fields) {
          Object fieldData = structOI.getStructFieldData(data, field);
          ObjectInspector fieldOI = field.getFieldObjectInspector();
          HiveDynamoDBType fieldType = HiveDynamoDBTypeFactory.getTypeObjectFromHiveType(fieldOI);

          String attributeName = field.getFieldName();
          AttributeValue attributeValue = fieldData == null
              ? getNullAttribute(nullSerialization)
              : fieldType.getDynamoDBData(fieldData, fieldOI, nullSerialization);

          if (attributeValue == null) {
            throw new NullPointerException("Null field found in struct: "
                + structOI.getStructFieldsDataAsList(data));
          }

          itemMap.put(attributeName, attributeValue);
        }
        break;
      default:
        throw new IllegalArgumentException("Unknown object inspector type: "
            + objectInspector.getCategory() + " Type name: " + objectInspector.getTypeName());
    }
    return itemMap;
  }