/** Returns the Java class name indicated by a schema's name and namespace. */ public static String getClassName(Schema schema) { String namespace = schema.getNamespace(); String name = schema.getName(); if (namespace == null || "".equals(namespace)) return name; String dot = namespace.endsWith("$") ? "" : "."; // back-compatibly handle $ return namespace + dot + name; }
private String getNestedClassName(Schema schema) { String namespace = schema.getNamespace(); String name = schema.getName(); if (namespace == null || "".equals(namespace)) return name; return namespace + "$" + name; }
/** Returns the Java class name indicated by a schema's name and namespace. */ public static String getClassName(Schema schema) { String namespace = schema.getNamespace(); String name = schema.getName(); if (namespace == null || "".equals(namespace)) return name; String dot = namespace.endsWith("$") ? "" : "."; return namespace + dot + name; }
/** * Is this a unresolved schema. * * @param schema * @return */ static boolean isUnresolvedSchema(final Schema schema) { return (schema.getType() == Schema.Type.RECORD && schema.getProp(UR_SCHEMA_ATTR) != null && UR_SCHEMA_NAME.equals(schema.getName()) && UR_SCHEMA_NS.equals(schema.getNamespace())); }
@Override public final GenericRecord getRootConfiguration() throws ConfigurationGenerationException { return getConfigurationByName(avroBaseSchema.getName(), avroBaseSchema.getNamespace()); }
private static Optional<Schema> removeUncomparableFieldsFromRecord(Schema record, Set<Schema> processed) { Preconditions.checkArgument(record.getType() == Schema.Type.RECORD); if (processed.contains(record)) { return Optional.absent(); } processed.add(record); List<Field> fields = Lists.newArrayList(); for (Field field : record.getFields()) { Optional<Schema> newFieldSchema = removeUncomparableFields(field.schema(), processed); if (newFieldSchema.isPresent()) { fields.add(new Field(field.name(), newFieldSchema.get(), field.doc(), field.defaultValue())); } } Schema newSchema = Schema.createRecord(record.getName(), record.getDoc(), record.getNamespace(), false); newSchema.setFields(fields); return Optional.of(newSchema); }
@Override public Schema convertSchema(Schema inputSchema, WorkUnitState workUnit) throws SchemaConversionException { Preconditions.checkArgument(inputSchema.getFields().equals(gobblinTrackingEventSchema.getFields())); Schema outputSchema = Schema .createRecord(ConfigUtils.getString(config, NEW_SCHEMA_NAME, inputSchema.getName()), inputSchema.getDoc(), inputSchema.getNamespace(), inputSchema.isError()); outputSchema.setFields(newFields); return outputSchema; }
@Override public Schema convertSchema(Schema inputSchema, WorkUnitState workUnit) throws SchemaConversionException { List<Field> outputSchemaFields = new ArrayList<>(); for (Field field : inputSchema.getFields()) { outputSchemaFields.add(convertFieldSchema(inputSchema, field, workUnit)); } Schema outputSchema = Schema .createRecord(inputSchema.getName(), inputSchema.getDoc(), inputSchema.getNamespace(), inputSchema.isError()); outputSchema.setFields(outputSchemaFields); return outputSchema; }
@Override public SchemaVisitorAction visitNonTerminal(final Schema nt) { Schema.Type type = nt.getType(); if (type == RECORD) { Schema newSchema = Schema.createRecord(nt.getName(), copyDocs ? nt.getDoc() : null, nt.getNamespace(), nt.isError()); copyProperties.copy(nt, newSchema); replace.put(nt, newSchema); } return SchemaVisitorAction.CONTINUE; }
public static Schema newSchema( Schema metadataSchema, String newSchemaName, List<Schema.Field> moreFields, int insertPoint) { Schema newSchema = Schema.createRecord( newSchemaName, metadataSchema.getDoc(), metadataSchema.getNamespace(), metadataSchema.isError()); List<Schema.Field> fields = metadataSchema.getFields(); List<Schema.Field> copyFieldList = _cloneFieldsAndResetPosition(fields); copyFieldList.addAll(insertPoint, moreFields); newSchema.setFields(copyFieldList); Map<String, Object> objectProperties = metadataSchema.getObjectProps(); for (Map.Entry<String, Object> entry : objectProperties.entrySet()) { newSchema.addProp(entry.getKey(), entry.getValue()); } return newSchema; }
public static String getJavaClassName(final Schema schema) { String namespace = schema.getNamespace(); if (namespace == null) { return SpecificCompiler.mangle(schema.getName()); } else { return namespace + '.' + SpecificCompiler.mangle(schema.getName()); } }
@Override public Schema convertSchemaImpl(Schema inputSchema, WorkUnitState workUnitState) throws SchemaConversionException { if (inputSchema == null) { throw new SchemaConversionException("input schema is empty"); } List<Schema.Field> fields = Lists.newArrayList(); for (Schema.Field field : inputSchema.getFields()) { Schema.Field newField = new Schema.Field(field.name(), field.schema(), field.doc(), field.defaultValue(), field.order()); fields.add(newField); } Schema.Field requestResponseField = new Schema.Field(HTTP_REQUEST_RESPONSE_FIELD, HttpRequestResponseRecord.getClassSchema(), "http output schema contains request url and return result", null); fields.add(requestResponseField); Schema combinedSchema = Schema.createRecord(inputSchema.getName(), inputSchema.getDoc() + " (Http request and response are contained)", inputSchema.getNamespace(), false); combinedSchema.setFields(fields); return combinedSchema; }
private void checkConvertedAvroData(Schema schema, GenericRecord record) { Assert.assertEquals(schema.getNamespace(), "example.avro"); Assert.assertEquals(schema.getType(), Schema.Type.RECORD); Assert.assertEquals(schema.getName(), "User"); Assert.assertEquals(schema.getFields().size(), 3); Schema.Field nameField = schema.getField("name"); Assert.assertEquals(nameField.name(), "name"); Assert.assertEquals(nameField.schema().getType(), Schema.Type.STRING); Schema.Field favNumberField = schema.getField("favorite_number"); Assert.assertEquals(favNumberField.name(), "favorite_number"); Assert.assertEquals(favNumberField.schema().getType(), Schema.Type.INT); Schema.Field favColorField = schema.getField("favorite_color"); Assert.assertEquals(favColorField.name(), "favorite_color"); Assert.assertEquals(favColorField.schema().getType(), Schema.Type.STRING); Assert.assertEquals(record.get("name"), "Alyssa"); Assert.assertEquals(record.get("favorite_number"), 256d); Assert.assertEquals(record.get("favorite_color"), "yellow"); }
public static Schema appendFields( Schema schema, List<Schema.Field> fields) { if (schema.getType() != Schema.Type.RECORD) { TalendRuntimeExceptionBuilder talendRuntimeExceptionBuilder = TalendRuntimeException.build( CommonErrorCodes.UNEXPECTED_EXCEPTION); talendRuntimeExceptionBuilder.setAndThrow( "Schema type is not supported: " + schema.getType()); } Schema newSchema = Schema.createRecord( schema.getName(), schema.getDoc(), schema.getNamespace(), schema.isError()); List<Schema.Field> copiedFieldList = new ArrayList<>(); _copyFields(schema.getFields(), copiedFieldList); _copyFields(fields, copiedFieldList); newSchema.setFields(copiedFieldList); _copyFieldProperties(schema.getObjectProps(), newSchema); return newSchema; }
@Test public void schemaWithRecordOfRecordCheckNamespace() throws Exception { String testName = "schemaWithRecordOfRecordCheckNamespace"; JsonObject schema = getSchemaData(testName).getAsJsonObject(); JsonObject expected = getExpectedSchema(testName).getAsJsonObject(); RecordConverter converter = new RecordConverter(new JsonSchema(schema), state, buildNamespace(state.getExtract().getNamespace(), "person")); Assert.assertEquals(avroSchemaToJsonElement(converter), expected); Assert.assertEquals(converter.schema().getField("someperson").schema().getNamespace(), "namespace.person.myrecord"); Assert.assertEquals(converter.schema().getNamespace(), "namespace.person"); }
@Test public void schemaWithRecordOfEnumCheckNamespace() throws Exception { String testName = "schemaWithRecordOfEnumCheckNamespace"; JsonObject schema = getSchemaData(testName).getAsJsonObject(); JsonObject expected = getExpectedSchema(testName).getAsJsonObject(); RecordConverter converter = new RecordConverter(new JsonSchema(schema), state, buildNamespace(state.getExtract().getNamespace(), "something")); Assert.assertEquals(avroSchemaToJsonElement(converter), expected); Assert.assertEquals(converter.schema().getField("someperson").schema().getNamespace(), "namespace.something.myrecord"); Assert.assertEquals(converter.schema().getNamespace(), "namespace.something"); }
@Test public void testSwitchNamespace() { String originalNamespace = "originalNamespace"; String originalName = "originalName"; String newNamespace = "newNamespace"; Schema schema = SchemaBuilder.builder(originalNamespace).record(originalName).fields(). requiredDouble("double").optionalFloat("float").endRecord(); Map<String, String> map = Maps.newHashMap(); map.put(originalNamespace, newNamespace); Schema newSchema = AvroUtils.switchNamespace(schema, map); Assert.assertEquals(newSchema.getNamespace(), newNamespace); Assert.assertEquals(newSchema.getName(), originalName); for(Schema.Field field : newSchema.getFields()) { Assert.assertEquals(field, schema.getField(field.name())); } }
public FieldAssembler<R> fields() { Schema record = Schema.createRecord(name(), doc(), space(), false); // place the record in the name context, fields yet to be set. completeSchema(record); return new FieldAssembler<>( context(), names().namespace(record.getNamespace()), record); } }
public FieldAssembler<R> fields() { Schema record = Schema.createRecord(name(), doc(), space(), false); // place the record in the name context, fields yet to be set. completeSchema(record); return new FieldAssembler<R>( context(), names().namespace(record.getNamespace()), record); } }
@Test public void testDecimalFixed() throws IOException { Schema schema = REFLECT.getSchema(DecimalRecordFixed.class); Assert.assertEquals("Should have the correct record name", "org.apache.avro.reflect.TestReflectLogicalTypes", schema.getNamespace()); Assert.assertEquals("Should have the correct record name", "DecimalRecordFixed", schema.getName()); Assert.assertEquals("Should have the correct logical type", LogicalTypes.decimal(9, 2), LogicalTypes.fromSchema(schema.getField("decimal").schema())); DecimalRecordFixed record = new DecimalRecordFixed(); record.decimal = new BigDecimal("3.14"); File test = write(REFLECT, schema, record); Assert.assertEquals("Should match the decimal after round trip", Arrays.asList(record), read(REFLECT.createDatumReader(schema), test)); }