Java 类org.apache.avro.generic.GenericEnumSymbol 实例源码

项目:esper    文件:TestAvroEventType.java   
public void testEnumSymbol() {
    Schema schema = record("typename").fields()
            .name("aEnum").type().enumeration("myEnum").symbols("a", "b").enumDefault("x")
            .endRecord();
    EventType eventType = makeAvroSupportEventType(schema);

    assertPropertyType(GenericEnumSymbol.class, null, eventType, "aEnum");

    Consumer<EventBean> asserter = eventBean -> {
        GenericEnumSymbol v = (GenericEnumSymbol) eventBean.get("aEnum");
        assertEquals("b", v.toString());
    };

    GenericData.Record datum = new GenericData.Record(schema);
    datum.put("aEnum", new GenericData.EnumSymbol(schema.getField("aEnum").schema(), "b"));
    asserter.accept(new AvroGenericDataEventBean(datum, eventType));

    String jsonWValues = "{'aEnum': 'b'}";
    datum = SupportAvroUtil.parseQuoted(schema, jsonWValues);
    asserter.accept(new AvroGenericDataEventBean(datum, eventType));
}
项目:spork    文件:AvroTupleWrapper.java   
public static Object unionResolver(Object o) {
  if (o instanceof org.apache.avro.util.Utf8) {
    return o.toString();
  } else if (o instanceof IndexedRecord) {
    return new AvroTupleWrapper<IndexedRecord>((IndexedRecord) o);
  } else if (o instanceof GenericArray) {
    return new AvroBagWrapper<GenericData.Record>(
        (GenericArray<GenericData.Record>) o);
  } else if (o instanceof Map) {
    return new AvroMapWrapper((Map<CharSequence, Object>) o);
  } else if (o instanceof GenericData.Fixed) {
    return new DataByteArray(((GenericData.Fixed) o).bytes());
  } else if (o instanceof ByteBuffer) {
    return new DataByteArray(((ByteBuffer) o).array());
  } else if (o instanceof GenericEnumSymbol) {
    return o.toString();
  } else {
    return o;
  }
}
项目:kaa    文件:DefaultConfigurationManager.java   
private void updateRecord(CommonRecord record, GenericRecord delta) {
  List<Field> deltaFields = delta.getSchema().getFields();
  for (Field deltaField : deltaFields) {
    String fieldName = deltaField.name();
    Object rawDeltaField = delta.get(fieldName);
    if (LOG.isDebugEnabled()) {
      LOG.debug("Processing field \"{}\", current value: {}",
          fieldName, record.getField(fieldName) != null ? record
              .getField(fieldName).toString() : null);
    }
    if (AvroGenericUtils.isRecord(rawDeltaField)) {
      processRecordField(record, (GenericRecord) rawDeltaField, fieldName);
    } else if (AvroGenericUtils.isArray(rawDeltaField)) {
      processArrayField(record, (GenericArray) rawDeltaField, fieldName);
    } else if (AvroGenericUtils.isEnum(rawDeltaField)) {
      processEnumField(record, (GenericEnumSymbol) rawDeltaField, fieldName);
    } else if (AvroGenericUtils.isFixed(rawDeltaField)) {
      processFixedField(record, (GenericFixed) rawDeltaField, fieldName);
    } else {
      record.setField(fieldName, commonFactory.createCommonValue(rawDeltaField));
    }
  }
}
项目:kaa    文件:DefaultDeltaCalculatorTest.java   
public static AvroBinaryDelta getComplexFieldDelta(Schema schema) {
  GenericRecord delta = new GenericData.Record(getDeltaSchemaByFullName(schema, "org.kaa.config.testT"));
  GenericEnumSymbol unchanged = new GenericData.EnumSymbol(getSchemaByFullName(delta.getSchema().getField("testField1").schema().getTypes(),
      "org.kaaproject.configuration.unchangedT"), "unchanged");
  GenericRecord testField2 = new GenericData.Record(getSchemaByFullName(delta.getSchema().getField("testField2").schema().getTypes(),
      "org.kaa.config.testRecordT"));
  testField2.put("testField3", 456);
  byte[] rawUuid = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
  GenericFixed uuid = new GenericData.Fixed(delta.getSchema().getField("__uuid").schema(), rawUuid);
  delta.put("testField1", unchanged);
  delta.put("testField2", testField2);
  delta.put("__uuid", uuid);

  AvroBinaryDelta deltaExpected = new AvroBinaryDelta(schema);
  deltaExpected.addDelta(delta);
  return deltaExpected;
}
项目:embulk-formatter-avro    文件:AvroEnumConverter.java   
@Override
public GenericEnumSymbol stringColumn(String value) {
    if (enumSymbols.contains(value)) {
        return new GenericData.EnumSymbol(avroSchema, value);
    } else {
        throw new RuntimeException(String.format("%s is not in %s", value, enumSymbols.toString()));
    }
}
项目:embulk-formatter-avro    文件:AvroEnumConverter.java   
@Override
public GenericEnumSymbol stringColumn(String value) {
    if (enumSymbols.contains(value)) {
        return new GenericData.EnumSymbol(avroSchema, value);
    } else {
        throw new RuntimeException(String.format("%s is not in %s", value, enumSymbols.toString()));
    }
}
项目:components    文件:GeneratorFunctions.java   
public GenericEnumSymbol apply(GeneratorContext input) {
    if (constants == null) {
        if (schema == null) {
            schema = new Schema.Parser().parse(jsonSchema);
        }
        constants = new GenericEnumSymbol[schema.getEnumSymbols().size()];
        for (int i = 0; i < constants.length; i++) {
            constants[i] = new GenericData.EnumSymbol(schema, schema.getEnumSymbols().get(i));
        }
    }
    return constants[input.getRandom().nextInt(constants.length)];
}
项目:avro-ui    文件:FormAvroConverter.java   
/**
 * Checks if is override unchanged datum.
 *
 * @param datum the datum
 * @return true, if is override unchanged datum
 */
