Java Code Examples for org.apache.avro.Schema#getObjectProp()

The following examples show how to use org.apache.avro.Schema#getObjectProp() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: iceberg   File: AvroSchemaUtil.java    License: Apache License 2.0 6 votes vote down vote up
private static Integer getId(Schema schema, String propertyName, NameMapping nameMapping, List<String> names) {
  if (schema.getType() == UNION) {
    return getId(fromOption(schema), propertyName, nameMapping, names);
  }

  Object id = schema.getObjectProp(propertyName);
  if (id != null) {
    return toInt(id);
  } else if (nameMapping != null) {
    MappedField mappedField = nameMapping.find(names);
    if (mappedField != null) {
      return mappedField.id();
    }
  }

  return null;
}
 
Example 2
private Optional<Map> getProperties(Schema schema) {
  Object propertiesProp = schema.getObjectProp(ARG_PROPERTIES_PROP);
  if (propertiesProp == null) {
    return Optional.empty();
  } else if (propertiesProp instanceof Map) {
    return Optional.of((Map) propertiesProp);
  } else {
    throw new RuntimeException(String.format(
        "%s property must be given as object, was %s instead",
        ARG_PROPERTIES_PROP,
        propertiesProp.getClass().getName()
    ));
  }
}
 
Example 3
Source Project: iceberg   File: AvroSchemaUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean isTimestamptz(Schema schema) {
  LogicalType logicalType = schema.getLogicalType();
  if (logicalType != null && logicalType instanceof LogicalTypes.TimestampMicros) {
    // timestamptz is adjusted to UTC
    Object value = schema.getObjectProp(ADJUST_TO_UTC_PROP);
    if (value instanceof Boolean) {
      return (Boolean) value;
    } else if (value instanceof String) {
      return Boolean.parseBoolean((String) value);
    }
  }

  return false;
}
 
Example 4
Source Project: iceberg   File: SchemaToType.java    License: Apache License 2.0 5 votes vote down vote up
private int getElementId(Schema schema) {
  if (schema.getObjectProp(AvroSchemaUtil.ELEMENT_ID_PROP) != null) {
    return AvroSchemaUtil.getElementId(schema);
  } else {
    return allocateId();
  }
}
 
Example 5
Source Project: iceberg   File: SchemaToType.java    License: Apache License 2.0 5 votes vote down vote up
private int getKeyId(Schema schema) {
  if (schema.getObjectProp(AvroSchemaUtil.KEY_ID_PROP) != null) {
    return AvroSchemaUtil.getKeyId(schema);
  } else {
    return allocateId();
  }
}
 
Example 6
Source Project: iceberg   File: SchemaToType.java    License: Apache License 2.0 5 votes vote down vote up
private int getValueId(Schema schema) {
  if (schema.getObjectProp(AvroSchemaUtil.VALUE_ID_PROP) != null) {
    return AvroSchemaUtil.getValueId(schema);
  } else {
    return allocateId();
  }
}
 
Example 7
Source Project: iceberg   File: AvroSchemaUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean isTimestamptz(Schema schema) {
  LogicalType logicalType = schema.getLogicalType();
  if (logicalType != null && logicalType instanceof LogicalTypes.TimestampMicros) {
    // timestamptz is adjusted to UTC
    Object value = schema.getObjectProp(ADJUST_TO_UTC_PROP);
    if (value instanceof Boolean) {
      return (Boolean) value;
    } else if (value instanceof String) {
      return Boolean.parseBoolean((String) value);
    }
  }

  return false;
}
 
Example 8
Source Project: iceberg   File: AvroSchemaUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static int getId(Schema schema, String propertyName) {
  if (schema.getType() == UNION) {
    return getId(fromOption(schema), propertyName);
  }

  Object id = schema.getObjectProp(propertyName);
  Preconditions.checkNotNull(id, "Missing expected '%s' property", propertyName);

  return toInt(id);
}
 
Example 9
Source Project: iceberg   File: SchemaToType.java    License: Apache License 2.0 5 votes vote down vote up
private int getElementId(Schema schema) {
  if (schema.getObjectProp(AvroSchemaUtil.ELEMENT_ID_PROP) != null) {
    return AvroSchemaUtil.getElementId(schema);
  } else {
    return allocateId();
  }
}
 
Example 10
Source Project: iceberg   File: SchemaToType.java    License: Apache License 2.0 5 votes vote down vote up
private int getKeyId(Schema schema) {
  if (schema.getObjectProp(AvroSchemaUtil.KEY_ID_PROP) != null) {
    return AvroSchemaUtil.getKeyId(schema);
  } else {
    return allocateId();
  }
}
 
Example 11
Source Project: iceberg   File: SchemaToType.java    License: Apache License 2.0 5 votes vote down vote up
private int getValueId(Schema schema) {
  if (schema.getObjectProp(AvroSchemaUtil.VALUE_ID_PROP) != null) {
    return AvroSchemaUtil.getValueId(schema);
  } else {
    return allocateId();
  }
}
 
Example 12
private Optional<Map> getProperties(Schema schema) {
  Object propertiesProp = schema.getObjectProp(ARG_PROPERTIES_PROP);
  if (propertiesProp == null) {
    return Optional.empty();
  } else if (propertiesProp instanceof Map) {
    return Optional.of((Map) propertiesProp);
  } else {
    throw new RuntimeException(String.format(
        "%s property must be given as object, was %s instead",
        ARG_PROPERTIES_PROP,
        propertiesProp.getClass().getName()
    ));
  }
}
 
