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

The following examples show how to use org.apache.avro.Schema#getObjectProp() . 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 File: AvroSchemaUtil.java    From iceberg with 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
Source File: AvroSchemaUtil.java    From iceberg with 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 3
Source File: PentahoAvroRecordReader.java    From pentaho-hadoop-shims with Apache License 2.0 5 votes vote down vote up
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 4
Source File: AvroNestedReader.java    From pentaho-hadoop-shims with Apache License 2.0 5 votes vote down vote up
/**
 * @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 5
Source File: AvroToPdiConverter.java    From pentaho-hadoop-shims with Apache License 2.0 5 votes vote down vote up
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 6
Source File: Generator.java    From avro-random-generator with Do What The F*ck You Want To Public License 5 votes vote down vote up
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 7
Source File: SchemaToType.java    From iceberg with 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 8
Source File: SchemaToType.java    From iceberg with 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 9
Source File: SchemaToType.java    From iceberg with 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 File: Generator.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 5 votes vote down vote up
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 11
Source File: AvroSchemaUtil.java    From iceberg with 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 12
Source File: SchemaToType.java    From iceberg with 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 13
Source File: SchemaToType.java    From iceberg with 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 14
Source File: SchemaToType.java    From iceberg with 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 15
Source File: AvroSchemaUtil.java    From iceberg with 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 16
Source File: BulkResultAdapterFactory.java    From components with Apache License 2.0 4 votes vote down vote up
@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;
}
 
Example 17
Source File: AvroSchemaUtil.java    From iceberg with 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;
}