Java 类org.apache.avro.generic.GenericData.EnumSymbol 实例源码

项目:jasvorno    文件:JasvornoConverterTest.java   
@Test
public void schemaOfTypeEnum() throws JsonProcessingException, IOException {
  Schema schema = SchemaBuilder.builder().enumeration("Suits").namespace("org.cards").doc("card suit names").symbols(
      "HEART", "SPADE", "DIAMOND", "CLUB");
  JsonNode datum = mapper.readTree("\"HEART\"");
  Object actualObj = JasvornoConverter.convertToAvro(datum, schema);
  assertTrue(actualObj instanceof EnumSymbol);
  assertThat(actualObj, is(new EnumSymbol(schema, "HEART")));
}
项目:AvroOutputPlugin    文件:AvroOutput.java   
public Object getValue( Object[] r, AvroOutputField outputField, int inputFieldIndex , Field fieldSchema) throws KettleException {
  Object value;

  switch ( outputField.getAvroType() ) {
    case AvroOutputField.AVRO_TYPE_INT:
      value = data.outputRowMeta.getInteger( r, inputFieldIndex ).intValue();
      break;
    case AvroOutputField.AVRO_TYPE_STRING:
      value = data.outputRowMeta.getString( r, inputFieldIndex );
      break;
    case AvroOutputField.AVRO_TYPE_LONG:
      value = data.outputRowMeta.getInteger( r, inputFieldIndex );
      break;
    case AvroOutputField.AVRO_TYPE_FLOAT:
      value = data.outputRowMeta.getNumber( r, inputFieldIndex ).floatValue();
      break;
    case AvroOutputField.AVRO_TYPE_DOUBLE:
      value = data.outputRowMeta.getNumber( r, inputFieldIndex );
      break;
    case AvroOutputField.AVRO_TYPE_BOOLEAN:
      value = data.outputRowMeta.getBoolean( r, inputFieldIndex );
      break;
    case AvroOutputField.AVRO_TYPE_ENUM:
        Schema schema = getFirstEnumSchema(fieldSchema);
        String fieldValue =  data.outputRowMeta.getString( r, inputFieldIndex );
        value = new GenericData.EnumSymbol(schema , fieldValue);
        break;
    default:
      throw new KettleException( "Avro type " + outputField.getAvroTypeDesc() + " is not supported for field " + outputField.getAvroName() + "." );
  }

  return value;
}
项目:incubator-samoa    文件:AvroLoader.java   
/**
 * Method to read Dense Instances from Avro File
 * 
 * @return Instance
 */
protected Instance readInstanceDense(GenericRecord record)
{
  Instance instance = new DenseInstance(this.instanceInformation.numAttributes() + 1);
  int numAttribute = 0;

  for (Attribute attribute : attributes) {
    Object value = record.get(attribute.name);

    boolean isNumeric = attributes.get(numAttribute).isNumeric();
    boolean isNominal = attributes.get(numAttribute).isNominal();

    if (isNumeric)
    {
      if (value instanceof Double)
        this.setDenseValue(instance, numAttribute, (double) value);
      else if (value instanceof Long)
        this.setDenseValue(instance, numAttribute, (long) value);
      else if (value instanceof Integer)
        this.setDenseValue(instance, numAttribute, (int) value);
      else
        throw new RuntimeException("Invalid data type in the Avro data for Numeric Type : " + attribute.name);
    }
    else if (isNominal)
    {
      double valueAttribute;

      if (!(value instanceof EnumSymbol))
        throw new RuntimeException("Invalid data type in the Avro data for Nominal Type : " + attribute.name);

      EnumSymbol enumSymbolalue = (EnumSymbol) value;

      String stringValue = enumSymbolalue.toString();

      if (("?".equals(stringValue)) || (stringValue == null)) {
        valueAttribute = Double.NaN;
      } else {
        valueAttribute = this.instanceInformation.attribute(numAttribute).indexOfValue(stringValue);
      }

      this.setDenseValue(instance, numAttribute, valueAttribute);
    }
    numAttribute++;
  }

  return (numAttribute > 0) ? instance : null;

}
项目:incubator-samoa    文件:AvroLoader.java   
/**
 * Method to read Sparse Instances from Avro File
 * 
 * @return Instance
 */
protected Instance readInstanceSparse(GenericRecord record) {

  Instance instance = new SparseInstance(1.0, null);
  int numAttribute = -1;
  ArrayList<Double> attributeValues = new ArrayList<Double>();
  List<Integer> indexValues = new ArrayList<Integer>();

  for (Attribute attribute : attributes) {
    numAttribute++;
    Object value = record.get(attribute.name);

    boolean isNumeric = attributes.get(numAttribute).isNumeric();
    boolean isNominal = attributes.get(numAttribute).isNominal();

    /** If value is empty/null iterate to the next attribute. **/
    if (value == null)
      continue;

    if (isNumeric)
    {
      if (value instanceof Double) {
        Double v = (double) value;
        //if (Double.isFinite(v))
        if (!Double.isNaN(v) && !Double.isInfinite(v))
          this.setSparseValue(instance, indexValues, attributeValues, numAttribute, (double) value);
      }
      else if (value instanceof Long)
        this.setSparseValue(instance, indexValues, attributeValues, numAttribute, (long) value);
      else if (value instanceof Integer)
        this.setSparseValue(instance, indexValues, attributeValues, numAttribute, (int) value);
      else
        throw new RuntimeException(AVRO_LOADER_INVALID_TYPE_ERROR + " : " + attribute.name);
    }
    else if (isNominal)
    {
      double valueAttribute;

      if (!(value instanceof EnumSymbol))
        throw new RuntimeException(AVRO_LOADER_INVALID_TYPE_ERROR + " : " + attribute.name);

      EnumSymbol enumSymbolalue = (EnumSymbol) value;

      String stringValue = enumSymbolalue.toString();

      if (("?".equals(stringValue)) || (stringValue == null)) {
        valueAttribute = Double.NaN;
      } else {
        valueAttribute = this.instanceInformation.attribute(numAttribute).indexOfValue(stringValue);
      }

      this.setSparseValue(instance, indexValues, attributeValues, numAttribute, valueAttribute);
    }
  }

  int[] arrayIndexValues = new int[attributeValues.size()];
  double[] arrayAttributeValues = new double[attributeValues.size()];

  for (int i = 0; i < arrayIndexValues.length; i++) {
    arrayIndexValues[i] = indexValues.get(i).intValue();
    arrayAttributeValues[i] = attributeValues.get(i).doubleValue();
  }

  instance.addSparseValues(arrayIndexValues, arrayAttributeValues, this.instanceInformation.numAttributes());
  return instance;

}