@@ -61,6 +61,18 @@ public class DataTranslator implements DataTranslatorContext
61
61
{
62
62
protected DataTranslationOptions _dataTranslationOptions ;
63
63
64
+ public static void backFillMissingDeafultInDataMap (DataMap map , RecordDataSchema dataSchema ) {
65
+ for (RecordDataSchema .Field field : dataSchema .getFields ()) {
66
+ DataSchema fieldType = field .getType ();
67
+ if ( fieldType .getDereferencedType () != DataSchema .Type .ARRAY && fieldType .getDereferencedType () != DataSchema .Type .MAP ) {
68
+ continue ;
69
+ }
70
+ if (field .getDefault () != null && (!map .containsKey (field .getName ()) || map .get (field .getName ()) == null )) {
71
+ map .put (field .getName (), field .getDefault ());
72
+ }
73
+ }
74
+ }
75
+
64
76
/**
65
77
* Convert the given {@link DataMap} conforming to the provided {@link RecordDataSchema} to a {@link GenericRecord}.
66
78
*
@@ -74,6 +86,7 @@ public class DataTranslator implements DataTranslatorContext
74
86
*/
75
87
public static GenericRecord dataMapToGenericRecord (DataMap map , RecordDataSchema dataSchema ) throws DataTranslationException
76
88
{
89
+ backFillMissingDeafultInDataMap (map , dataSchema );
77
90
Schema avroSchema = SchemaTranslator .dataToAvroSchema (dataSchema );
78
91
return dataMapToGenericRecord (map , dataSchema , avroSchema , null );
79
92
}
@@ -94,6 +107,7 @@ public static GenericRecord dataMapToGenericRecord(DataMap map, RecordDataSchema
94
107
DataMapToAvroRecordTranslationOptions options )
95
108
throws DataTranslationException
96
109
{
110
+ backFillMissingDeafultInDataMap (map , dataSchema );
97
111
Schema avroSchema = SchemaTranslator .dataToAvroSchema (dataSchema );
98
112
return dataMapToGenericRecord (map , dataSchema , avroSchema , options );
99
113
}
@@ -102,6 +116,7 @@ public static GenericRecord dataMapToGenericRecord(DataMap map, RecordDataSchema
102
116
DataMapToAvroRecordTranslationOptions options )
103
117
throws DataTranslationException
104
118
{
119
+ backFillMissingDeafultInDataMap (map , dataSchema );
105
120
DataMapToGenericRecordTranslator translator = new DataMapToGenericRecordTranslator (options );
106
121
try
107
122
{
@@ -116,6 +131,7 @@ public static GenericRecord dataMapToGenericRecord(DataMap map, RecordDataSchema
116
131
117
132
public static <T extends SpecificRecordBase > T dataMapToSpecificRecord (DataMap map , RecordDataSchema dataSchema ,
118
133
Schema avroSchema ) throws DataTranslationException {
134
+ backFillMissingDeafultInDataMap (map , dataSchema );
119
135
DataMapToSpecificRecordTranslator translator = new DataMapToSpecificRecordTranslator ();
120
136
try {
121
137
T avroRecord = translator .translate (map , dataSchema , avroSchema );
@@ -145,6 +161,7 @@ public static <T extends SpecificRecordBase> T dataMapToSpecificRecord(DataMap m
145
161
*/
146
162
public static GenericRecord dataMapToGenericRecord (DataMap map , RecordDataSchema dataSchema , Schema avroSchema ) throws DataTranslationException
147
163
{
164
+ backFillMissingDeafultInDataMap (map , dataSchema );
148
165
return dataMapToGenericRecord (map , dataSchema , avroSchema , null );
149
166
}
150
167
0 commit comments