org.apache.avro.Schema.setFields() - java examples

Here are the examples of the java api org.apache.avro.Schema.setFields() taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

155 Examples 7

19 View Complete Implementation : AvroFlattener.java
Copyright Apache License 2.0
Author : apache
/**
 * Flatten Record schema
 * @param schema Record Schema to flatten
 * @param shouldPopulateLineage If lineage information should be tagged in the field, this is true when we are
 *                              un-nesting fields
 * @param flattenComplexTypes Flatten complex types recursively other than Record and Option
 * @return Flattened Record Schema
 */
private Schema flattenRecord(Schema schema, boolean shouldPopulateLineage, boolean flattenComplexTypes) {
    Preconditions.checkNotNull(schema);
    Preconditions.checkArgument(Schema.Type.RECORD.equals(schema.getType()));
    Schema flattenedSchema;
    List<Schema.Field> flattenedFields = new ArrayList<>();
    if (schema.getFields().size() > 0) {
        for (Schema.Field oldField : schema.getFields()) {
            List<Schema.Field> newFields = flattenField(oldField, ImmutableList.<String>of(), shouldPopulateLineage, flattenComplexTypes, Optional.<Schema>absent());
            if (null != newFields && newFields.size() > 0) {
                flattenedFields.addAll(newFields);
            }
        }
    }
    flattenedSchema = Schema.createRecord(schema.getName(), schema.getDoc(), schema.getNamespace(), schema.isError());
    flattenedSchema.setFields(flattenedFields);
    return flattenedSchema;
}

19 View Complete Implementation : AvroUtils.java
Copyright Apache License 2.0
Author : linkedin
public static Schema getSchema(SeekableInput input) throws IOException {
    DatumReader<GenericRecord> datumReader = new GenericDatumReader<GenericRecord>();
    DataFileReader<GenericRecord> dataFileReader = new DataFileReader<GenericRecord>(input, datumReader);
    Schema schema = dataFileReader.getSchema();
    if (PadDefaultNullsToSchema) {
        // a list of "cloned" fields, with optional default value set to null
        ArrayList<Field> paddedFields = new ArrayList<Field>();
        for (Field field : schema.getFields()) {
            // should this field be padded?
            boolean needsNullPadding = // the field has nested schema
            (field.schema() != null) && // the nested schema is UNION
            (field.schema().getType().equals(Type.UNION)) && // the first element of union is NULL type
            (field.schema().getTypes().get(0).getType().equals(Type.NULL));
            JsonNode defValue = needsNullPadding ? NullNode.getInstance() : field.defaultValue();
            Field f = new Field(field.name(), field.schema(), field.doc(), defValue);
            paddedFields.add(f);
        }
        schema = Schema.createRecord(schema.getName(), schema.getDoc(), schema.getNamespace(), schema.isError());
        schema.setFields(paddedFields);
    }
    return schema;
}

18 View Complete Implementation : AvroUtils.java
Copyright Apache License 2.0
Author : kite-sdk
public static AvroKeySchema mergeSpecificStringTypes(Clreplaced<? extends SpecificRecord> specificClreplaced, AvroKeySchema keySchema) {
    Schema schemaField;
    try {
        schemaField = (Schema) specificClreplaced.getField("SCHEMA$").get(null);
    } catch (IllegalArgumentException e) {
        throw new DatasetException(e);
    } catch (SecurityException e) {
        throw new DatasetException(e);
    } catch (IllegalAccessException e) {
        throw new DatasetException(e);
    } catch (NoSuchFieldException e) {
        throw new DatasetException(e);
    }
    // Ensure schema is limited to keySchema's fields. The clreplaced may have more
    // fields
    // in the case that the enreplacedy is being used as a key.
    List<Field> fields = Lists.newArrayList();
    ParreplacedionStrategy strategy = keySchema.getParreplacedionStrategy();
    for (Schema.Field field : keySchema.getAvroSchema().getFields()) {
        String sourceName = Accessor.getDefault().getParreplacedioner(strategy, field.name()).getSourceName();
        fields.add(copy(schemaField.getField(sourceName)));
    }
    Schema schema = Schema.createRecord(keySchema.getAvroSchema().getName(), keySchema.getAvroSchema().getDoc(), keySchema.getAvroSchema().getNamespace(), keySchema.getAvroSchema().isError());
    schema.setFields(fields);
    return new AvroKeySchema(schema, keySchema.getParreplacedionStrategy());
}

18 View Complete Implementation : TSalesforceOutputProperties.java
Copyright Apache License 2.0
Author : Talend
private Schema newSchema(Schema metadataSchema, String newSchemaName, List<Schema.Field> moreFields) {
    Schema newSchema = Schema.createRecord(newSchemaName, metadataSchema.getDoc(), metadataSchema.getNamespace(), metadataSchema.isError());
    newSchema.setFields(moreFields);
    for (Map.Entry<String, Object> entry : metadataSchema.getObjectProps().entrySet()) {
        newSchema.addProp(entry.getKey(), entry.getValue());
    }
    return newSchema;
}

18 View Complete Implementation : TestAvroExport.java
Copyright Apache License 2.0
Author : dkhadoop
private Schema buildAvroSchema(ColumnGenerator... extraCols) {
    List<Field> fields = new ArrayList<Field>();
    fields.add(buildAvroField("id", Schema.Type.INT));
    fields.add(buildAvroField("msg", Schema.Type.STRING));
    int colNum = 0;
    for (ColumnGenerator gen : extraCols) {
        if (gen.getColumnAvroSchema() != null) {
            fields.add(buildAvroField(forIdx(colNum++), gen.getColumnAvroSchema()));
        }
    }
    Schema schema = Schema.createRecord("myschema", null, null, false);
    schema.setFields(fields);
    return schema;
}

18 View Complete Implementation : AvroStoreBusHttpServer.java
Copyright Apache License 2.0
Author : jingwei
/**
 * Creates the Avro schema for the Person store.
 */
static Schema createPersonSchema() {
    List<Field> fields = new ArrayList<Field>();
    fields.add(new Field("id", Schema.create(Type.INT), null, null));
    fields.add(new Field("age", Schema.create(Type.INT), null, null));
    fields.add(new Field("fname", Schema.create(Type.STRING), null, null));
    fields.add(new Field("lname", Schema.create(Type.STRING), null, null));
    Schema schema = Schema.createRecord("Person", null, "avro.test", false);
    schema.setFields(fields);
    return schema;
}

18 View Complete Implementation : TestAvroSchemaConverter.java
Copyright Apache License 2.0
Author : apache
@Test
public void testOptionalFields() throws Exception {
    Schema schema = Schema.createRecord("record1", null, null, false);
    Schema optionalInt = optional(Schema.create(INT));
    schema.setFields(Collections.singletonList(new Schema.Field("myint", optionalInt, null, JsonProperties.NULL_VALUE)));
    testRoundTripConversion(schema, "message record1 {\n" + "  optional int32 myint;\n" + "}\n");
}

