gobblin-modules/gobblin-parquet-apache/src/main/java/org/apache/gobblin/converter/parquet/JsonElementConversionFactory.java [66:295]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class JsonElementConversionFactory {

  /**
   * Use to create a converter for a single field from a parquetSchema.
   *
   * @param schema
   * @param repeated - Is the {@link Type} repeated in the parent {@link Group}
   * @return
   */
  public static JsonElementConverter getConverter(JsonSchema schema, boolean repeated) {

    InputType fieldType = schema.getInputType();
    switch (fieldType) {
      case INT:
        return new IntConverter(schema, repeated);

      case LONG:
        return new LongConverter(schema, repeated);

      case FLOAT:
        return new FloatConverter(schema, repeated);

      case DOUBLE:
        return new DoubleConverter(schema, repeated);

      case BOOLEAN:
        return new BooleanConverter(schema, repeated);

      case STRING:
        return new StringConverter(schema, repeated);

      case ARRAY:
        return new ArrayConverter(schema);

      case ENUM:
        return new EnumConverter(schema);

      case RECORD:
        return new RecordConverter(schema);

      case MAP:
        return new MapConverter(schema);

      case DATE:
      case TIMESTAMP:
        return new StringConverter(schema, repeated);

      default:
        throw new UnsupportedOperationException(fieldType + " is unsupported");
    }
  }

  /**
   * Converts a JsonElement into a supported ParquetType
   * @author tilakpatidar
   *
   */
  public static abstract class JsonElementConverter {
    protected final JsonSchema jsonSchema;

    protected JsonElementConverter(JsonSchema schema) {
      this.jsonSchema = schema;
    }

    /**
     * Convert value to a parquet type and perform null check.
     * @param value
     * @return Parquet safe type
     */
    public Object convert(JsonElement value) {
      if (value.isJsonNull()) {
        if (this.jsonSchema.isNullable()) {
          return null;
        }
        throw new RuntimeException(
            "Field: " + this.jsonSchema.getColumnName() + " is not nullable and contains a null value");
      }
      return convertField(value);
    }

    /**
     * Returns a {@link Type} parquet schema
     * @return
     */
    abstract public Type schema();

    /**
     * Convert JsonElement to Parquet type
     * @param value
     * @return
     */
    abstract Object convertField(JsonElement value);
  }

  /**
   * Converts a {@link JsonSchema} to a {@link PrimitiveType}
   */
  public static abstract class PrimitiveConverter extends JsonElementConverter {
    protected final boolean repeated;
    private PrimitiveTypeName outputType;
    protected Type schema;

    /**
     * @param jsonSchema
     * @param repeated
     * @param outputType
     */
    public PrimitiveConverter(JsonSchema jsonSchema, boolean repeated, PrimitiveTypeName outputType) {
      super(jsonSchema);
      this.repeated = repeated;
      this.outputType = outputType;
      this.schema = buildSchema();
    }

    protected Type buildSchema() {
      return new PrimitiveType(this.repeated ? REPEATED : optionalOrRequired(this.jsonSchema), this.outputType,
          this.jsonSchema.getColumnName());
    }

    @Override
    public Type schema() {
      return this.schema;
    }
  }

  /**
   * Converts {@link JsonSchema} having collection of elements of {@link InputType} into a {@link GroupType}.
   */
  public static abstract class CollectionConverter extends JsonElementConverter {
    protected InputType elementType;
    protected JsonElementConverter elementConverter;
    protected Type schema;

    public CollectionConverter(JsonSchema collectionSchema, InputType elementType, boolean repeated) {
      super(collectionSchema);
      this.elementType = elementType;
      this.elementConverter = getConverter(getElementSchema(), repeated);
      this.schema = buildSchema();
    }

    @Override
    public Type schema() {
      return this.schema;
    }

    /**
     * Prepare a {@link JsonSchema} for the elements in a collection.
     * @return
     */
    abstract JsonSchema getElementSchema();

    abstract Type buildSchema();
  }

  public static class IntConverter extends PrimitiveConverter {

    public IntConverter(JsonSchema schema, boolean repeated) {
      super(schema, repeated, INT32);
    }

    @Override
    IntegerValue convertField(JsonElement value) {
      return new IntegerValue(value.getAsInt());
    }
  }

  public static class LongConverter extends PrimitiveConverter {

    public LongConverter(JsonSchema schema, boolean repeated) {
      super(schema, repeated, INT64);
    }

    @Override
    LongValue convertField(JsonElement value) {
      return new LongValue(value.getAsLong());
    }
  }

  public static class FloatConverter extends PrimitiveConverter {

    public FloatConverter(JsonSchema schema, boolean repeated) {
      super(schema, repeated, PrimitiveTypeName.FLOAT);
    }

    @Override
    FloatValue convertField(JsonElement value) {
      return new FloatValue(value.getAsFloat());
    }
  }

  public static class DoubleConverter extends PrimitiveConverter {

    public DoubleConverter(JsonSchema schema, boolean repeated) {
      super(schema, repeated, PrimitiveTypeName.DOUBLE);
    }

    @Override
    DoubleValue convertField(JsonElement value) {
      return new DoubleValue(value.getAsDouble());
    }
  }

  public static class BooleanConverter extends PrimitiveConverter {

    public BooleanConverter(JsonSchema schema, boolean repeated) {
      super(schema, repeated, PrimitiveTypeName.BOOLEAN);
    }

    @Override
    BooleanValue convertField(JsonElement value) {
      return new BooleanValue(value.getAsBoolean());
    }
  }

  public static class StringConverter extends PrimitiveConverter {

    public StringConverter(JsonSchema schema, boolean repeated) {
      super(schema, repeated, BINARY);
      this.schema = buildSchema();
    }

    @Override
    BinaryValue convertField(JsonElement value) {
      return new BinaryValue(Binary.fromString(value.getAsString()));
    }

    @Override
    protected Type buildSchema() {
      String columnName = this.jsonSchema.getColumnName();
      if (this.repeated) {
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



gobblin-modules/gobblin-parquet/src/main/java/org/apache/gobblin/converter/parquet/JsonElementConversionFactory.java [66:295]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class JsonElementConversionFactory {

  /**
   * Use to create a converter for a single field from a parquetSchema.
   *
   * @param schema
   * @param repeated - Is the {@link Type} repeated in the parent {@link Group}
   * @return
   */
  public static JsonElementConverter getConverter(JsonSchema schema, boolean repeated) {

    InputType fieldType = schema.getInputType();
    switch (fieldType) {
      case INT:
        return new IntConverter(schema, repeated);

      case LONG:
        return new LongConverter(schema, repeated);

      case FLOAT:
        return new FloatConverter(schema, repeated);

      case DOUBLE:
        return new DoubleConverter(schema, repeated);

      case BOOLEAN:
        return new BooleanConverter(schema, repeated);

      case STRING:
        return new StringConverter(schema, repeated);

      case ARRAY:
        return new ArrayConverter(schema);

      case ENUM:
        return new EnumConverter(schema);

      case RECORD:
        return new RecordConverter(schema);

      case MAP:
        return new MapConverter(schema);

      case DATE:
      case TIMESTAMP:
        return new StringConverter(schema, repeated);

      default:
        throw new UnsupportedOperationException(fieldType + " is unsupported");
    }
  }

  /**
   * Converts a JsonElement into a supported ParquetType
   * @author tilakpatidar
   *
   */
  public static abstract class JsonElementConverter {
    protected final JsonSchema jsonSchema;

    protected JsonElementConverter(JsonSchema schema) {
      this.jsonSchema = schema;
    }

    /**
     * Convert value to a parquet type and perform null check.
     * @param value
     * @return Parquet safe type
     */
    public Object convert(JsonElement value) {
      if (value.isJsonNull()) {
        if (this.jsonSchema.isNullable()) {
          return null;
        }
        throw new RuntimeException(
            "Field: " + this.jsonSchema.getColumnName() + " is not nullable and contains a null value");
      }
      return convertField(value);
    }

    /**
     * Returns a {@link Type} parquet schema
     * @return
     */
    abstract public Type schema();

    /**
     * Convert JsonElement to Parquet type
     * @param value
     * @return
     */
    abstract Object convertField(JsonElement value);
  }

  /**
   * Converts a {@link JsonSchema} to a {@link PrimitiveType}
   */
  public static abstract class PrimitiveConverter extends JsonElementConverter {
    protected final boolean repeated;
    private PrimitiveTypeName outputType;
    protected Type schema;

    /**
     * @param jsonSchema
     * @param repeated
     * @param outputType
     */
    public PrimitiveConverter(JsonSchema jsonSchema, boolean repeated, PrimitiveTypeName outputType) {
      super(jsonSchema);
      this.repeated = repeated;
      this.outputType = outputType;
      this.schema = buildSchema();
    }

    protected Type buildSchema() {
      return new PrimitiveType(this.repeated ? REPEATED : optionalOrRequired(this.jsonSchema), this.outputType,
          this.jsonSchema.getColumnName());
    }

    @Override
    public Type schema() {
      return this.schema;
    }
  }

  /**
   * Converts {@link JsonSchema} having collection of elements of {@link InputType} into a {@link GroupType}.
   */
  public static abstract class CollectionConverter extends JsonElementConverter {
    protected InputType elementType;
    protected JsonElementConverter elementConverter;
    protected Type schema;

    public CollectionConverter(JsonSchema collectionSchema, InputType elementType, boolean repeated) {
      super(collectionSchema);
      this.elementType = elementType;
      this.elementConverter = getConverter(getElementSchema(), repeated);
      this.schema = buildSchema();
    }

    @Override
    public Type schema() {
      return this.schema;
    }

    /**
     * Prepare a {@link JsonSchema} for the elements in a collection.
     * @return
     */
    abstract JsonSchema getElementSchema();

    abstract Type buildSchema();
  }

  public static class IntConverter extends PrimitiveConverter {

    public IntConverter(JsonSchema schema, boolean repeated) {
      super(schema, repeated, INT32);
    }

    @Override
    IntegerValue convertField(JsonElement value) {
      return new IntegerValue(value.getAsInt());
    }
  }

  public static class LongConverter extends PrimitiveConverter {

    public LongConverter(JsonSchema schema, boolean repeated) {
      super(schema, repeated, INT64);
    }

    @Override
    LongValue convertField(JsonElement value) {
      return new LongValue(value.getAsLong());
    }
  }

  public static class FloatConverter extends PrimitiveConverter {

    public FloatConverter(JsonSchema schema, boolean repeated) {
      super(schema, repeated, PrimitiveTypeName.FLOAT);
    }

    @Override
    FloatValue convertField(JsonElement value) {
      return new FloatValue(value.getAsFloat());
    }
  }

  public static class DoubleConverter extends PrimitiveConverter {

    public DoubleConverter(JsonSchema schema, boolean repeated) {
      super(schema, repeated, PrimitiveTypeName.DOUBLE);
    }

    @Override
    DoubleValue convertField(JsonElement value) {
      return new DoubleValue(value.getAsDouble());
    }
  }

  public static class BooleanConverter extends PrimitiveConverter {

    public BooleanConverter(JsonSchema schema, boolean repeated) {
      super(schema, repeated, PrimitiveTypeName.BOOLEAN);
    }

    @Override
    BooleanValue convertField(JsonElement value) {
      return new BooleanValue(value.getAsBoolean());
    }
  }

  public static class StringConverter extends PrimitiveConverter {

    public StringConverter(JsonSchema schema, boolean repeated) {
      super(schema, repeated, BINARY);
      this.schema = buildSchema();
    }

    @Override
    BinaryValue convertField(JsonElement value) {
      return new BinaryValue(Binary.fromString(value.getAsString()));
    }

    @Override
    protected Type buildSchema() {
      String columnName = this.jsonSchema.getColumnName();
      if (this.repeated) {
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



