@Test public void testFromConnectComplexWithDefaults() { int dateDefVal = 100; int timeDefVal = 1000 * 60 * 60 * 2; long tsDefVal = 1000 * 60 * 60 * 24 * 365 + 100; java.util.Date dateDef = Date.toLogical(Date.SCHEMA, dateDefVal); java.util.Date timeDef = Time.toLogical(Time.SCHEMA, timeDefVal); java.util.Date tsDef = Timestamp.toLogical(Timestamp.SCHEMA, tsDefVal); BigDecimal decimalDef = new BigDecimal(BigInteger.valueOf(314159L), 5); byte[] decimalDefVal = decimalDef.unscaledValue().toByteArray(); Schema schema = SchemaBuilder.struct() .field("int8", SchemaBuilder.int8().defaultValue((byte) 2).doc("int8 field").build()) .field("int16", SchemaBuilder.int16().defaultValue((short)12).doc("int16 field").build()) .field("int32", SchemaBuilder.int32().defaultValue(12).doc("int32 field").build()) .field("int64", SchemaBuilder.int64().defaultValue(12L).doc("int64 field").build()) .field("float32", SchemaBuilder.float32().defaultValue(12.2f).doc("float32 field").build()) .field("float64", SchemaBuilder.float64().defaultValue(12.2).doc("float64 field").build()) .field("boolean", SchemaBuilder.bool().defaultValue(true).doc("bool field").build()) .field("string", SchemaBuilder.string().defaultValue("foo").doc("string field").build()) .field("bytes", SchemaBuilder.bytes().defaultValue(ByteBuffer.wrap("foo".getBytes())).doc("bytes field").build()) .field("array", SchemaBuilder.array(Schema.STRING_SCHEMA).defaultValue(Arrays.asList("a", "b", "c")).build()) .field("map", SchemaBuilder.map(Schema.STRING_SCHEMA, Schema.INT32_SCHEMA).defaultValue(Collections.singletonMap("field", 1)).build()) .field("date", Date.builder().defaultValue(dateDef).doc("date field").build()) .field("time", Time.builder().defaultValue(timeDef).doc("time field").build()) .field("ts", Timestamp.builder().defaultValue(tsDef).doc("ts field").build()) .field("decimal", Decimal.builder(5).defaultValue(decimalDef).doc("decimal field").build()) .build(); Struct struct = new Struct(schema) .put("int8", (byte) 2) .put("int16", (short) 12) .put("int32", 12) .put("int64", 12L) .put("float32", 12.2f) .put("float64", 12.2) .put("boolean", true) .put("string", "foo") .put("bytes", ByteBuffer.wrap("foo".getBytes())) .put("array", Arrays.asList("a", "b", "c")) .put("map", Collections.singletonMap("field", 1)) .put("date", dateDef) .put("time", timeDef) .put("ts", tsDef) .put("decimal", decimalDef); org.apache.avro.Schema complexMapElementSchema = org.apache.avro.SchemaBuilder .record("MapEntry").namespace("io.confluent.connect.avro").fields() .requiredInt("key") .requiredInt("value") .endRecord(); org.apache.avro.Schema int8Schema = org.apache.avro.SchemaBuilder.builder().intType(); int8Schema.addProp("connect.doc", "int8 field"); int8Schema.addProp("connect.default", JsonNodeFactory.instance.numberNode(2)); int8Schema.addProp("connect.type", "int8"); org.apache.avro.Schema int16Schema = org.apache.avro.SchemaBuilder.builder().intType(); int16Schema.addProp("connect.doc", "int16 field"); int16Schema.addProp("connect.default", JsonNodeFactory.instance.numberNode(((short)12)).intValue()); int16Schema.addProp("connect.type", "int16"); org.apache.avro.Schema int32Schema = org.apache.avro.SchemaBuilder.builder().intType(); int32Schema.addProp("connect.doc", "int32 field"); int32Schema.addProp("connect.default", JsonNodeFactory.instance.numberNode(12)); org.apache.avro.Schema int64Schema = org.apache.avro.SchemaBuilder.builder().longType(); int64Schema.addProp("connect.doc", "int64 field"); int64Schema.addProp("connect.default", JsonNodeFactory.instance.numberNode(12L)); org.apache.avro.Schema float32Schema = org.apache.avro.SchemaBuilder.builder().floatType(); float32Schema.addProp("connect.doc", "float32 field"); float32Schema.addProp("connect.default", JsonNodeFactory.instance.numberNode(12.2f)); org.apache.avro.Schema float64Schema = org.apache.avro.SchemaBuilder.builder().doubleType(); float64Schema.addProp("connect.doc", "float64 field"); float64Schema.addProp("connect.default", JsonNodeFactory.instance.numberNode(12.2)); org.apache.avro.Schema boolSchema = org.apache.avro.SchemaBuilder.builder().booleanType(); boolSchema.addProp("connect.doc", "bool field"); boolSchema.addProp("connect.default", JsonNodeFactory.instance.booleanNode(true)); org.apache.avro.Schema stringSchema = org.apache.avro.SchemaBuilder.builder().stringType(); stringSchema.addProp("connect.doc", "string field"); stringSchema.addProp("connect.default", JsonNodeFactory.instance.textNode("foo")); org.apache.avro.Schema bytesSchema = org.apache.avro.SchemaBuilder.builder().bytesType(); bytesSchema.addProp("connect.doc", "bytes field"); bytesSchema.addProp("connect.default", JsonNodeFactory.instance.textNode( new String("foo".getBytes(), StandardCharsets.ISO_8859_1) )); org.apache.avro.Schema dateSchema = org.apache.avro.SchemaBuilder.builder().intType(); dateSchema.addProp("connect.doc", "date field"); dateSchema.addProp("connect.default", JsonNodeFactory.instance.numberNode(dateDefVal)); dateSchema.addProp(AvroData.CONNECT_NAME_PROP, Date.LOGICAL_NAME); dateSchema.addProp(AvroData.CONNECT_VERSION_PROP, 1); LogicalTypes.date().addToSchema(dateSchema); dateSchema.addProp(AvroData.AVRO_LOGICAL_TYPE_PROP, AvroData.AVRO_LOGICAL_DATE); org.apache.avro.Schema timeSchema = org.apache.avro.SchemaBuilder.builder().intType(); timeSchema.addProp("connect.doc", "time field"); timeSchema.addProp("connect.default", JsonNodeFactory.instance.numberNode(timeDefVal)); timeSchema.addProp(AvroData.CONNECT_NAME_PROP, Time.LOGICAL_NAME); timeSchema.addProp(AvroData.CONNECT_VERSION_PROP, 1); LogicalTypes.timeMillis().addToSchema(timeSchema); timeSchema.addProp(AvroData.AVRO_LOGICAL_TYPE_PROP, AvroData.AVRO_LOGICAL_TIME_MILLIS); org.apache.avro.Schema tsSchema = org.apache.avro.SchemaBuilder.builder().longType(); tsSchema.addProp("connect.doc", "ts field"); tsSchema.addProp("connect.default", JsonNodeFactory.instance.numberNode(tsDefVal)); tsSchema.addProp(AvroData.CONNECT_NAME_PROP, Timestamp.LOGICAL_NAME); tsSchema.addProp(AvroData.CONNECT_VERSION_PROP, 1); LogicalTypes.timestampMillis().addToSchema(tsSchema); tsSchema.addProp(AvroData.AVRO_LOGICAL_TYPE_PROP, AvroData.AVRO_LOGICAL_TIMESTAMP_MILLIS); org.apache.avro.Schema decimalSchema = org.apache.avro.SchemaBuilder.builder().bytesType(); decimalSchema.addProp("scale", 5); decimalSchema.addProp("precision", 64); decimalSchema.addProp("connect.doc", "decimal field"); decimalSchema.addProp(AvroData.CONNECT_VERSION_PROP, 1); decimalSchema.addProp("connect.default", JsonNodeFactory.instance.textNode( new String(decimalDefVal, StandardCharsets.ISO_8859_1))); decimalSchema.addProp("connect.parameters", parameters("scale", "5")); decimalSchema.addProp(AvroData.CONNECT_NAME_PROP, Decimal.LOGICAL_NAME); LogicalTypes.decimal(64, 5).addToSchema(decimalSchema); decimalSchema.addProp(AvroData.AVRO_LOGICAL_TYPE_PROP, AvroData.AVRO_LOGICAL_DECIMAL); org.apache.avro.Schema arraySchema = org.apache.avro.SchemaBuilder.builder().array().items().stringType(); ArrayNode arrayNode = JsonNodeFactory.instance.arrayNode(); arrayNode.add("a"); arrayNode.add("b"); arrayNode.add("c"); arraySchema.addProp("connect.default", arrayNode); org.apache.avro.Schema mapSchema = org.apache.avro.SchemaBuilder.builder().map().values().intType(); ObjectNode mapNode = JsonNodeFactory.instance.objectNode(); mapNode.put("field", 1); mapSchema.addProp("connect.default", mapNode); org.apache.avro.Schema nonStringMapSchema = org.apache.avro.SchemaBuilder.builder().array() .items(complexMapElementSchema); ArrayNode nonStringMapNode = JsonNodeFactory.instance.arrayNode(); nonStringMapNode.add(JsonNodeFactory.instance.numberNode(1)); nonStringMapNode.add(JsonNodeFactory.instance.numberNode(1)); ArrayNode nonStringMapArrayNode = JsonNodeFactory.instance.arrayNode(); nonStringMapArrayNode.add(nonStringMapNode); nonStringMapSchema.addProp("connect.default", nonStringMapArrayNode); org.apache.avro.Schema avroSchema = org.apache.avro.SchemaBuilder .record(AvroData.DEFAULT_SCHEMA_NAME).namespace(AvroData.NAMESPACE) .fields() .name("int8").type(int8Schema).withDefault(2) .name("int16").type(int16Schema).withDefault(12) .name("int32").type(int32Schema).withDefault(12) .name("int64").type(int64Schema).withDefault(12L) .name("float32").type(float32Schema).withDefault(12.2f) .name("float64").type(float64Schema).withDefault(12.2) .name("boolean").type(boolSchema).withDefault(true) .name("string").type(stringSchema).withDefault("foo") .name("bytes").type(bytesSchema).withDefault(ByteBuffer.wrap("foo".getBytes())) .name("array").type(arraySchema).withDefault(Arrays.asList("a", "b", "c")) .name("map").type(mapSchema).withDefault(Collections.singletonMap("field", 1)) .name("date").type(dateSchema).withDefault(dateDefVal) .name("time").type(timeSchema).withDefault(timeDefVal) .name("ts").type(tsSchema).withDefault(tsDefVal) .name("decimal").type(decimalSchema).withDefault(ByteBuffer.wrap(decimalDefVal)) .endRecord(); org.apache.avro.generic.GenericRecord avroRecord = new org.apache.avro.generic.GenericRecordBuilder(avroSchema) .set("int8", 2) .set("int16", 12) .set("int32", 12) .set("int64", 12L) .set("float32", 12.2f) .set("float64", 12.2) .set("boolean", true) .set("string", "foo") .set("bytes", ByteBuffer.wrap("foo".getBytes())) .set("array", Arrays.asList("a", "b", "c")) .set("map", Collections.singletonMap("field", 1)) .set("date", dateDefVal) .set("time", timeDefVal) .set("ts", tsDefVal) .set("decimal", decimalDefVal) .build(); SchemaAndValue schemaAndValue = new SchemaAndValue(schema, struct); schemaAndValue = convertToConnect(avroSchema, avroRecord, schemaAndValue); schemaAndValue = convertToConnect(avroSchema, avroRecord, schemaAndValue); schemaAndValue = convertToConnect(avroSchema, avroRecord, schemaAndValue); assertNotNull(schemaAndValue); }
public static List<Field> fields(Schema schema) { Schema.Type type = schema.type(); if (Schema.Type.STRUCT.equals(type)) { return schema.fields(); } else { return null; } }
AvroData { public static List<Field> fields(Schema schema) { Schema.Type type = schema.type(); if (Schema.Type.STRUCT.equals(type)) { return schema.fields(); } else { return null; } } }
AvroData { public static List<Field> fields(Schema schema) { Schema.Type type = schema.type(); if (Schema.Type.STRUCT.equals(type)) { return schema.fields(); } else { return null; } } AvroData(int cacheSize);  AvroData(AvroDataConfig avroDataConfig); }
AvroData { public static List<Field> fields(Schema schema) { Schema.Type type = schema.type(); if (Schema.Type.STRUCT.equals(type)) { return schema.fields(); } else { return null; } } AvroData(int cacheSize);  AvroData(AvroDataConfig avroDataConfig); Object fromConnectData(Schema schema, Object value); org.apache.avro.Schema fromConnectSchema(Schema schema); org.apache.avro.Schema fromConnectSchema(Schema schema,
                                                  Map<Schema, org.apache.avro.Schema> schemaMap); org.apache.avro.Schema fromConnectSchema(Schema schema,
                                                  FromConnectContext fromConnectContext,
                                                  boolean ignoreOptional); org.apache.avro.Schema fromConnectSchemaWithCycle(
      Schema schema,
      FromConnectContext fromConnectContext, boolean ignoreOptional); SchemaAndValue toConnectData(org.apache.avro.Schema avroSchema, Object value); SchemaAndValue toConnectData(org.apache.avro.Schema avroSchema, Object value,
                                      Integer version); Schema toConnectSchema(org.apache.avro.Schema schema); static Schema nonOptional(Schema schema); static List<Field> fields(Schema schema); static Schema keySchema(Schema schema); static Schema valueSchema(Schema schema); }