18 View Complete Implementation : PigSchema2Avro.java
Copyright Apache License 2.0
Author : linkedin
/**
 * Convert pig data to Avro record
 */
protected static Schema convertRecord(ResourceFieldSchema[] pigFields, boolean nullable) throws IOException {
    AvroStorageLog.funcCall("convertRecord");
    // Type name is required for Avro record
    String typeName = getRecordName();
    Schema outSchema = Schema.createRecord(typeName, null, null, false);
    List<Schema.Field> outFields = new ArrayList<Schema.Field>();
    for (int i = 0; i < pigFields.length; i++) {
        /* get schema */
        Schema fieldSchema = convert(pigFields[i], nullable);
        /* get field name of output */
        String outname = pigFields[i].getName();
        if (outname == null)
            // field name cannot be null
            outname = FIELD_NAME + "_" + i;
        /* get doc of output */
        String desc = pigFields[i].getDescription();
        outFields.add(new Field(outname, fieldSchema, desc, null));
    }
    outSchema.setFields(outFields);
    return outSchema;
}

18 View Complete Implementation : TFilterRowProperties.java
Copyright Apache License 2.0
Author : Talend
private Schema newSchema(Schema metadataSchema, String newSchemaName, List<Schema.Field> moreFields) {
    Schema newSchema = Schema.createRecord(newSchemaName, metadataSchema.getDoc(), metadataSchema.getNamespace(), metadataSchema.isError());
    // TODO duplicate with salesforce, make it to a common one?
    List<Schema.Field> copyFieldList = new ArrayList<>();
    for (Schema.Field se : metadataSchema.getFields()) {
        Schema.Field field = new Schema.Field(se.name(), se.schema(), se.doc(), se.defaultVal(), se.order());
        field.getObjectProps().putAll(se.getObjectProps());
        copyFieldList.add(field);
    }
    copyFieldList.addAll(moreFields);
    newSchema.setFields(copyFieldList);
    for (Map.Entry<String, Object> entry : metadataSchema.getObjectProps().entrySet()) {
        newSchema.addProp(entry.getKey(), entry.getValue());
    }
    return newSchema;
}

18 View Complete Implementation : TestAvroExport.java
Copyright Apache License 2.0
Author : aliyun
public void testAvroRecordsNotSupported() throws IOException, SQLException {
    String[] argv = {};
    final int TOTAL_RECORDS = 1;
    Schema schema = Schema.createRecord("nestedrecord", null, null, false);
    schema.setFields(Lists.newArrayList(buildAvroField("myint", Schema.Type.INT)));
    GenericRecord record = new GenericData.Record(schema);
    record.put("myint", 100);
    // DB type is not used so can be anything:
    ColumnGenerator gen = colGenerator(record, schema, null, "VARCHAR(64)");
    createAvroFile(0, TOTAL_RECORDS, gen);
    createTable(gen);
    try {
        runExport(getArgv(true, 10, 10, newStrArray(argv, "-m", "" + 1)));
        fail("Avro records can not be exported.");
    } catch (Exception e) {
        // expected
        replacedertTrue(true);
    }
}

18 View Complete Implementation : TestUtils.java
Copyright Apache License 2.0
Author : Talend
public static Schema makeRecordSchema(Schema sourceSchema, Collection<String> targetFieldNames) {
    Schema targetSchema = Schema.createRecord(sourceSchema.getName(), sourceSchema.getDoc(), sourceSchema.getNamespace(), sourceSchema.isError());
    List<Schema.Field> copyFieldList = new ArrayList<>();
    for (String targetFieldName : targetFieldNames) {
        Schema.Field field = NetSuiteDatasetRuntimeImpl.getNsFieldByName(sourceSchema, targetFieldName);
        if (field != null) {
            Schema.Field targetField = NetSuiteDatasetRuntimeImpl.copyField(field);
            copyFieldList.add(targetField);
        }
    }
    targetSchema.setFields(copyFieldList);
    for (Map.Entry<String, Object> entry : sourceSchema.getObjectProps().entrySet()) {
        targetSchema.addProp(entry.getKey(), entry.getValue());
    }
    return targetSchema;
}

18 View Complete Implementation : TestSpecificInputOutputFormat.java
Copyright Apache License 2.0
Author : apache
@Test
public void testReadWriteChangedCar() throws Exception {
    final Job job = new Job(conf, "read changed/short");
    job.setInputFormatClreplaced(AvroParquetInputFormat.clreplaced);
    AvroParquetInputFormat.setInputPaths(job, parquetPath);
    // Test push-down predicates by using an electric car filter
    AvroParquetInputFormat.setUnboundRecordFilter(job, ElectricCarFilter.clreplaced);
    // Test schema projection by dropping the engine, year, and vin (like ShortCar),
    // but making make optional (unlike ShortCar)
    Schema projection = Schema.createRecord(Car.SCHEMA$.getName(), Car.SCHEMA$.getDoc(), Car.SCHEMA$.getNamespace(), false);
    List<Schema.Field> fields = Lists.newArrayList();
    for (Schema.Field field : Car.SCHEMA$.getFields()) {
        // No make!
        if ("engine".equals(field.name()) || "year".equals(field.name()) || "vin".equals(field.name())) {
            fields.add(new Schema.Field(field.name(), field.schema(), field.doc(), field.defaultVal(), field.order()));
        }
    }
    projection.setFields(fields);
    AvroParquetInputFormat.setRequestedProjection(job, projection);
    AvroParquetInputFormat.setAvroReadSchema(job, ShortCar.SCHEMA$);
    job.setMapperClreplaced(TestSpecificInputOutputFormat.MyMapperShort.clreplaced);
    job.setNumReduceTasks(0);
    job.setOutputFormatClreplaced(AvroParquetOutputFormat.clreplaced);
    AvroParquetOutputFormat.setOutputPath(job, outputPath);
    AvroParquetOutputFormat.setSchema(job, ShortCar.SCHEMA$);
    waitForJob(job);
    final Path mapperOutput = new Path(outputPath.toString(), "part-m-00000.parquet");
    try (final AvroParquetReader<ShortCar> out = new AvroParquetReader<>(mapperOutput)) {
        ShortCar car;
        int lineNumber = 0;
        while ((car = out.read()) != null) {
            // Make sure that predicate push down worked as expected
            // Note we use lineNumber * 2 because of predicate push down
            Car expectedCar = nextRecord(lineNumber * 2);
            // We removed the optional extra field using projection so we shouldn't see it here...
            replacedertNull(car.getMake());
            replacedertEquals(car.getEngine(), expectedCar.getEngine());
            replacedertEquals(car.getYear(), expectedCar.getYear());
            replacedertEquals(car.getVin(), expectedCar.getVin());
            ++lineNumber;
        }
    }
}

