Search in sources :

Example 11 with JsonSchema

use of org.apache.gobblin.converter.json.JsonSchema in project incubator-gobblin by apache.

the class JsonElementConversionFactoryTest method schemaWithMapValuesAsJsonArray.

@Test(expectedExceptions = IllegalStateException.class)
public void schemaWithMapValuesAsJsonArray() throws Exception {
    String testName = "schemaWithMapValuesAsJsonArray";
    JsonObject schema = getSchemaData(testName).getAsJsonObject();
    new RecordConverter(new JsonSchema(schema), state, buildNamespace(state.getExtract().getNamespace(), "something"));
}
Also used : RecordConverter(org.apache.gobblin.converter.avro.JsonElementConversionFactory.RecordConverter) JsonSchema(org.apache.gobblin.converter.json.JsonSchema) JsonObject(com.google.gson.JsonObject) Test(org.testng.annotations.Test)

Example 12 with JsonSchema

use of org.apache.gobblin.converter.json.JsonSchema in project incubator-gobblin by apache.

the class JsonElementConversionFactory method getConvertor.

/**
 * Backward Compatible form of {@link JsonElementConverter#getConvertor(JsonSchema, String, WorkUnitState)}
 * @param fieldName
 * @param fieldType
 * @param schemaNode
 * @param state
 * @param nullable
 * @return
 * @throws UnsupportedDateTypeException
 */
public static JsonElementConverter getConvertor(String fieldName, String fieldType, JsonObject schemaNode, WorkUnitState state, boolean nullable) throws UnsupportedDateTypeException {
    if (!schemaNode.has(COLUMN_NAME_KEY)) {
        schemaNode.addProperty(COLUMN_NAME_KEY, fieldName);
    }
    if (!schemaNode.has(DATA_TYPE_KEY)) {
        schemaNode.add(DATA_TYPE_KEY, new JsonObject());
    }
    JsonObject dataType = schemaNode.get(DATA_TYPE_KEY).getAsJsonObject();
    if (!dataType.has(TYPE_KEY)) {
        dataType.addProperty(TYPE_KEY, fieldType);
    }
    if (!schemaNode.has(IS_NULLABLE_KEY)) {
        schemaNode.addProperty(IS_NULLABLE_KEY, nullable);
    }
    JsonSchema schema = new JsonSchema(schemaNode);
    return getConvertor(schema, null, state);
}
Also used : JsonSchema(org.apache.gobblin.converter.json.JsonSchema) JsonObject(com.google.gson.JsonObject)

Example 13 with JsonSchema

use of org.apache.gobblin.converter.json.JsonSchema in project incubator-gobblin by apache.

the class JsonIntermediateToAvroConverter method convertSchema.

@Override
public Schema convertSchema(JsonArray schema, WorkUnitState workUnit) throws SchemaConversionException {
    try {
        JsonSchema jsonSchema = new JsonSchema(schema);
        jsonSchema.setColumnName(workUnit.getExtract().getTable());
        recordConverter = new RecordConverter(jsonSchema, workUnit, workUnit.getExtract().getNamespace());
    } catch (UnsupportedDateTypeException e) {
        throw new SchemaConversionException(e);
    }
    Schema recordSchema = recordConverter.schema();
    if (workUnit.getPropAsBoolean(CONVERTER_AVRO_NULLIFY_FIELDS_ENABLED, DEFAULT_CONVERTER_AVRO_NULLIFY_FIELDS_ENABLED)) {
        return this.generateSchemaWithNullifiedField(workUnit, recordSchema);
    }
    return recordSchema;
}
Also used : SchemaConversionException(org.apache.gobblin.converter.SchemaConversionException) RecordConverter(org.apache.gobblin.converter.avro.JsonElementConversionFactory.RecordConverter) JsonSchema(org.apache.gobblin.converter.json.JsonSchema) JsonSchema(org.apache.gobblin.converter.json.JsonSchema) Schema(org.apache.avro.Schema)

Example 14 with JsonSchema

use of org.apache.gobblin.converter.json.JsonSchema in project incubator-gobblin by apache.

the class JsonElementConversionFactoryTest method schemaWithMapValuesAsJsonNull.

@Test(expectedExceptions = UnsupportedOperationException.class)
public void schemaWithMapValuesAsJsonNull() throws Exception {
    String testName = "schemaWithMapValuesAsJsonNull";
    JsonObject schema = getSchemaData(testName).getAsJsonObject();
    new RecordConverter(new JsonSchema(schema), state, buildNamespace(state.getExtract().getNamespace(), "something"));
}
Also used : RecordConverter(org.apache.gobblin.converter.avro.JsonElementConversionFactory.RecordConverter) JsonSchema(org.apache.gobblin.converter.json.JsonSchema) JsonObject(com.google.gson.JsonObject) Test(org.testng.annotations.Test)

Example 15 with JsonSchema

use of org.apache.gobblin.converter.json.JsonSchema in project incubator-gobblin by apache.

the class JsonElementConversionFactoryTest method schemaWithRecordIsNullable.

@Test
public void schemaWithRecordIsNullable() throws Exception {
    String testName = "schemaWithRecordIsNullable";
    JsonObject schema = getSchemaData(testName).getAsJsonObject();
    JsonArray expected = getExpectedSchema(testName).getAsJsonArray();
    RecordConverter converter = new RecordConverter(new JsonSchema(schema), state, buildNamespace(state.getExtract().getNamespace(), "something"));
    Assert.assertEquals(avroSchemaToJsonElement(converter), expected);
}
Also used : JsonArray(com.google.gson.JsonArray) RecordConverter(org.apache.gobblin.converter.avro.JsonElementConversionFactory.RecordConverter) JsonSchema(org.apache.gobblin.converter.json.JsonSchema) JsonObject(com.google.gson.JsonObject) Test(org.testng.annotations.Test)

Aggregations

JsonSchema (org.apache.gobblin.converter.json.JsonSchema)26 JsonObject (com.google.gson.JsonObject)25 Test (org.testng.annotations.Test)24 RecordConverter (org.apache.gobblin.converter.avro.JsonElementConversionFactory.RecordConverter)11 JsonArray (com.google.gson.JsonArray)7 ArrayConverter (org.apache.gobblin.converter.avro.JsonElementConversionFactory.ArrayConverter)5 MapConverter (org.apache.gobblin.converter.avro.JsonElementConversionFactory.MapConverter)5 UnionConverter (org.apache.gobblin.converter.avro.JsonElementConversionFactory.UnionConverter)2 Schema (org.apache.avro.Schema)1 SchemaConversionException (org.apache.gobblin.converter.SchemaConversionException)1 EnumConverter (org.apache.gobblin.converter.avro.JsonElementConversionFactory.EnumConverter)1 StringConverter (org.apache.gobblin.converter.avro.JsonElementConversionFactory.StringConverter)1