AvroData { public static List<Field> fields(Schema schema) { Schema.Type type = schema.type(); if (Schema.Type.STRUCT.equals(type)) { return schema.fields(); } else { return null; } } AvroData(int cacheSize);  AvroData(AvroDataConfig avroDataConfig); Object fromConnectData(Schema schema, Object value); org.apache.avro.Schema fromConnectSchema(Schema schema); org.apache.avro.Schema fromConnectSchema(Schema schema,
                                                  Map<Schema, org.apache.avro.Schema> schemaMap); org.apache.avro.Schema fromConnectSchema(Schema schema,
                                                  FromConnectContext fromConnectContext,
                                                  boolean ignoreOptional); org.apache.avro.Schema fromConnectSchemaWithCycle(
      Schema schema,
      FromConnectContext fromConnectContext, boolean ignoreOptional); SchemaAndValue toConnectData(org.apache.avro.Schema avroSchema, Object value); SchemaAndValue toConnectData(org.apache.avro.Schema avroSchema, Object value,
                                      Integer version); Schema toConnectSchema(org.apache.avro.Schema schema); static Schema nonOptional(Schema schema); static List<Field> fields(Schema schema); static Schema keySchema(Schema schema); static Schema valueSchema(Schema schema); static final String NAMESPACE; static final String DEFAULT_SCHEMA_NAME; static final String DEFAULT_SCHEMA_FULL_NAME; static final String MAP_ENTRY_TYPE_NAME; static final String KEY_FIELD; static final String VALUE_FIELD; static final String CONNECT_NAME_PROP; static final String CONNECT_DOC_PROP; static final String CONNECT_VERSION_PROP; static final String CONNECT_DEFAULT_VALUE_PROP; static final String CONNECT_PARAMETERS_PROP; static final String CONNECT_INTERNAL_TYPE_NAME; static final String AVRO_RECORD_DOC_PROP; static final String AVRO_ENUM_DOC_PREFIX_PROP; static final String AVRO_FIELD_DOC_PREFIX_PROP; static final String AVRO_FIELD_DEFAULT_FLAG_PROP; static final String AVRO_ENUM_DEFAULT_PREFIX_PROP; static final String CONNECT_TYPE_PROP; static final String CONNECT_TYPE_INT8; static final String CONNECT_TYPE_INT16; static final String AVRO_TYPE_UNION; static final String AVRO_TYPE_ENUM; static final String AVRO_TYPE_ANYTHING; static final org.apache.avro.Schema ANYTHING_SCHEMA_MAP_ELEMENT; static final org.apache.avro.Schema ANYTHING_SCHEMA; }