18 View Complete Implementation : AvroStoreJoinerHttpServer.java
Copyright Apache License 2.0
Author : jingwei
/**
 * Creates the Avro schema for the Person store.
 */
static Schema createPersonSchema() {
    List<Field> fields = new ArrayList<Field>();
    fields.add(new Field("id", Schema.create(Type.INT), null, null));
    fields.add(new Field("age", Schema.create(Type.INT), null, null));
    fields.add(new Field("fname", Schema.create(Type.STRING), null, null));
    fields.add(new Field("lname", Schema.create(Type.STRING), null, null));
    Schema schema = Schema.createRecord("Person", null, "avro.test", false);
    schema.setFields(fields);
    return schema;
}

18 View Complete Implementation : TestAvroSchemaConverter.java
Copyright Apache License 2.0
Author : apache
@Test
public void testOptionalMapValue() throws Exception {
    Schema schema = Schema.createRecord("record1", null, null, false);
    Schema optionalIntMap = Schema.createMap(optional(Schema.create(INT)));
    schema.setFields(Arrays.asList(new Schema.Field("myintmap", optionalIntMap, null, null)));
    testRoundTripConversion(schema, "message record1 {\n" + "  required group myintmap (MAP) {\n" + "    repeated group map (MAP_KEY_VALUE) {\n" + "      required binary key (UTF8);\n" + "      optional int32 value;\n" + "    }\n" + "  }\n" + "}\n");
}

18 View Complete Implementation : HiveSchemaConverter.java
Copyright Apache License 2.0
Author : kite-sdk
private static Schema convert(LinkedList<String> path, String name, StructTypeInfo type, Collection<String[]> required) {
    List<String> names = type.getAllStructFieldNames();
    List<TypeInfo> types = type.getAllStructFieldTypeInfos();
    Preconditions.checkArgument(names.size() == types.size(), "Cannot convert struct: %s names != %s types", names.size(), types.size());
    List<Schema.Field> fields = Lists.newArrayList();
    for (int i = 0; i < names.size(); i += 1) {
        path.addLast(name);
        fields.add(convertField(path, names.get(i), types.get(i), required));
        path.removeLast();
    }
    Schema recordSchema = Schema.createRecord(name, doc(type), null, false);
    recordSchema.setFields(fields);
    return recordSchema;
}

18 View Complete Implementation : TFileInputDelimitedProperties.java
Copyright Apache License 2.0
Author : Talend
private Schema newSchema(Schema metadataSchema, String newSchemaName, List<Schema.Field> moreFields) {
    Schema newSchema = Schema.createRecord(newSchemaName, metadataSchema.getDoc(), metadataSchema.getNamespace(), metadataSchema.isError());
    // TODO duplicate with salesforce, make it to a common one?
    List<Schema.Field> copyFieldList = new ArrayList<>();
    for (Schema.Field se : metadataSchema.getFields()) {
        Schema.Field field = new Schema.Field(se.name(), se.schema(), se.doc(), se.defaultVal(), se.order());
        field.getObjectProps().putAll(se.getObjectProps());
        for (Map.Entry<String, Object> entry : se.getObjectProps().entrySet()) {
            field.addProp(entry.getKey(), entry.getValue());
        }
        copyFieldList.add(field);
    }
    copyFieldList.addAll(moreFields);
    newSchema.setFields(copyFieldList);
    for (Map.Entry<String, Object> entry : metadataSchema.getObjectProps().entrySet()) {
        newSchema.addProp(entry.getKey(), entry.getValue());
    }
    return newSchema;
}

18 View Complete Implementation : FlattenNestedKeyConverter.java
Copyright Apache License 2.0
Author : apache
@Override
public Schema convertSchema(Schema inputSchema, WorkUnitState workUnit) throws SchemaConversionException {
    // Clear previous state
    fieldNameMap.clear();
    Config config = ConfigUtils.propertiesToConfig(workUnit.getProperties()).getConfig(getClreplaced().getSimpleName());
    List<String> nestedKeys = ConfigUtils.getStringList(config, FIELDS_TO_FLATTEN);
    // No keys need flatten
    if (nestedKeys == null || nestedKeys.size() == 0) {
        return inputSchema;
    }
    List<Field> fields = new ArrayList<>();
    // Clone the existing fields
    for (Field field : inputSchema.getFields()) {
        fields.add(new Field(field.name(), field.schema(), field.doc(), field.defaultValue(), field.order()));
    }
    // Convert each of nested keys into a top level field
    for (String key : nestedKeys) {
        if (!key.contains(FIELD_LOCATION_DELIMITER)) {
            continue;
        }
        String nestedKey = key.trim();
        // Create camel-cased name
        String hyphenizedKey = nestedKey.replace(FIELD_LOCATION_DELIMITER, "-");
        String name = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, hyphenizedKey);
        if (fieldNameMap.containsKey(name)) {
            // Duplicate
            continue;
        }
        fieldNameMap.put(name, nestedKey);
        // Find the field
        Optional<Field> optional = AvroUtils.getField(inputSchema, nestedKey);
        if (!optional.isPresent()) {
            throw new SchemaConversionException("Unable to get field with location: " + nestedKey);
        }
        Field field = optional.get();
        // Make a copy under a new name
        Field copy = new Field(name, field.schema(), field.doc(), field.defaultValue(), field.order());
        fields.add(copy);
    }
    Schema outputSchema = Schema.createRecord(inputSchema.getName(), inputSchema.getDoc(), inputSchema.getNamespace(), inputSchema.isError());
    outputSchema.setFields(fields);
    return outputSchema;
}

18 View Complete Implementation : TestAvroSchemaConverter.java
Copyright Apache License 2.0
Author : apache
@Test
public void testParquetMapWithoutMapKeyValueAnnotation() throws Exception {
    Schema schema = Schema.createRecord("myrecord", null, null, false);
    Schema map = Schema.createMap(Schema.create(INT));
    schema.setFields(Collections.singletonList(new Schema.Field("mymap", map, null, null)));
    String parquetSchema = "message myrecord {\n" + "  required group mymap (MAP) {\n" + "    repeated group map {\n" + "      required binary key (UTF8);\n" + "      required int32 value;\n" + "    }\n" + "  }\n" + "}\n";
    testParquetToAvroConversion(schema, parquetSchema);
    testParquetToAvroConversion(NEW_BEHAVIOR, schema, parquetSchema);
}

