Search in sources :

Example 6 with GenericRecord

use of org.apache.avro.generic.GenericRecord in project hive by apache.

the class TestAvroSerializer method canSerializeArraysWithNullableComplexElements.

@Test
public void canSerializeArraysWithNullableComplexElements() throws SerDeException, IOException {
    final String field = "{ \"name\":\"listOfNullableLists\", \"type\": " + "{\"type\":\"array\", \"items\": [\"null\", " + "{\"type\": \"array\", \"items\": \"int\"}]} }";
    List<List<Integer>> intListList = new ArrayList<List<Integer>>();
    List<Integer> intList = new ArrayList<Integer>();
    Collections.addAll(intList, 1, 2, 3);
    Collections.addAll(intListList, intList, null);
    GenericRecord r = serializeAndDeserialize(field, "listOfNullableLists", intListList);
    Object result = r.get("listOfNullableLists");
    assertNotSame(intListList, result);
    assertEquals(intListList, result);
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.junit.Test)

Example 7 with GenericRecord

use of org.apache.avro.generic.GenericRecord in project hive by apache.

the class TestAvroSerializer method canSerializeNullableMaps.

@Test
public void canSerializeNullableMaps() throws SerDeException, IOException {
    String field = "{ \"name\":\"nullableMap\", \"type\": [\"null\", " + "{\"type\":\"map\", \"values\":\"boolean\"}] }";
    Map<String, Boolean> m = new HashMap<String, Boolean>();
    m.put("yes", true);
    m.put("no", false);
    GenericRecord r = serializeAndDeserialize(field, "nullableMap", m);
    Object result = r.get("nullableMap");
    assertNotSame(m, result);
    assertEquals(m, result);
    r = serializeAndDeserialize(field, "nullableMap", null);
    assertNull(r.get("nullableMap"));
}
Also used : HashMap(java.util.HashMap) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.junit.Test)

Example 8 with GenericRecord

use of org.apache.avro.generic.GenericRecord in project hive by apache.

the class TestAvroSerializer method canSerializeMapsWithNullableComplexValues.

@Test
public void canSerializeMapsWithNullableComplexValues() throws SerDeException, IOException {
    String field = "{ \"name\":\"mapWithNullableLists\", \"type\": " + "{\"type\":\"map\", \"values\": [\"null\", " + "{\"type\": \"array\", \"items\": \"int\"}]} }";
    Map<String, List<Integer>> m = new HashMap<String, List<Integer>>();
    List<Integer> intList = new ArrayList<Integer>();
    Collections.addAll(intList, 1, 2, 3);
    m.put("list", intList);
    m.put("null", null);
    GenericRecord r = serializeAndDeserialize(field, "mapWithNullableLists", m);
    Object result = r.get("mapWithNullableLists");
    assertNotSame(m, result);
    assertEquals(m, result);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.junit.Test)

Example 9 with GenericRecord

use of org.apache.avro.generic.GenericRecord 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 10 with GenericRecord

use of org.apache.avro.generic.GenericRecord in project hive by apache.

the class TestAvroSerializer method serializeAndDeserialize.

/**
   * Verify that we can serialize an avro value by taking one, running it through
   * the deser process and then serialize it again.
   */
private GenericRecord serializeAndDeserialize(String recordValue, String fieldName, Object fieldValue) throws SerDeException, IOException {
    Schema s = buildSchema(recordValue);
    GenericData.Record r = new GenericData.Record(s);
    r.put(fieldName, fieldValue);
    AvroSerializer as = new AvroSerializer();
    AvroDeserializer ad = new AvroDeserializer();
    AvroObjectInspectorGenerator aoig = new AvroObjectInspectorGenerator(s);
    ObjectInspector oi = aoig.getObjectInspector();
    List<String> columnNames = aoig.getColumnNames();
    List<TypeInfo> columnTypes = aoig.getColumnTypes();
    AvroGenericRecordWritable agrw = Utils.serializeAndDeserializeRecord(r);
    Object obj = ad.deserialize(columnNames, columnTypes, agrw, s);
    Writable result = as.serialize(obj, oi, columnNames, columnTypes, s);
    assertTrue(result instanceof AvroGenericRecordWritable);
    GenericRecord r2 = ((AvroGenericRecordWritable) result).getRecord();
    assertEquals(s, r2.getSchema());
    return r2;
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) Schema(org.apache.avro.Schema) Writable(org.apache.hadoop.io.Writable) GenericData(org.apache.avro.generic.GenericData) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) GenericRecord(org.apache.avro.generic.GenericRecord) GenericRecord(org.apache.avro.generic.GenericRecord)

Aggregations

GenericRecord (org.apache.avro.generic.GenericRecord)262 Schema (org.apache.avro.Schema)101 Test (org.junit.Test)80 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)46 File (java.io.File)35 IOException (java.io.IOException)34 GenericData (org.apache.avro.generic.GenericData)30 GenericDatumReader (org.apache.avro.generic.GenericDatumReader)30 ArrayList (java.util.ArrayList)29 ByteArrayOutputStream (java.io.ByteArrayOutputStream)27 DataFileWriter (org.apache.avro.file.DataFileWriter)20 HashMap (java.util.HashMap)19 ByteBuffer (java.nio.ByteBuffer)18 BinaryEncoder (org.apache.avro.io.BinaryEncoder)17 Field (org.apache.avro.Schema.Field)14 DataFileStream (org.apache.avro.file.DataFileStream)14 GenericRecordBuilder (org.apache.avro.generic.GenericRecordBuilder)14 Utf8 (org.apache.avro.util.Utf8)14 Encoder (org.apache.avro.io.Encoder)12 DatasetRepository (com.cloudera.cdk.data.DatasetRepository)11