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);
}
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"));
}
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);
}
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"));
}
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;
}
Aggregations