18 View Complete Implementation : PartitionCollapsingSchemas.java
Copyright Apache License 2.0
Author : apache
public Schema getReduceOutputSchema() {
    if (_reduceOutputSchema == null) {
        _reduceOutputSchema = Schema.createRecord(_outputSchemaName, null, _outputSchemaNamespace, false);
        List<Field> fields = Arrays.asList(new Field("key", getKeySchema(), null, null), new Field("value", getOutputValueSchema(), null, null));
        _reduceOutputSchema.setFields(fields);
    }
    return _reduceOutputSchema;
}

18 View Complete Implementation : TestAvroSchemaConverter.java
Copyright Apache License 2.0
Author : apache
@Test
public void testUnknownTwoLevelListOfLists() throws Exception {
    // This tests the case where we don't detect a 2-level list by the repeated
    // group's name, but it must be 2-level because the repeated group doesn't
    // contain an optional or repeated element as required for 3-level lists
    Schema listOfLists = optional(Schema.createArray(Schema.createArray(Schema.create(INT))));
    Schema schema = Schema.createRecord("UnknownTwoLevelListInList", null, null, false);
    schema.setFields(Lists.newArrayList(new Schema.Field("listOfLists", listOfLists, null, JsonProperties.NULL_VALUE)));
    System.err.println("Avro schema: " + schema.toString(true));
    // Cannot use round-trip replacedertion because repeated group names differ
    testParquetToAvroConversion(schema, "message UnknownTwoLevelListInList {\n" + "  optional group listOfLists (LIST) {\n" + "    repeated group mylist (LIST) {\n" + "      repeated int32 innerlist;\n" + "    }\n" + "  }\n" + "}");
    // Cannot use round-trip replacedertion because 3-level representation is used
    testParquetToAvroConversion(NEW_BEHAVIOR, schema, "message UnknownTwoLevelListInList {\n" + "  optional group listOfLists (LIST) {\n" + "    repeated group mylist (LIST) {\n" + "      repeated int32 innerlist;\n" + "    }\n" + "  }\n" + "}");
}

18 View Complete Implementation : TestParquetExport.java
Copyright Apache License 2.0
Author : apache
@Test
public void testParquetRecordsNotSupported() throws IOException, SQLException {
    String[] argv = {};
    final int TOTAL_RECORDS = 1;
    Schema schema = Schema.createRecord("nestedrecord", null, null, false);
    schema.setFields(Lists.newArrayList(buildField("myint", Schema.Type.INT)));
    GenericRecord record = new GenericData.Record(schema);
    record.put("myint", 100);
    // DB type is not used so can be anything:
    ColumnGenerator gen = colGenerator(record, schema, null, "VARCHAR(64)");
    createParquetFile(TOTAL_RECORDS, gen);
    createTable(gen);
    thrown.expect(Exception.clreplaced);
    thrown.reportMissingExceptionWithMessage("Expected Exception as Parquet records are not supported");
    runExport(getArgv(true, 10, 10, newStrArray(argv, "-m", "" + 1)));
}

18 View Complete Implementation : AbstractNetSuiteTestBase.java
Copyright Apache License 2.0
Author : Talend
protected Schema getDynamicSchema() {
    Schema emptySchema = Schema.createRecord("dynamic", null, null, false);
    emptySchema.setFields(new ArrayList<Schema.Field>());
    emptySchema = AvroUtils.setIncludeAllFields(emptySchema, true);
    return emptySchema;
}

18 View Complete Implementation : TestSpecificInputOutputFormat.java
Copyright Apache License 2.0
Author : apache
@Test
public void testReadWrite() throws Exception {
    final Job job = new Job(conf, "read");
    job.setInputFormatClreplaced(AvroParquetInputFormat.clreplaced);
    AvroParquetInputFormat.setInputPaths(job, parquetPath);
    // Test push-down predicates by using an electric car filter
    AvroParquetInputFormat.setUnboundRecordFilter(job, ElectricCarFilter.clreplaced);
    // Test schema projection by dropping the optional extras
    Schema projection = Schema.createRecord(Car.SCHEMA$.getName(), Car.SCHEMA$.getDoc(), Car.SCHEMA$.getNamespace(), false);
    List<Schema.Field> fields = Lists.newArrayList();
    for (Schema.Field field : Car.SCHEMA$.getFields()) {
        if (!"optionalExtra".equals(field.name())) {
            fields.add(new Schema.Field(field.name(), field.schema(), field.doc(), field.defaultVal(), field.order()));
        }
    }
    projection.setFields(fields);
    AvroParquetInputFormat.setRequestedProjection(job, projection);
    job.setMapperClreplaced(TestSpecificInputOutputFormat.MyMapper2.clreplaced);
    job.setNumReduceTasks(0);
    job.setOutputFormatClreplaced(AvroParquetOutputFormat.clreplaced);
    AvroParquetOutputFormat.setOutputPath(job, outputPath);
    AvroParquetOutputFormat.setSchema(job, Car.SCHEMA$);
    waitForJob(job);
    final Path mapperOutput = new Path(outputPath.toString(), "part-m-00000.parquet");
    try (final AvroParquetReader<Car> out = new AvroParquetReader<>(mapperOutput)) {
        Car car;
        Car previousCar = null;
        int lineNumber = 0;
        while ((car = out.read()) != null) {
            if (previousCar != null) {
                // Testing reference equality here. The "model" field should be dictionary-encoded.
                replacedertTrue(car.getModel() == previousCar.getModel());
            }
            // Make sure that predicate push down worked as expected
            if (car.getEngine().getType() == EngineType.PETROL) {
                fail("UnboundRecordFilter failed to remove cars with PETROL engines");
            }
            // Note we use lineNumber * 2 because of predicate push down
            Car expectedCar = nextRecord(lineNumber * 2);
            // We removed the optional extra field using projection so we shouldn't
            // see it here...
            expectedCar.setOptionalExtra(null);
            replacedertEquals("line " + lineNumber, expectedCar, car);
            ++lineNumber;
            previousCar = car;
        }
    }
}

18 View Complete Implementation : TestAvroSchemaConverter.java
Copyright Apache License 2.0
Author : apache
@Test
public void testOldThriftListOfLists() throws Exception {
    Schema listOfLists = optional(Schema.createArray(Schema.createArray(Schema.create(INT))));
    Schema schema = Schema.createRecord("ThriftCompatListInList", null, null, false);
    schema.setFields(Lists.newArrayList(new Schema.Field("listOfLists", listOfLists, null, JsonProperties.NULL_VALUE)));
    System.err.println("Avro schema: " + schema.toString(true));
    // Cannot use round-trip replacedertion because repeated group names differ
    testParquetToAvroConversion(schema, "message ThriftCompatListInList {\n" + "  optional group listOfLists (LIST) {\n" + "    repeated group listOfLists_tuple (LIST) {\n" + "      repeated int32 listOfLists_tuple_tuple;\n" + "    }\n" + "  }\n" + "}");
    // Cannot use round-trip replacedertion because 3-level representation is used
    testParquetToAvroConversion(NEW_BEHAVIOR, schema, "message ThriftCompatListInList {\n" + "  optional group listOfLists (LIST) {\n" + "    repeated group listOfLists_tuple (LIST) {\n" + "      repeated int32 listOfLists_tuple_tuple;\n" + "    }\n" + "  }\n" + "}");
}

