Search in sources :

Example 1 with GenericEnumSymbol

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;
}
Also used : GenericFixed(org.apache.avro.generic.GenericFixed) IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) GenericEnumSymbol(org.apache.avro.generic.GenericEnumSymbol) ByteBuffer(java.nio.ByteBuffer) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 2 with GenericEnumSymbol

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"));
}
Also used : Schema(org.apache.avro.Schema) GenericEnumSymbol(org.apache.avro.generic.GenericEnumSymbol) GenericEnumSymbol(org.apache.avro.generic.GenericEnumSymbol) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.junit.Test)

Example 3 with GenericEnumSymbol

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()));
    }
}
Also used : Schema(org.apache.avro.Schema) GenericEnumSymbol(org.apache.avro.generic.GenericEnumSymbol) GenericEnumSymbol(org.apache.avro.generic.GenericEnumSymbol) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.junit.Test)

Aggregations

Schema (org.apache.avro.Schema)3 GenericEnumSymbol (org.apache.avro.generic.GenericEnumSymbol)3 GenericRecord (org.apache.avro.generic.GenericRecord)2 Test (org.junit.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ByteBuffer (java.nio.ByteBuffer)1 Collection (java.util.Collection)1 Map (java.util.Map)1 GenericFixed (org.apache.avro.generic.GenericFixed)1 IndexedRecord (org.apache.avro.generic.IndexedRecord)1