use of org.apache.avro.generic.GenericEnumSymbol in project streamline by hortonworks.
the class AvroStreamsSnapshotDeserializer method convertValue.
private Object convertValue(Object deserializedObj) {
Object value;
// check for specific-record type and build a map from that
if (deserializedObj instanceof IndexedRecord) {
// record
IndexedRecord indexedRecord = (IndexedRecord) deserializedObj;
List<Schema.Field> fields = indexedRecord.getSchema().getFields();
ImmutableMap.Builder<String, Object> keyValues = ImmutableMap.builder();
for (Schema.Field field : fields) {
Object currentValue = convertValue(indexedRecord.get(field.pos()));
if (currentValue != null) {
keyValues.put(field.name(), currentValue);
}
}
value = keyValues.build();
} else if (deserializedObj instanceof ByteBuffer) {
// byte array representation
ByteBuffer byteBuffer = (ByteBuffer) deserializedObj;
byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes);
value = bytes;
} else if (deserializedObj instanceof GenericEnumSymbol) {
// enums
GenericEnumSymbol symbol = (GenericEnumSymbol) deserializedObj;
value = symbol.toString();
} else if (deserializedObj instanceof CharSequence) {
// symbols
value = deserializedObj.toString();
} else if (deserializedObj instanceof Map) {
// type of map
Map<Object, Object> map = (Map<Object, Object>) deserializedObj;
ImmutableMap.Builder<String, Object> keyValues = ImmutableMap.builder();
for (Map.Entry entry : map.entrySet()) {
Object currentValue = convertValue(entry.getValue());
if (currentValue != null) {
keyValues.put(entry.getKey().toString(), currentValue);
}
}
value = keyValues.build();
} else if (deserializedObj instanceof Collection) {
// type of array
Collection<Object> collection = (Collection<Object>) deserializedObj;
ImmutableList.Builder<Object> values = ImmutableList.builder();
for (Object obj : collection) {
Object currentValue = convertValue(obj);
if (currentValue != null) {
values.add(currentValue);
}
}
value = values.build();
} else if (deserializedObj instanceof GenericFixed) {
// fixed type
GenericFixed genericFixed = (GenericFixed) deserializedObj;
value = genericFixed.bytes();
} else {
// other primitive types
value = deserializedObj;
}
return value;
}
use of org.apache.avro.generic.GenericEnumSymbol in project hive by apache.
the class TestAvroSerializer method canSerializeNullableEnums.
@Test
public void canSerializeNullableEnums() throws SerDeException, IOException {
String type = "{\"type\": \"enum\", \"name\": \"enum1_values\",\n" + " \"namespace\": \"org.apache.hadoop.hive\",\n" + " \"symbols\":[\"BLUE\",\"RED\",\"GREEN\"]}";
Schema schema = AvroSerdeUtils.getSchemaFor(type);
String field = "{ \"name\":\"nullableenum\", \"type\": [\"null\", " + schema + "] }";
GenericEnumSymbol symbol = new GenericData.EnumSymbol(schema, enum1.BLUE.toString());
GenericRecord r = serializeAndDeserialize(field, "nullableenum", symbol);
assertEquals(enum1.BLUE, enum1.valueOf(r.get("nullableenum").toString()));
r = serializeAndDeserialize(field, "nullableenum", null);
assertNull(r.get("nullableenum"));
}
use of org.apache.avro.generic.GenericEnumSymbol in project hive by apache.
the class TestAvroSerializer method canSerializeEnums.
@Test
public void canSerializeEnums() throws SerDeException, IOException {
String type = "{\"type\": \"enum\", \"name\": \"enum1_values\", " + "\"symbols\":[\"BLUE\",\"RED\",\"GREEN\"]}";
Schema schema = AvroSerdeUtils.getSchemaFor(type);
String field = "{ \"name\":\"enum1\", \"type\": " + schema + " }";
for (enum1 e : enum1.values()) {
GenericEnumSymbol symbol = new GenericData.EnumSymbol(schema, e.toString());
GenericRecord r = serializeAndDeserialize(field, "enum1", symbol);
assertEquals(e, enum1.valueOf(r.get("enum1").toString()));
}
}
Aggregations