18 View Complete Implementation : AvroStoreBus2HttpServer.java
Copyright Apache License 2.0
Author : jingwei
/**
 * Creates the Avro schema for the Person store.
 */
static Schema createPersonSchema() {
    List<Field> fields = new ArrayList<Field>();
    fields.add(new Field("id", Schema.create(Type.INT), null, null));
    fields.add(new Field("age", Schema.create(Type.INT), null, null));
    fields.add(new Field("fname", Schema.create(Type.STRING), null, null));
    fields.add(new Field("lname", Schema.create(Type.STRING), null, null));
    Schema schema = Schema.createRecord("Person", null, "avro.test", false);
    schema.setFields(fields);
    return schema;
}

18 View Complete Implementation : ResultSetStringRecordConverter.java
Copyright Apache License 2.0
Author : Talend
/**
 * Creates new actual schema from incoming specification <code>schema</code>
 * Actual schema fields has the same names as specification schema, but they have String type
 * Note, getSchema() will return schema, which differs from specification schema preplaceded to this method
 */
// TODO this one more kind of copySchema() method which should be implemented in Daikon see TDKN-96
@Override
public void setSchema(Schema schema) {
    actualSchema = Schema.createRecord(schema.getName(), schema.getDoc(), schema.getNamespace(), schema.isError());
    List<Schema.Field> stringFields = new ArrayList<>();
    for (Schema.Field specField : schema.getFields()) {
        boolean nullable = AvroUtils.isNullable(specField.schema());
        Schema stringSchema = AvroUtils._string();
        if (nullable) {
            stringSchema = AvroUtils.wrapAsNullable(stringSchema);
        }
        Schema.Field stringField = new Schema.Field(specField.name(), stringSchema, specField.doc(), specField.defaultVal(), specField.order());
        for (Map.Entry<String, Object> entry : specField.getObjectProps().entrySet()) {
            stringField.addProp(entry.getKey(), entry.getValue());
        }
        stringFields.add(stringField);
    }
    actualSchema.setFields(stringFields);
    for (Map.Entry<String, Object> entry : schema.getObjectProps().entrySet()) {
        actualSchema.addProp(entry.getKey(), entry.getValue());
    }
}

18 View Complete Implementation : AbstractTestAvroStoreJoiner.java
Copyright Apache License 2.0
Author : jingwei
/**
 * Creates the Avro schema for the Address store.
 */
static Schema createAddressSchema() {
    List<Field> fields = new ArrayList<Field>();
    fields.add(new Field("id", Schema.create(Type.INT), null, null));
    fields.add(new Field("city", Schema.create(Type.STRING), null, null));
    fields.add(new Field("state", Schema.create(Type.STRING), null, null));
    fields.add(new Field("country", Schema.create(Type.STRING), null, null));
    fields.add(new Field("postal_code", Schema.create(Type.STRING), null, null));
    Schema schema = Schema.createRecord("Address", null, "avro.test", false);
    schema.setFields(fields);
    return schema;
}

18 View Complete Implementation : AvroStoreBus2HttpServer.java
Copyright Apache License 2.0
Author : jingwei
/**
 * Creates the Avro schema for the Address store.
 */
static Schema createAddressSchema() {
    List<Field> fields = new ArrayList<Field>();
    fields.add(new Field("id", Schema.create(Type.INT), null, null));
    fields.add(new Field("city", Schema.create(Type.STRING), null, null));
    fields.add(new Field("state", Schema.create(Type.STRING), null, null));
    fields.add(new Field("country", Schema.create(Type.STRING), null, null));
    fields.add(new Field("postal_code", Schema.create(Type.STRING), null, null));
    Schema schema = Schema.createRecord("Address", null, "avro.test", false);
    schema.setFields(fields);
    return schema;
}

18 View Complete Implementation : TestAvroSchemaConverter.java
Copyright Apache License 2.0
Author : apache
@Test
public void testDecimalBytesType() throws Exception {
    Schema schema = Schema.createRecord("myrecord", null, null, false);
    Schema decimal = LogicalTypes.decimal(9, 2).addToSchema(Schema.create(Schema.Type.BYTES));
    schema.setFields(Collections.singletonList(new Schema.Field("dec", decimal, null, null)));
    testRoundTripConversion(schema, "message myrecord {\n" + "  required binary dec (DECIMAL(9,2));\n" + "}\n");
}

18 View Complete Implementation : AvroStoreBusHttpServer.java
Copyright Apache License 2.0
Author : jingwei
/**
 * Creates the Avro schema for the Address store.
 */
static Schema createAddressSchema() {
    List<Field> fields = new ArrayList<Field>();
    fields.add(new Field("id", Schema.create(Type.INT), null, null));
    fields.add(new Field("city", Schema.create(Type.STRING), null, null));
    fields.add(new Field("state", Schema.create(Type.STRING), null, null));
    fields.add(new Field("country", Schema.create(Type.STRING), null, null));
    fields.add(new Field("postal_code", Schema.create(Type.STRING), null, null));
    Schema schema = Schema.createRecord("Address", null, "avro.test", false);
    schema.setFields(fields);
    return schema;
}

18 View Complete Implementation : TFilterRowTest.java
Copyright Apache License 2.0
Author : Talend
private Schema newSchema(Schema metadataSchema, String newSchemaName, List<Schema.Field> moreFields) {
    Schema newSchema = Schema.createRecord(newSchemaName, metadataSchema.getDoc(), metadataSchema.getNamespace(), metadataSchema.isError());
    // TODO duplicate with salesforce, make it to a common one?
    List<Schema.Field> copyFieldList = new ArrayList<>();
    for (Schema.Field se : metadataSchema.getFields()) {
        Schema.Field field = new Schema.Field(se.name(), se.schema(), se.doc(), se.defaultVal(), se.order());
        field.getObjectProps().putAll(se.getObjectProps());
        for (Map.Entry<String, Object> entry : se.getObjectProps().entrySet()) {
            field.addProp(entry.getKey(), entry.getValue());
        }
        copyFieldList.add(field);
    }
    copyFieldList.addAll(moreFields);
    newSchema.setFields(copyFieldList);
    for (Map.Entry<String, Object> entry : metadataSchema.getObjectProps().entrySet()) {
        newSchema.addProp(entry.getKey(), entry.getValue());
    }
    return newSchema;
}

