0
votes

How do you instantiate an Array instance declared with SchemaBuilder.array(ARRAY_SCHEMA)?

With Kafka Connect Schema you can instantiate an org.apache.kafka.connect.data.Struct in your custom connector like so:

Schema STRUCT_SCHEMA = SchemaBuilder.struct().field("timestamp", Schema.INT64_SCHEMA).build();
Struct s = new Struct(STRUCT_SCHEMA);

You can also declare an array schema, but I can't figure out how to create one:

Schema ARRAY_SCHEMA = SchemaBuilder.array(Schema.FLOAT64_SCHEMA).build();
// Create one here, how?

I'm actually trying to nest an array inside a struct, but I presume the problem is I can't instantiate an array instance. Simply adding a native Java array doesn't seem to work. The error I'm seeing at runtime is:

Unable to create Struct from value: org.apache.kafka.connect.errors.DataException: Invalid Java object for schema type ARRAY: class [Ljava.lang.Double; for field: "floatValues"

For nested schema:

Schema ARRAY_SCHEMA = SchemaBuilder.array(Schema.OPTIONAL_FLOAT64_SCHEMA).optional().build();
Schema STRUCT_SCHEMA = SchemaBuilder.struct()
                .field("timestamp", Schema.INT64_SCHEMA)
                .field("floatValues", ARRAY_SCHEMA).build();
Struct s = new Struct(STRUCT_SCHEMA);
s.put("floatValues", new Double[]{Double.valueOf(1)});
// I assume I must need to wrap the Double[] inside some Kafka Connect Array object?

Resources I've reviewed:

1
Did you try a Float[]?OneCricketeer
Using Float[] results in similar error message at runtime just substitute "class [Ljava.lang.Double;" with "class [Ljava.lang.Float;"Ryan
How about a List<Double>?OneCricketeer
Might be getting closer. Using a List<Double> results in Invalid Java object for schema type FLOAT64: class java.lang.Float for field: "null"Ryan
Actually, using a List<Double> works. Using a List<Float> results in Invalid Java object for schema type FLOAT64: class java.lang.Float for field: "null" Thanks.Ryan

1 Answers

1
votes

You need to use a List

You can see the Struct unit tests here