private static boolean isOverrideUnchangedDatum(Object datum) {
    if (datum != null && datum instanceof GenericEnumSymbol) {
        Schema schema = ((GenericContainer)datum).getSchema();
        return isOverrideType(schema);
    } else {
        return false;
    }
}
项目:kaa    文件:DefaultConfigurationManager.java   
private void processEnumField(CommonRecord record, GenericEnumSymbol symbol, String fieldName) {
  Schema enumSchema = symbol.getSchema();
  if (AvroGenericUtils.isReset(symbol)) {
    record.getField(fieldName).getArray().getList().clear();
  } else if (!AvroGenericUtils.isUnchanged(symbol)) {
    record.setField(fieldName, commonFactory.createCommonValue(
            commonFactory.createCommonEnum(enumSchema, symbol.toString())));
  }
}
项目:kaa    文件:AvroGenericUtils.java   
/**
 * checks if the given value is Reset (value's schema is "org.kaaproject.configuration.resetT").
 *
 * @param field object which going to be verified.
 * @return true if the value is Reset, false otherwise.
 */
public static boolean isReset(Object field) {
  if (!isEnum(field)) {
    return false;
  }
  return getEnumFullName((GenericEnumSymbol) field).equals(RESETT);
}
项目:kaa    文件:DefaultConfigurationManagerTest.java   
public static void fillComplexPartialDelta(GenericRecord delta) {
  GenericRecord testField22 = new GenericData.Record(getSchemaByFullName(
      delta.getSchema().getField("testField2").schema().getTypes(),
      "org.kaa.config.testRecordT"));
  testField22.put("testField3", 654);
  GenericEnumSymbol unchanged = new GenericData.EnumSymbol(
      getSchemaByFullName(delta.getSchema().getField("testField1")
          .schema().getTypes(), "org.kaaproject.configuration.unchangedT"),
      "unchanged");
  delta.put("testField1", unchanged);
  delta.put("testField2", testField22);
}
项目:kaa    文件:CommonToGenericTest.java   
@Test
public void testRecord() {
  List<String> symbols = new LinkedList<>();
  symbols.add("enum1");
  symbols.add("enum2");
  Schema recordSchema = Schema.createRecord("someRecord", "", "", false);
  List<Schema.Field> fields = new LinkedList<>();
  fields.add(new Schema.Field("field1", Schema.createEnum("someEnum", "", "", symbols), "", null));
  fields.add(new Schema.Field("field2", Schema.create(Type.BOOLEAN), "", null));
  fields.add(new Schema.Field("field3", Schema.create(Type.INT), "", null));
  fields.add(new Schema.Field("field4", Schema.create(Type.LONG), "", null));
  fields.add(new Schema.Field("field5", Schema.create(Type.DOUBLE), "", null));
  fields.add(new Schema.Field("field6", Schema.create(Type.FLOAT), "", null));
  fields.add(new Schema.Field("field7", Schema.create(Type.STRING), "", null));
  fields.add(new Schema.Field("field8", Schema.create(Type.BYTES), "", null));
  recordSchema.setFields(fields);
  CommonRecord record = new DefaultCommonRecord(recordSchema);

  record.setField("field1", new DefaultCommonValue(new DefaultCommonEnum(Schema.createEnum("someEnum", "", "", symbols), "enum2")));
  record.setField("field2", new DefaultCommonValue(new Boolean(Boolean.TRUE)));
  record.setField("field3", new DefaultCommonValue(new Integer(1)));
  record.setField("field4", new DefaultCommonValue(new Long(2)));
  record.setField("field5", new DefaultCommonValue(new Double(5.5)));
  record.setField("field6", new DefaultCommonValue(new Float(5.6)));
  record.setField("field7", new DefaultCommonValue(new String("abc")));
  record.setField("field8", new DefaultCommonValue(ByteBuffer.wrap(new byte[]{1, 2})));

  GenericRecord avroRecord = CommonToGeneric.createRecord(record);
  GenericEnumSymbol field1 = (GenericEnumSymbol) avroRecord.get("field1");
  assertEquals("enum2", field1.toString());
  assertTrue((Boolean) avroRecord.get("field2"));
  assertEquals(1, ((Integer) avroRecord.get("field3")).intValue());
  assertEquals(2, ((Long) avroRecord.get("field4")).longValue());
  assertEquals(5.5, ((Double) avroRecord.get("field5")).doubleValue(), 0.1);
  assertEquals(5.6, ((Float) avroRecord.get("field6")).floatValue(), 0.1);
  assertEquals("abc", (String) avroRecord.get("field7"));
  assertArrayEquals(new byte[]{1, 2}, ((ByteBuffer) avroRecord.get("field8")).array());
}
项目:kaa    文件:DefaultDeltaCalculationAlgorithm.java   
/**
 * Put notchaged.
 *
 * @param delta the delta
 * @param field the field
 * @throws DeltaCalculatorException the delta calculator exception
 */