18 View Complete Implementation : AvroFieldsPickConverter.java
Copyright Apache License 2.0
Author : apache
private static Schema createSchemaHelper(final Schema inputSchema, TrieNode node) {
    List<Field> newFields = Lists.newArrayList();
    for (TrieNode child : node.children.values()) {
        Schema recordSchema = getActualRecord(inputSchema);
        Field innerSrcField = recordSchema.getField(child.val);
        Preconditions.checkNotNull(innerSrcField, child.val + " does not exist under " + recordSchema);
        if (child.children.isEmpty()) {
            // Leaf
            newFields.add(new Field(innerSrcField.name(), innerSrcField.schema(), innerSrcField.doc(), innerSrcField.defaultValue()));
        } else {
            Schema innerSrcSchema = innerSrcField.schema();
            // Recurse of schema
            Schema innerDestSchema = createSchemaHelper(innerSrcSchema, child);
            Field innerDestField = new Field(innerSrcField.name(), innerDestSchema, innerSrcField.doc(), innerSrcField.defaultValue());
            newFields.add(innerDestField);
        }
    }
    if (Type.UNION.equals(inputSchema.getType())) {
        Preconditions.checkArgument(inputSchema.getTypes().size() <= 2, "For union type in nested record, it should only have NULL and Record type");
        Schema recordSchema = getActualRecord(inputSchema);
        Schema newRecord = Schema.createRecord(recordSchema.getName(), recordSchema.getDoc(), recordSchema.getNamespace(), recordSchema.isError());
        newRecord.setFields(newFields);
        if (inputSchema.getTypes().size() == 1) {
            return Schema.createUnion(newRecord);
        }
        return Schema.createUnion(Lists.newArrayList(Schema.create(Type.NULL), newRecord));
    }
    Schema newRecord = Schema.createRecord(inputSchema.getName(), inputSchema.getDoc(), inputSchema.getNamespace(), inputSchema.isError());
    newRecord.setFields(newFields);
    return newRecord;
}

18 View Complete Implementation : TestAvroSchemaConverter.java
Copyright Apache License 2.0
Author : apache
@Test
public void testUnionOfTwoTypes() throws Exception {
    Schema schema = Schema.createRecord("record2", null, null, false);
    Schema multipleTypes = Schema.createUnion(Arrays.asList(Schema.create(Schema.Type.NULL), Schema.create(INT), Schema.create(Schema.Type.FLOAT)));
    schema.setFields(Arrays.asList(new Schema.Field("myunion", multipleTypes, null, JsonProperties.NULL_VALUE)));
    // Avro union is modelled using optional data members of the different
    // types. This does not translate back into an Avro union
    testAvroToParquetConversion(schema, "message record2 {\n" + "  optional group myunion {\n" + "    optional int32 member0;\n" + "    optional float member1;\n" + "  }\n" + "}\n");
}

18 View Complete Implementation : TestAvroSchemaConverter.java
Copyright Apache License 2.0
Author : apache
@Test
public void testArrayOfOptionalRecords() throws Exception {
    Schema innerRecord = Schema.createRecord("element", null, null, false);
    Schema optionalString = optional(Schema.create(Schema.Type.STRING));
    innerRecord.setFields(Lists.newArrayList(new Schema.Field("s1", optionalString, null, JsonProperties.NULL_VALUE), new Schema.Field("s2", optionalString, null, JsonProperties.NULL_VALUE)));
    Schema schema = Schema.createRecord("HasArray", null, null, false);
    schema.setFields(Lists.newArrayList(new Schema.Field("myarray", Schema.createArray(optional(innerRecord)), null, null)));
    System.err.println("Avro schema: " + schema.toString(true));
    testRoundTripConversion(NEW_BEHAVIOR, schema, "message HasArray {\n" + "  required group myarray (LIST) {\n" + "    repeated group list {\n" + "      optional group element {\n" + "        optional binary s1 (UTF8);\n" + "        optional binary s2 (UTF8);\n" + "      }\n" + "    }\n" + "  }\n" + "}\n");
}

18 View Complete Implementation : GobblinTrackingEventFlattenFilterConverter.java
Copyright Apache License 2.0
Author : apache
@Override
public Schema convertSchema(Schema inputSchema, WorkUnitState workUnit) throws SchemaConversionException {
    Preconditions.checkArgument(AvroUtils.checkReaderWriterCompatibility(gobblinTrackingEventSchema, inputSchema, true));
    Schema outputSchema = Schema.createRecord(ConfigUtils.getString(config, NEW_SCHEMA_NAME, inputSchema.getName()), inputSchema.getDoc(), inputSchema.getNamespace(), inputSchema.isError());
    outputSchema.setFields(newFields);
    return outputSchema;
}

18 View Complete Implementation : TestAvroSchemaConverter.java
Copyright Apache License 2.0
Author : apache
@Test
public void testOptionalArrayElement() throws Exception {
    Schema schema = Schema.createRecord("record1", null, null, false);
    Schema optionalIntArray = Schema.createArray(optional(Schema.create(INT)));
    schema.setFields(Arrays.asList(new Schema.Field("myintarray", optionalIntArray, null, null)));
    testRoundTripConversion(NEW_BEHAVIOR, schema, "message record1 {\n" + "  required group myintarray (LIST) {\n" + "    repeated group list {\n" + "      optional int32 element;\n" + "    }\n" + "  }\n" + "}\n");
}

18 View Complete Implementation : PartitionCollapsingSchemas.java
Copyright Apache License 2.0
Author : apache
public Schema getDatedIntermediateValueSchema() {
    if (_dateIntermediateValueSchema == null) {
        _dateIntermediateValueSchema = Schema.createRecord(DATED_INTERMEDIATE_VALUE_SCHEMA_NAME, null, _outputSchemaNamespace, false);
        List<Field> intermediateValueFields = Arrays.asList(new Field("value", getIntermediateValueSchema(), null, null), new Field("time", Schema.create(Type.LONG), null, null));
        _dateIntermediateValueSchema.setFields(intermediateValueFields);
    }
    return _dateIntermediateValueSchema;
}

