in odps-sqoop/src/java/org/apache/sqoop/avro/AvroUtil.java [131:198]
public static Object fromAvro(Object avroObject, Schema schema, String type) {
if (avroObject == null) {
return null;
}
switch (schema.getType()) {
case NULL:
return null;
case BOOLEAN:
case INT:
case FLOAT:
case DOUBLE:
return avroObject;
case LONG:
if (type.equals(DATE_TYPE)) {
return new Date((Long) avroObject);
} else if (type.equals(TIME_TYPE)) {
return new Time((Long) avroObject);
} else if (type.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());
if (type.equals(BLOB_REF_TYPE)) {
// TODO: Should convert BytesWritable to BlobRef properly. (SQOOP-991)
throw new UnsupportedOperationException("BlobRef not supported");
}
return bw;
case STRING:
if (type.equals(BIG_DECIMAL_TYPE)) {
return new BigDecimal(avroObject.toString());
} else if (type.equals(DATE_TYPE)) {
return Date.valueOf(avroObject.toString());
} else if (type.equals(TIME_TYPE)) {
return Time.valueOf(avroObject.toString());
} else if (type.equals(TIMESTAMP_TYPE)) {
return Timestamp.valueOf(avroObject.toString());
}
return avroObject.toString();
case ENUM:
return avroObject.toString();
case UNION:
List<Schema> types = schema.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, type);
} else if (s2.getType() == Schema.Type.NULL) {
return fromAvro(avroObject, s1, type);
} 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 "
+ schema.getType());
}
}