Search in sources :

Example 16 with DataConversionException

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);
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) Map(java.util.Map) SchemaConversionException(org.apache.gobblin.converter.SchemaConversionException) DataConversionException(org.apache.gobblin.converter.DataConversionException) Test(org.testng.annotations.Test)

Example 17 with DataConversionException

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);
}
Also used : SingleRecordIterable(org.apache.gobblin.converter.SingleRecordIterable) JsonObject(com.google.gson.JsonObject) Time(java.sql.Time) DataConversionException(org.apache.gobblin.converter.DataConversionException) Timestamp(java.sql.Timestamp) Date(java.sql.Date)

Aggregations

DataConversionException (org.apache.gobblin.converter.DataConversionException)17 IOException (java.io.IOException)7 SingleRecordIterable (org.apache.gobblin.converter.SingleRecordIterable)7 JsonObject (com.google.gson.JsonObject)6 GenericRecord (org.apache.avro.generic.GenericRecord)5 SchemaConversionException (org.apache.gobblin.converter.SchemaConversionException)5 JsonElement (com.google.gson.JsonElement)4 Map (java.util.Map)3 Schema (org.apache.avro.Schema)3 CopyNotSupportedException (org.apache.gobblin.fork.CopyNotSupportedException)2 HiveMetastoreClientPool (org.apache.gobblin.hive.HiveMetastoreClientPool)2 IMetaStoreClient (org.apache.hadoop.hive.metastore.IMetaStoreClient)2 NoSuchObjectException (org.apache.hadoop.hive.metastore.api.NoSuchObjectException)2 Table (org.apache.hadoop.hive.metastore.api.Table)2 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)2 Partition (org.apache.hadoop.hive.ql.metadata.Partition)2 TException (org.apache.thrift.TException)2 RawJsonDocument (com.couchbase.client.java.document.RawJsonDocument)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 JsonArray (com.google.gson.JsonArray)1