18 View Complete Implementation : TSalesforceBulkExecProperties.java
Copyright Apache License 2.0
Author : Talend
private Schema newSchema(Schema metadataSchema, String newSchemaName, List<Schema.Field> moreFields) {
    Schema newSchema = Schema.createRecord(newSchemaName, metadataSchema.getDoc(), metadataSchema.getNamespace(), metadataSchema.isError());
    List<Schema.Field> copyFieldList = new ArrayList<>();
    for (Schema.Field se : metadataSchema.getFields()) {
        Schema.Field field = new Schema.Field(se.name(), se.schema(), se.doc(), se.defaultVal(), se.order());
        field.getObjectProps().putAll(se.getObjectProps());
        for (Map.Entry<String, Object> entry : se.getObjectProps().entrySet()) {
            field.addProp(entry.getKey(), entry.getValue());
        }
        copyFieldList.add(field);
    }
    copyFieldList.addAll(moreFields);
    newSchema.setFields(copyFieldList);
    for (Map.Entry<String, Object> entry : metadataSchema.getObjectProps().entrySet()) {
        newSchema.addProp(entry.getKey(), entry.getValue());
    }
    return newSchema;
}

18 View Complete Implementation : TestAvroSchemaConverter.java
Copyright Apache License 2.0
Author : apache
@Test
public void testDecimalFixedType() throws Exception {
    Schema schema = Schema.createRecord("myrecord", null, null, false);
    Schema decimal = LogicalTypes.decimal(9, 2).addToSchema(Schema.createFixed("dec", null, null, 8));
    schema.setFields(Collections.singletonList(new Schema.Field("dec", decimal, null, null)));
    testRoundTripConversion(schema, "message myrecord {\n" + "  required fixed_len_byte_array(8) dec (DECIMAL(9,2));\n" + "}\n");
}

18 View Complete Implementation : AvroStoreJoinerHttpServer.java
Copyright Apache License 2.0
Author : jingwei
/**
 * Creates the Avro schema for the Address store.
 */
static Schema createAddressSchema() {
    List<Field> fields = new ArrayList<Field>();
    fields.add(new Field("id", Schema.create(Type.INT), null, null));
    fields.add(new Field("city", Schema.create(Type.STRING), null, null));
    fields.add(new Field("state", Schema.create(Type.STRING), null, null));
    fields.add(new Field("country", Schema.create(Type.STRING), null, null));
    fields.add(new Field("postal_code", Schema.create(Type.STRING), null, null));
    Schema schema = Schema.createRecord("Address", null, "avro.test", false);
    schema.setFields(fields);
    return schema;
}

18 View Complete Implementation : PigSchema2Avro.java
Copyright Apache License 2.0
Author : sigmoidanalytics
/**
 * Convert pig data to Avro record
 */
protected static Schema convertRecord(ResourceFieldSchema[] pigFields, boolean nullable) throws IOException {
    AvroStorageLog.funcCall("convertRecord");
    // Type name is required for Avro record
    String typeName = getRecordName();
    Schema outSchema = Schema.createRecord(typeName, null, null, false);
    List<Schema.Field> outFields = new ArrayList<Schema.Field>();
    for (int i = 0; i < pigFields.length; i++) {
        /* get schema */
        Schema fieldSchema = convert(pigFields[i], nullable);
        /* get field name of output */
        String outname = pigFields[i].getName();
        if (outname == null)
            // field name cannot be null
            outname = FIELD_NAME + "_" + i;
        /* get doc of output */
        String desc = pigFields[i].getDescription();
        outFields.add(new Field(outname, fieldSchema, desc, null));
    }
    outSchema.setFields(outFields);
    return outSchema;
}

18 View Complete Implementation : TestAvroSchemaConverter.java
Copyright Apache License 2.0
Author : apache
@Test
public void testOldAvroListOfLists() throws Exception {
    Schema listOfLists = optional(Schema.createArray(Schema.createArray(Schema.create(INT))));
    Schema schema = Schema.createRecord("AvroCompatListInList", null, null, false);
    schema.setFields(Lists.newArrayList(new Schema.Field("listOfLists", listOfLists, null, JsonProperties.NULL_VALUE)));
    System.err.println("Avro schema: " + schema.toString(true));
    testRoundTripConversion(schema, "message AvroCompatListInList {\n" + "  optional group listOfLists (LIST) {\n" + "    repeated group array (LIST) {\n" + "      repeated int32 array;\n" + "    }\n" + "  }\n" + "}");
    // Cannot use round-trip replacedertion because 3-level representation is used
    testParquetToAvroConversion(NEW_BEHAVIOR, schema, "message AvroCompatListInList {\n" + "  optional group listOfLists (LIST) {\n" + "    repeated group array (LIST) {\n" + "      repeated int32 array;\n" + "    }\n" + "  }\n" + "}");
}

18 View Complete Implementation : AvroSchemaGenerator.java
Copyright Apache License 2.0
Author : aliyun
public Schema generate(String schemaNameOverride) throws IOException {
    ClreplacedWriter clreplacedWriter = new ClreplacedWriter(options, connManager, tableName, null);
    Map<String, Integer> columnTypes = clreplacedWriter.getColumnTypes();
    String[] columnNames = clreplacedWriter.getColumnNames(columnTypes);
    List<Field> fields = new ArrayList<Field>();
    for (String columnName : columnNames) {
        String cleanedCol = AvroUtil.toAvroIdentifier(ClreplacedWriter.toJavaIdentifier(columnName));
        int sqlType = columnTypes.get(columnName);
        Schema avroSchema = toAvroSchema(sqlType, columnName);
        Field field = new Field(cleanedCol, avroSchema, null, NullNode.getInstance());
        field.addProp("columnName", columnName);
        field.addProp("sqlType", Integer.toString(sqlType));
        fields.add(field);
    }
    TableClreplacedName tableClreplacedName = new TableClreplacedName(options);
    String shortClreplacedName = tableClreplacedName.getShortClreplacedForTable(tableName);
    String avroTableName = (tableName == null ? TableClreplacedName.QUERY_RESULT : tableName);
    String avroName = schemaNameOverride != null ? schemaNameOverride : (shortClreplacedName == null ? avroTableName : shortClreplacedName);
    String avroNamespace = tableClreplacedName.getPackageForTable();
    String doc = "Sqoop import of " + avroTableName;
    Schema schema = Schema.createRecord(avroName, doc, avroNamespace, false);
    schema.setFields(fields);
    schema.addProp("tableName", avroTableName);
    return schema;
}

18 View Complete Implementation : MarketoRuntimeTestBase.java
Copyright Apache License 2.0
Author : Talend
public Schema getFullDynamicSchema() {
    Schema emptySchema = Schema.createRecord("dynamic", null, null, false);
    emptySchema.setFields(new ArrayList<Field>());
    emptySchema = AvroUtils.setIncludeAllFields(emptySchema, true);
    return emptySchema;
}