private static void putUnchanged(GenericRecord delta, String field)
        throws DeltaCalculatorException {
  Schema unchangedSchema = getSchemaByFullName(
          delta, field, KAA_NAMESPACE + "." + UNCHANGED + "T");
  if (unchangedSchema != null) {
    GenericEnumSymbol unchanged = new GenericData.EnumSymbol(unchangedSchema, UNCHANGED);
    delta.put(field, unchanged);
  } else {
    throw new DeltaCalculatorException(new StringBuilder()
        .append("Failed to find schema for \"unchanged\" type ").append(" in ")
        .append(delta.getSchema().getFullName()).append(" field ").append(field).toString());
  }
}
项目:kaa    文件:DefaultDeltaCalculationAlgorithm.java   
/**
 * Put reset.
 *
 * @param delta the delta
 * @param field the field
 * @throws DeltaCalculatorException the delta calculator exception
 */
private static void putReset(GenericRecord delta, String field) throws DeltaCalculatorException {
  Schema resetSchema = getSchemaByFullName(delta, field, KAA_NAMESPACE + "." + RESET + "T");
  if (resetSchema != null) {
    GenericEnumSymbol reset = new GenericData.EnumSymbol(resetSchema, RESET);
    delta.put(field, reset);
  } else {
    throw new DeltaCalculatorException(new StringBuilder().append(
            "Failed to find schema for \"reset\" type ").append(" in ").append(
            delta.getSchema().getFullName()).append(" field ").append(field).toString());
  }
}
项目:kaa    文件:DefaultDeltaCalculatorTest.java   
@Test
public void testPrimitiveFieldsDelta() throws IOException, DeltaCalculatorException {
  URL protocolSchemaUrl = Thread.currentThread().getContextClassLoader().getResource("delta/primitiveFieldsDeltaProtocolSchema.json");
  Schema protocolSchema = new Schema.Parser().parse(new File(protocolSchemaUrl.getPath()));
  URL schemaUrl = Thread.currentThread().getContextClassLoader().getResource("delta/primitiveFieldsDeltaSchema.json");
  Schema schema = new Schema.Parser().parse(new File(schemaUrl.getPath()));
  DeltaCalculationAlgorithm calculator = new DefaultDeltaCalculationAlgorithm(protocolSchema, schema);

  ByteArrayOutputStream baosOld = new ByteArrayOutputStream();
  URL oldConfigUrl = Thread.currentThread().getContextClassLoader().getResource("delta/primitiveFieldsDeltaCurrent.json");
  IOUtils.copy(new FileInputStream(oldConfigUrl.getPath()), baosOld, 1024);
  String oldStr = new String(baosOld.toByteArray(), "UTF-8");

  ByteArrayOutputStream baosNew = new ByteArrayOutputStream();
  URL newConfigUrl = Thread.currentThread().getContextClassLoader().getResource("delta/primitiveFieldsDeltaNew.json");
  IOUtils.copy(new FileInputStream(newConfigUrl.getPath()), baosNew, 1024);
  String newStr = new String(baosNew.toByteArray(), "UTF-8");

  BaseData oldData = new BaseData(new BaseSchema(schema.toString()), oldStr);
  BaseData newData = new BaseData(new BaseSchema(schema.toString()), newStr);
  RawBinaryDelta deltaResult = calculator.calculate(oldData, newData);

  GenericRecord delta = new GenericData.Record(getDeltaSchemaByFullName(protocolSchema, "org.kaa.config.testT"));
  GenericEnumSymbol unchanged = new GenericData.EnumSymbol(getSchemaByFullName(delta.getSchema().getField("testField1").schema().getTypes(),
      "org.kaaproject.configuration.unchangedT"), "unchanged");
  byte[] rawUuid = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
  GenericFixed uuid = new GenericData.Fixed(delta.getSchema().getField("__uuid").schema(), rawUuid);
  delta.put("testField1", unchanged);
  delta.put("testField2", 456);
  delta.put("__uuid", uuid);

  AvroBinaryDelta deltaExpected = new AvroBinaryDelta(protocolSchema);
  deltaExpected.addDelta(delta);

  assertArrayEquals(deltaExpected.getData(), deltaResult.getData());
}
项目:avro-random-generator    文件:Generator.java   
private GenericEnumSymbol generateEnumSymbol(Schema schema) {
  List<String> enums = schema.getEnumSymbols();
  return new
      GenericData.EnumSymbol(schema, enums.get(random.nextInt(enums.size())));
}
项目:ksql    文件:Generator.java   
private GenericEnumSymbol generateEnumSymbol(Schema schema) {
  List<String> enums = schema.getEnumSymbols();
  return new
      GenericData.EnumSymbol(schema, enums.get(random.nextInt(enums.size())));
}
项目:esper    文件:AvroTypeUtil.java   
public static Class propertyType(Schema fieldSchema) {
    if (fieldSchema.getType() == Schema.Type.UNION) {
        boolean hasNull = false;
        Set<Class> unionTypes = new HashSet<>();
        for (Schema memberSchema : fieldSchema.getTypes()) {
            if (memberSchema.getType() == Schema.Type.NULL) {
                hasNull = true;
            } else {
                Class type = propertyType(memberSchema);
                if (type != null) {
                    unionTypes.add(type);
                }
            }
        }
        if (unionTypes.isEmpty()) {
            return null;
        }
        if (unionTypes.size() == 1) {
            if (hasNull) {
                return JavaClassHelper.getBoxedType(unionTypes.iterator().next());
            }
            return unionTypes.iterator().next();
        }
        boolean allNumeric = true;
        for (Class unioned : unionTypes) {
            if (!JavaClassHelper.isNumeric(unioned)) {
                allNumeric = false;
            }
        }
        if (allNumeric) {
            return Number.class;
        }
        return Object.class;
    } else if (fieldSchema.getType() == Schema.Type.RECORD) {
        return GenericData.Record.class;
    } else if (fieldSchema.getType() == Schema.Type.ARRAY) {
        return Collection.class;
    } else if (fieldSchema.getType() == Schema.Type.MAP) {
        return Map.class;
    } else if (fieldSchema.getType() == Schema.Type.FIXED) {
        return GenericFixed.class;
    } else if (fieldSchema.getType() == Schema.Type.ENUM) {
        return GenericEnumSymbol.class;
    } else if (fieldSchema.getType() == Schema.Type.STRING) {
        String prop = fieldSchema.getProp(PROP_JAVA_STRING_KEY);
        return prop == null || !prop.equals(PROP_JAVA_STRING_VALUE) ? CharSequence.class : String.class;
    }
    AvroTypeDesc desc = TYPES_PER_AVRO_ORD[fieldSchema.getType().ordinal()];
    return desc == null ? null : desc.getType();
}
项目:zSqoop    文件:AvroExportMapper.java   
private Object fromAvro(Object avroObject, Schema fieldSchema,
    String columnType) {
  // map from Avro type to Sqoop's Java representation of the SQL type
  // see SqlManager#toJavaType

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

  switch (fieldSchema.getType()) {
    case NULL:
      return null;
    case BOOLEAN:
    case INT:
    case FLOAT:
    case DOUBLE:
      return avroObject;
    case LONG:
      if (columnType.equals(DATE_TYPE)) {
        return new Date((Long) avroObject);
      } else if (columnType.equals(TIME_TYPE)) {
        return new Time((Long) avroObject);
      } else if (columnType.equals(TIMESTAMP_TYPE)) {
        return new Timestamp((Long) avroObject);
      }
      return avroObject;
    case BYTES:
      ByteBuffer bb = (ByteBuffer) avroObject;
      BytesWritable bw = new BytesWritable();
      bw.set(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining());
      return bw;
    case STRING:
      if (columnType.equals(BIG_DECIMAL_TYPE)) {
        return new BigDecimal(avroObject.toString());
      } else if (columnType.equals(DATE_TYPE)) {
        return Date.valueOf(avroObject.toString());
      } else if (columnType.equals(TIME_TYPE)) {
        return Time.valueOf(avroObject.toString());
      } else if (columnType.equals(TIMESTAMP_TYPE)) {
        return Timestamp.valueOf(avroObject.toString());
      }
      return avroObject.toString();
    case ENUM:
      return ((GenericEnumSymbol) avroObject).toString();
    case UNION:
      List<Schema> types = fieldSchema.getTypes();
      if (types.size() != 2) {
        throw new IllegalArgumentException("Only support union with null");
      }
      Schema s1 = types.get(0);
      Schema s2 = types.get(1);
      if (s1.getType() == Schema.Type.NULL) {
        return fromAvro(avroObject, s2, columnType);
      } else if (s2.getType() == Schema.Type.NULL) {
        return fromAvro(avroObject, s1, columnType);
      } else {
        throw new IllegalArgumentException("Only support union with null");
      }
    case FIXED:
      return new BytesWritable(((GenericFixed) avroObject).bytes());
    case RECORD:
    case ARRAY:
    case MAP:
    default:
      throw new IllegalArgumentException("Cannot convert Avro type "
          + fieldSchema.getType());
  }
}
项目:kaa    文件:DefaultConfigurationManagerTest.java   
public static void fillArrayResetDelta(GenericRecord delta) {
  GenericEnumSymbol reset = new GenericData.EnumSymbol(
      getSchemaByFullName(delta.getSchema().getField("testField1")
          .schema().getTypes(), "org.kaaproject.configuration.resetT"), "reset");
  delta.put("testField1", reset);
}
项目:kaa    文件:DefaultDeltaCalculatorTest.java   
@Test
public void testArrayFieldsDelta() throws IOException, URISyntaxException, DeltaCalculatorException {
  URL protocolSchemaUrl = Thread.currentThread().getContextClassLoader().getResource("delta/arrayFieldsDeltaProtocolSchema.json");
  Schema protocolSchema = new Schema.Parser().parse(new File(protocolSchemaUrl.getPath()));
  URL schemaUrl = Thread.currentThread().getContextClassLoader().getResource("delta/arrayFieldsDeltaSchema.json");
  Schema schema = new Schema.Parser().parse(new File(schemaUrl.getPath()));

  DeltaCalculationAlgorithm calculator = new DefaultDeltaCalculationAlgorithm(protocolSchema, schema);

  ByteArrayOutputStream baosOld = new ByteArrayOutputStream();
  URL oldConfigUrl = Thread.currentThread().getContextClassLoader().getResource("delta/arrayFieldsDeltaCurrent.json");
  IOUtils.copy(new FileInputStream(oldConfigUrl.getPath()), baosOld, 1024);
  String oldStr = new String(baosOld.toByteArray(), "UTF-8");

  ByteArrayOutputStream baosNew = new ByteArrayOutputStream();
  URL newConfigUrl = Thread.currentThread().getContextClassLoader().getResource("delta/arrayFieldsDeltaNew.json");
  IOUtils.copy(new FileInputStream(newConfigUrl.getPath()), baosNew, 1024);
  String newStr = new String(baosNew.toByteArray(), "UTF-8");

  BaseData oldData = new BaseData(new BaseSchema(schema.toString()), oldStr);
  BaseData newData = new BaseData(new BaseSchema(schema.toString()), newStr);
  RawBinaryDelta deltaResult = calculator.calculate(oldData, newData);

      /* The first delta - the item object was changed */
  GenericRecord delta1 = new GenericData.Record(getDeltaSchemaByFullName(protocolSchema, "org.kaa.config.testRecordItemT"));
  byte[] rawUuid1 = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3};
  GenericFixed uuid1 = new GenericData.Fixed(delta1.getSchema().getField("__uuid").schema(), rawUuid1);
  delta1.put("testField4", 36);
  delta1.put("__uuid", uuid1);

      /* The second delta - one item was removed from the array */
  GenericRecord delta2 = new GenericData.Record(getDeltaSchemaByFullName(protocolSchema, "org.kaa.config.testT"));
  byte[] rawUuid2 = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
  GenericFixed uuid2 = new GenericData.Fixed(delta2.getSchema().getField("__uuid").schema(), rawUuid2);

  GenericEnumSymbol unchanged = new GenericData.EnumSymbol(getSchemaByFullName(delta2.getSchema().getField("testField1").schema().getTypes(),
      "org.kaaproject.configuration.unchangedT"), "unchanged");
  delta2.put("testField1", unchanged);
  delta2.put("__uuid", uuid2);
  delta2.put("testField5", unchanged);

  GenericRecord testField21 = new GenericData.Record(getSchemaByFullName(delta2.getSchema().getField("testField2").schema().getTypes(),
      "org.kaa.config.testRecordT"));
  GenericArray<GenericFixed> testField31 = new GenericData.Array<GenericFixed>(1, getArraySchema(testField21, "testField3"));
  byte[] rawUuidToDelete = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
  GenericFixed uuidToDelete = new GenericData.Fixed(getSchemaByFullName(testField31.getSchema().getElementType().getTypes(),
      "org.kaaproject.configuration.uuidT"), rawUuidToDelete);
  testField31.add(uuidToDelete);
  testField21.put("testField3", testField31);
  delta2.put("testField2", testField21);

      /* The third delta - one new item was added to the array */
  GenericRecord delta3 = new GenericData.Record(getDeltaSchemaByFullName(protocolSchema, "org.kaa.config.testT"));
  delta3.put("testField1", unchanged);
  delta3.put("__uuid", uuid2);

  GenericRecord testField22 = new GenericData.Record(getSchemaByFullName(delta3.getSchema().getField("testField2").schema().getTypes(),
      "org.kaa.config.testRecordT"));
  GenericArray<GenericRecord> testField32 = new GenericData.Array<GenericRecord>(1, getArraySchema(testField22, "testField3"));
  GenericRecord itemRecord = new GenericData.Record(getSchemaByFullName(testField32.getSchema().getElementType().getTypes(),
      "org.kaa.config.testRecordItemT"));
  itemRecord.put("testField4", 4);
  byte[] rawNewRecordUuid = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4};
  GenericFixed newRecordUuid = new GenericData.Fixed(itemRecord.getSchema().getField("__uuid").schema(), rawNewRecordUuid);
  itemRecord.put("__uuid", newRecordUuid);
  testField32.add(itemRecord);
  testField22.put("testField3", testField32);
  delta3.put("testField2", testField22);

  AvroBinaryDelta deltaExpected = new AvroBinaryDelta(protocolSchema);
  deltaExpected.addDelta(delta1);
  deltaExpected.addDelta(delta2);
  deltaExpected.addDelta(delta3);

  assertArrayEquals(deltaExpected.getData(), deltaResult.getData());
}
项目:kaa    文件:DefaultDeltaCalculatorTest.java   
@Test
public void testArrayPrimitiveFieldsDelta() throws IOException, URISyntaxException, DeltaCalculatorException {
  URL protocolSchemaUrl = Thread.currentThread().getContextClassLoader().getResource("delta/arrayPrimitiveFieldsDeltaProtocolSchema.json");
  Schema protocolSchema = new Schema.Parser().parse(new File(protocolSchemaUrl.getPath()));
  URL schemaUrl = Thread.currentThread().getContextClassLoader().getResource("delta/arrayPrimitiveFieldsDeltaSchema.json");
  Schema schema = new Schema.Parser().parse(new File(schemaUrl.getPath()));
  DeltaCalculationAlgorithm calculator = new DefaultDeltaCalculationAlgorithm(protocolSchema, schema);

  ByteArrayOutputStream baosOld = new ByteArrayOutputStream();
  URL oldConfigUrl = Thread.currentThread().getContextClassLoader().getResource("delta/arrayPrimitiveFieldsDeltaCurrent.json");
  IOUtils.copy(new FileInputStream(oldConfigUrl.getPath()), baosOld, 1024);
  String oldStr = new String(baosOld.toByteArray(), "UTF-8");

  ByteArrayOutputStream baosNew = new ByteArrayOutputStream();
  URL newConfigUrl = Thread.currentThread().getContextClassLoader().getResource("delta/arrayPrimitiveFieldsDeltaNew.json");
  IOUtils.copy(new FileInputStream(newConfigUrl.getPath()), baosNew, 1024);
  String newStr = new String(baosNew.toByteArray(), "UTF-8");

  BaseData oldData = new BaseData(new BaseSchema(schema.toString()), oldStr);
  BaseData newData = new BaseData(new BaseSchema(schema.toString()), newStr);
  RawBinaryDelta deltaResult = calculator.calculate(oldData, newData);

  GenericRecord delta1 = new GenericData.Record(getDeltaSchemaByFullName(protocolSchema, "org.kaa.config.testT"));
  GenericEnumSymbol reset = new GenericData.EnumSymbol(getSchemaByFullName(delta1.getSchema().getField("testField1").schema().getTypes(),
      "org.kaaproject.configuration.resetT"), "reset");
  byte[] rawUuid = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
  GenericFixed uuid = new GenericData.Fixed(delta1.getSchema().getField("__uuid").schema(), rawUuid);
  delta1.put("testField1", reset);
  delta1.put("__uuid", uuid);

  GenericRecord delta2 = new GenericData.Record(getDeltaSchemaByFullName(protocolSchema, "org.kaa.config.testT"));
  delta2.put("__uuid", uuid);
  GenericArray<Integer> testField1 = new GenericData.Array<Integer>(3, getArraySchema(delta2, "testField1"));
  testField1.add(321);
  testField1.add(456);
  testField1.add(654);
  delta2.put("testField1", testField1);

  AvroBinaryDelta deltaExpected = new AvroBinaryDelta(protocolSchema);
  deltaExpected.addDelta(delta1);
  deltaExpected.addDelta(delta2);

  assertArrayEquals(deltaExpected.getData(), deltaResult.getData());
}
项目:sqoop    文件:AvroExportMapper.java   
private Object fromAvro(Object avroObject, Schema fieldSchema,
    String columnType) {
  // map from Avro type to Sqoop's Java representation of the SQL type
  // see SqlManager#toJavaType

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

  switch (fieldSchema.getType()) {
    case NULL:
      return null;
    case BOOLEAN:
    case INT:
    case FLOAT:
    case DOUBLE:
      return avroObject;
    case LONG:
      if (columnType.equals(DATE_TYPE)) {
        return new Date((Long) avroObject);
      } else if (columnType.equals(TIME_TYPE)) {
        return new Time((Long) avroObject);
      } else if (columnType.equals(TIMESTAMP_TYPE)) {
        return new Timestamp((Long) avroObject);
      }
      return avroObject;
    case BYTES:
      ByteBuffer bb = (ByteBuffer) avroObject;
      BytesWritable bw = new BytesWritable();
      bw.set(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining());
      return bw;
    case STRING:
      if (columnType.equals(BIG_DECIMAL_TYPE)) {
        return new BigDecimal(avroObject.toString());
      } else if (columnType.equals(DATE_TYPE)) {
        return Date.valueOf(avroObject.toString());
      } else if (columnType.equals(TIME_TYPE)) {
        return Time.valueOf(avroObject.toString());
      } else if (columnType.equals(TIMESTAMP_TYPE)) {
        return Timestamp.valueOf(avroObject.toString());
      }
      return avroObject.toString();
    case ENUM:
      return ((GenericEnumSymbol) avroObject).toString();
    case UNION:
      List<Schema> types = fieldSchema.getTypes();
      if (types.size() != 2) {
        throw new IllegalArgumentException("Only support union with null");
      }
      Schema s1 = types.get(0);
      Schema s2 = types.get(1);
      if (s1.getType() == Schema.Type.NULL) {
        return fromAvro(avroObject, s2, columnType);
      } else if (s2.getType() == Schema.Type.NULL) {
        return fromAvro(avroObject, s1, columnType);
      } else {
        throw new IllegalArgumentException("Only support union with null");
      }
    case FIXED:
      return new BytesWritable(((GenericFixed) avroObject).bytes());
    case RECORD:
    case ARRAY:
    case MAP:
    default:
      throw new IllegalArgumentException("Cannot convert Avro type "
          + fieldSchema.getType());
  }
}
项目:kaa    文件:AvroGenericUtils.java   
/**
 * Checks if the given value is Unchanged (value's schema is
 * "org.kaaproject.configuration.unchangedT").
 *
 * @param field object which going to be verified.
 * @return true if the value is Unchanged, false otherwise.
 */
