[GOBBLIN-223][GOBBLIN-177] Changed CsvToJsonConverter to throw DataConversionException
Closes from aditya1105/csvConverter
@@ -55,24 +55,20 @@ public class CsvToJsonConverter extends Converter<String, JsonArray, String, Jso
* Takes in a record with format String and splits the data based on SOURCE_SCHEMA_DELIMITER
* Uses the inputSchema and the split record to convert the record to a JsonObject
* @return a JsonObject representing the record
- * @throws IOException
+ * @throws DataConversionException
*/
@Override
public Iterable<JsonObject> convertRecord(JsonArray outputSchema, String inputRecord, WorkUnitState workUnit)
throws DataConversionException {
+ try {
String strDelimiter = workUnit.getProp(ConfigurationKeys.CONVERTER_CSV_TO_JSON_DELIMITER);
if (Strings.isNullOrEmpty(strDelimiter)) {
throw new IllegalArgumentException("Delimiter cannot be empty");
}
InputStreamCSVReader reader = new InputStreamCSVReader(inputRecord, strDelimiter.charAt(0),
- workUnit.getProp(ConfigurationKeys.CONVERTER_CSV_TO_JSON_ENCLOSEDCHAR,
- ConfigurationKeys.DEFAULT_CONVERTER_CSV_TO_JSON_ENCLOSEDCHAR).charAt(0));
+ workUnit.getProp(ConfigurationKeys.CONVERTER_CSV_TO_JSON_ENCLOSEDCHAR, ConfigurationKeys.DEFAULT_CONVERTER_CSV_TO_JSON_ENCLOSEDCHAR).charAt(0));
List<String> recordSplit;
- try {
recordSplit = Lists.newArrayList(reader.splitRecord());
- } catch (IOException e) {
- throw new DataConversionException(e);
- }
JsonObject outputRecord = new JsonObject();
for (int i = 0; i < outputSchema.size(); i++) {
@@ -82,8 +78,7 @@ public class CsvToJsonConverter extends Converter<String, JsonArray, String, Jso
} else if (recordSplit.get(i).isEmpty() || recordSplit.get(i).toLowerCase().equals(NULL)) {
outputRecord.add(outputSchema.get(i).getAsJsonObject().get("columnName").getAsString(), JsonNull.INSTANCE);
} else {
- outputRecord.addProperty(outputSchema.get(i).getAsJsonObject().get("columnName").getAsString(),
- recordSplit.get(i));
+ outputRecord.addProperty(outputSchema.get(i).getAsJsonObject().get("columnName").getAsString(), recordSplit.get(i));
}
} else {
outputRecord.add(outputSchema.get(i).getAsJsonObject().get("columnName").getAsString(), JsonNull.INSTANCE);
@@ -91,5 +86,8 @@ public class CsvToJsonConverter extends Converter<String, JsonArray, String, Jso
}
return new SingleRecordIterable<>(outputRecord);
+ } catch (Exception e) {
+ throw new DataConversionException(e);
+ }
}
}