18 View Complete Implementation : TSnowflakeOutputProperties.java
Copyright Apache License 2.0
Author : Talend
private void updateOutputSchemas() {
    Schema inputSchema = table.main.schema.getValue();
    final List<Schema.Field> additionalRejectFields = new ArrayList<Schema.Field>();
    addSchemaField(FIELD_COLUMN_NAME, additionalRejectFields);
    addSchemaField(FIELD_ROW_NUMBER, additionalRejectFields);
    addSchemaField(FIELD_CATEGORY, additionalRejectFields);
    addSchemaField(FIELD_CHARACTER, additionalRejectFields);
    addSchemaField(FIELD_ERROR_MESSAGE, additionalRejectFields);
    addSchemaField(FIELD_BYTE_OFFSET, additionalRejectFields);
    addSchemaField(FIELD_LINE, additionalRejectFields);
    addSchemaField(FIELD_SQL_STATE, additionalRejectFields);
    addSchemaField(FIELD_CODE, additionalRejectFields);
    Schema rejectSchema = Schema.createRecord("rejectOutput", inputSchema.getDoc(), inputSchema.getNamespace(), inputSchema.isError());
    List<Schema.Field> copyFieldList = new ArrayList<>();
    copyFieldList.addAll(additionalRejectFields);
    rejectSchema.setFields(copyFieldList);
    schemaReject.schema.setValue(rejectSchema);
}

18 View Complete Implementation : TestAvroSchemaConverter.java
Copyright Apache License 2.0
Author : apache
@Test
public void testArrayOfOptionalRecordsOldBehavior() throws Exception {
    Schema innerRecord = Schema.createRecord("InnerRecord", null, null, false);
    Schema optionalString = optional(Schema.create(Schema.Type.STRING));
    innerRecord.setFields(Lists.newArrayList(new Schema.Field("s1", optionalString, null, JsonProperties.NULL_VALUE), new Schema.Field("s2", optionalString, null, JsonProperties.NULL_VALUE)));
    Schema schema = Schema.createRecord("HasArray", null, null, false);
    schema.setFields(Lists.newArrayList(new Schema.Field("myarray", Schema.createArray(optional(innerRecord)), null, null)));
    System.err.println("Avro schema: " + schema.toString(true));
    // Cannot use round-trip replacedertion because InnerRecord optional is removed
    testAvroToParquetConversion(schema, "message HasArray {\n" + "  required group myarray (LIST) {\n" + "    repeated group array {\n" + "      optional binary s1 (UTF8);\n" + "      optional binary s2 (UTF8);\n" + "    }\n" + "  }\n" + "}\n");
}

18 View Complete Implementation : AvroSchemaConverter.java
Copyright Apache License 2.0
Author : apache
private Schema convertFields(String name, List<Type> parquetFields, Map<String, Integer> names) {
    List<Schema.Field> fields = new ArrayList<Schema.Field>();
    Integer nameCount = names.merge(name, 1, (oldValue, value) -> oldValue + 1);
    for (Type parquetType : parquetFields) {
        Schema fieldSchema = convertField(parquetType, names);
        if (parquetType.isRepereplacedion(REPEATED)) {
            throw new UnsupportedOperationException("REPEATED not supported outside LIST or MAP. Type: " + parquetType);
        } else if (parquetType.isRepereplacedion(Type.Repereplacedion.OPTIONAL)) {
            fields.add(new Schema.Field(parquetType.getName(), optional(fieldSchema), null, NULL_VALUE));
        } else {
            // REQUIRED
            fields.add(new Schema.Field(parquetType.getName(), fieldSchema, null, (Object) null));
        }
    }
    Schema schema = Schema.createRecord(name, null, nameCount > 1 ? name + nameCount : null, false);
    schema.setFields(fields);
    return schema;
}

18 View Complete Implementation : TSnowflakeRowProperties.java
Copyright Apache License 2.0
Author : Talend
private void updateRejectSchema() {
    Schema schema = schemaFlow.schema.getValue();
    final List<Schema.Field> additionalRejectFields = new ArrayList<Schema.Field>();
    additionalRejectFields.add(createRejectField("errorCode"));
    additionalRejectFields.add(createRejectField("errorMessage"));
    Schema newSchema = Schema.createRecord("rejectOutput", schema.getDoc(), schema.getNamespace(), schema.isError());
    List<Schema.Field> copyFieldList = new ArrayList<>();
    for (Schema.Field se : schema.getFields()) {
        Schema.Field field = new Schema.Field(se.name(), se.schema(), se.doc(), se.defaultVal(), se.order());
        field.getObjectProps().putAll(se.getObjectProps());
        for (Map.Entry<String, Object> entry : se.getObjectProps().entrySet()) {
            field.addProp(entry.getKey(), entry.getValue());
        }
        copyFieldList.add(field);
    }
    copyFieldList.addAll(additionalRejectFields);
    newSchema.setFields(copyFieldList);
    for (Map.Entry<String, Object> entry : schema.getObjectProps().entrySet()) {
        newSchema.addProp(entry.getKey(), entry.getValue());
    }
    schemaReject.schema.setValue(newSchema);
}

18 View Complete Implementation : TAzureStorageOutputTableProperties.java
Copyright Apache License 2.0
Author : Talend
private Schema newSchema(Schema metadataSchema, String newSchemaName, List<Schema.Field> moreFields) {
    Schema newSchema = Schema.createRecord(newSchemaName, metadataSchema.getDoc(), metadataSchema.getNamespace(), metadataSchema.isError());
    List<Schema.Field> copyFieldList = new ArrayList<>();
    for (Schema.Field se : metadataSchema.getFields()) {
        Schema.Field field = new Schema.Field(se.name(), se.schema(), se.doc(), se.defaultVal(), se.order());
        field.getObjectProps().putAll(se.getObjectProps());
        for (Map.Entry<String, Object> entry : se.getObjectProps().entrySet()) {
            field.addProp(entry.getKey(), entry.getValue());
        }
        copyFieldList.add(field);
    }
    copyFieldList.addAll(moreFields);
    newSchema.setFields(copyFieldList);
    for (Map.Entry<String, Object> entry : metadataSchema.getObjectProps().entrySet()) {
        newSchema.addProp(entry.getKey(), entry.getValue());
    }
    return newSchema;
}

18 View Complete Implementation : AbstractTestAvroStoreJoiner.java
Copyright Apache License 2.0
Author : jingwei
/**
 * Creates the Avro schema for the Person store.
 */
static Schema createPersonSchema() {
    List<Field> fields = new ArrayList<Field>();
    fields.add(new Field("id", Schema.create(Type.INT), null, null));
    fields.add(new Field("age", Schema.create(Type.INT), null, null));
    fields.add(new Field("fname", Schema.create(Type.STRING), null, null));
    fields.add(new Field("lname", Schema.create(Type.STRING), null, null));
    Schema schema = Schema.createRecord("Person", null, "avro.test", false);
    schema.setFields(fields);
    return schema;
}