Example 13
private Object convertToPentahoType( int pentahoType, ByteBuffer avroData, Schema field ) {
  Object pentahoData = null;
  if ( avroData != null ) {
    try {
      switch ( pentahoType ) {
        case ValueMetaInterface.TYPE_BIGNUMBER:
          Conversions.DecimalConversion converter = new Conversions.DecimalConversion();
          Schema schema = field;
          if ( schema.getType().equals( Schema.Type.UNION ) ) {
            List<Schema> schemas = field.getTypes();
            for ( Schema s : schemas ) {
              if ( !s.getName().equalsIgnoreCase( "null" ) ) {
                schema = s;
                break;
              }
            }
          }
          Object precision = schema.getObjectProp( AvroSpec.DECIMAL_PRECISION );
          Object scale = schema.getObjectProp( AvroSpec.DECIMAL_SCALE );
          LogicalTypes.Decimal decimalType =
            LogicalTypes.decimal( Integer.parseInt( precision.toString() ), Integer.parseInt( scale.toString() ) );
          pentahoData = converter.fromBytes( avroData, avroSchema, decimalType );
          break;
        case ValueMetaInterface.TYPE_BINARY:
          pentahoData = new byte[ avroData.remaining() ];
          avroData.get( (byte[]) pentahoData );
          break;
      }
    } catch ( Exception e ) {
      // If unable to do the type conversion just ignore. null will be returned.
    }
  }
  return pentahoData;
}
 
Example 14
/**
 * @param pentahoType
 * @param avroData
 * @param fieldSchema
 * @return
 */
public Object convertToKettleValue( AvroInputField pentahoType, ByteBuffer avroData, Schema fieldSchema ) {
  Object pentahoData = null;
  if ( avroData != null ) {
    try {
      switch ( pentahoType.getPentahoType() ) {
        case ValueMetaInterface.TYPE_BIGNUMBER:
          Conversions.DecimalConversion converter = new Conversions.DecimalConversion();
          Schema schema = fieldSchema;
          if ( schema.getType().equals( Schema.Type.UNION ) ) {
            List<Schema> schemas = schema.getTypes();
            for ( Schema s : schemas ) {
              if ( !s.getName().equalsIgnoreCase( "null" ) ) {
                schema = s;
                break;
              }
            }
          }
          Object precision = schema.getObjectProp( AvroSpec.DECIMAL_PRECISION );
          Object scale = schema.getObjectProp( AvroSpec.DECIMAL_SCALE );
          LogicalTypes.Decimal decimalType =
            LogicalTypes.decimal( Integer.parseInt( precision.toString() ), Integer.parseInt( scale.toString() ) );
          pentahoData = converter.fromBytes( avroData, m_schemaToUse, decimalType );
          break;
        case ValueMetaInterface.TYPE_BINARY:
          pentahoData = new byte[ avroData.remaining() ];
          avroData.get( (byte[]) pentahoData );
          break;
      }
    } catch ( Exception e ) {
      // If unable to do the type conversion just ignore. null will be returned.
    }
  }
  return pentahoData;
}
 
Example 15
private Object convertToPentahoType( int pentahoType, ByteBuffer avroData, Schema.Field field ) {
  Object pentahoData = null;
  if ( avroData != null ) {
    try {
      switch ( pentahoType ) {
        case ValueMetaInterface.TYPE_BIGNUMBER:
          Conversions.DecimalConversion converter = new Conversions.DecimalConversion();
          Schema schema = field.schema();
          if ( schema.getType().equals( Schema.Type.UNION ) ) {
            List<Schema> schemas = field.schema().getTypes();
            for ( Schema s : schemas ) {
              if ( !s.getName().equalsIgnoreCase( "null" ) ) {
                schema = s;
                break;
              }
            }
          }
          Object precision = schema.getObjectProp( AvroSpec.DECIMAL_PRECISION );
          Object scale = schema.getObjectProp( AvroSpec.DECIMAL_SCALE );
          LogicalTypes.Decimal decimalType =
            LogicalTypes.decimal( Integer.parseInt( precision.toString() ), Integer.parseInt( scale.toString() ) );
          pentahoData = converter.fromBytes( avroData, avroSchema, decimalType );
          break;
        case ValueMetaInterface.TYPE_BINARY:
          pentahoData = new byte[ avroData.remaining() ];
          avroData.get( (byte[]) pentahoData );
          break;
      }
    } catch ( Exception e ) {
      // If unable to do the type conversion just ignore. null will be returned.
    }
  }
  return pentahoData;
}
 
Example 16
Source Project: iceberg   File: AvroSchemaUtil.java    License: Apache License 2.0 4 votes vote down vote up
static boolean hasProperty(Schema schema, String propertyName) {
  if (schema.getType() == UNION) {
    return hasProperty(fromOption(schema), propertyName);
  }
  return schema.getObjectProp(propertyName) != null;
}
 
Example 17
@Override
public void setSchema(Schema schema) {
    this.schema = schema;
    Object propValue = schema.getObjectProp(SalesforceSchemaConstants.RETURN_NULL_FOR_EMPTY);
    this.returnNullForEmpty = (propValue == null) ? false : (Boolean) propValue;
}