use of org.apache.gobblin.converter.DataConversionException in project incubator-gobblin by apache.
the class JsonStringToJsonIntermediateConverterTest method testAllCases.
@Test
public void testAllCases() throws DataConversionException {
for (Map.Entry<String, JsonElement> keyset : testJsonData.entrySet()) {
JsonArray testData = keyset.getValue().getAsJsonArray();
JsonObject json = testData.get(0).getAsJsonObject();
JsonArray schema = testData.get(1).getAsJsonArray();
JsonObject expected = testData.get(2).getAsJsonObject();
JsonObject result = null;
try {
result = parseJsonObject(json, schema);
} catch (Exception e) {
e.printStackTrace();
assertEquals("Test case failed : " + keyset.getKey(), "No exception", e.getMessage());
}
assertEquals("Test case failed : " + keyset.getKey(), expected, result);
}
}
use of org.apache.gobblin.converter.DataConversionException in project incubator-gobblin by apache.
the class AvroToJdbcEntryConverter method convertRecord.
@Override
public Iterable<JdbcEntryData> convertRecord(JdbcEntrySchema outputSchema, GenericRecord record, WorkUnitState workUnit) throws DataConversionException {
if (LOG.isDebugEnabled()) {
LOG.debug("Converting " + record);
}
List<JdbcEntryDatum> jdbcEntryData = Lists.newArrayList();
for (JdbcEntryMetaDatum entry : outputSchema) {
final String jdbcColName = entry.getColumnName();
final JdbcType jdbcType = entry.getJdbcType();
String avroColName = convertJdbcColNameToAvroColName(jdbcColName);
final Object val = avroRecordValueGet(record, AVRO_RECORD_LEVEL_SPLITTER.split(avroColName).iterator());
if (val == null) {
jdbcEntryData.add(new JdbcEntryDatum(jdbcColName, null));
continue;
}
if (!JDBC_SUPPORTED_TYPES.contains(jdbcType)) {
throw new DataConversionException("Unsupported JDBC type detected " + jdbcType);
}
switch(jdbcType) {
case VARCHAR:
jdbcEntryData.add(new JdbcEntryDatum(jdbcColName, val.toString()));
continue;
case INTEGER:
case BOOLEAN:
case BIGINT:
case FLOAT:
case DOUBLE:
jdbcEntryData.add(new JdbcEntryDatum(jdbcColName, val));
continue;
case DATE:
jdbcEntryData.add(new JdbcEntryDatum(jdbcColName, new Date((long) val)));
continue;
case TIME:
jdbcEntryData.add(new JdbcEntryDatum(jdbcColName, new Time((long) val)));
continue;
case TIMESTAMP:
jdbcEntryData.add(new JdbcEntryDatum(jdbcColName, new Timestamp((long) val)));
continue;
default:
throw new DataConversionException(jdbcType + " is not supported");
}
}
JdbcEntryData converted = new JdbcEntryData(jdbcEntryData);
if (LOG.isDebugEnabled()) {
LOG.debug("Converted data into " + converted);
}
return new SingleRecordIterable<>(converted);
}
Aggregations