public static boolean isUnchanged(Object field) {
  if (!isEnum(field)) {
    return false;
  }
  return getEnumFullName((GenericEnumSymbol) field).equals(UNCHANGEDT);
}
项目:kaa    文件:CommonToGeneric.java   
/**
 * Creates new GenericEnumSymbol from the given CommonEnum object.
 *
 * @param commonEnum CommonEnum object.
 * @return new GenericEnumSymbol object.
 */
public static GenericEnumSymbol createEnum(CommonEnum commonEnum) {
  return new GenericData.EnumSymbol(commonEnum.getSchema(), commonEnum.getSymbol());
}
项目:kaa    文件:AvroGenericUtils.java   
/**
 * Checks if the given value is GenericEnum.
 *
 * @param field object which going to be verified.
 * @return true if the value is GenericEnum, false otherwise.
 */
public static boolean isEnum(Object field) {
  return field instanceof GenericEnumSymbol;
}
项目:kaa    文件:AvroGenericUtils.java   
/**
 * Retrieves full schema name of the given enum.
 *
 * @param symbol enum symbol whose name will be returned.
 * @return schema full name.
 */
public static String getEnumFullName(GenericEnumSymbol symbol) {
  Schema enumSchema = symbol.getSchema();
  return enumSchema.getFullName();
}
项目:kaa    文件:DefaultRecordGenerationAlgorithmImpl.java   
/**
 * Processes enum type.
 *
 * @param schemaNode schema for current type.
 * @return generated value for input enum type.
 */
private Object processEnum(Schema schemaNode) {
  GenericEnumSymbol result = new GenericData.EnumSymbol(schemaNode,
          schemaNode.getEnumSymbols().get(0));
  return result;
}