public Struct toConnectRow(final GenericRow row) { final Struct struct = new Struct(schema); schema.fields().forEach( field -> struct.put(field, row.getColumns().get(field.index())) ); return struct; } }
private static boolean areFieldListsEqual(List<Field> fields1, List<Field> fields2) { if (fields1 == null && fields2 != null || fields1 != null && fields2 == null) { return false; } if (fields1.size() != fields2.size()) { return false; } for(int i = 0; i < fields1.size(); i++) { Field field1 = fields1.get(i); Field field2 = fields2.get(i); boolean equal = Objects.equals(field1.index(), field2.index()) && Objects.equals(field1.name(), field2.name()) && areConnectSchemasEqual(field1.schema(), field2.schema()); if (!equal) { return false; } } return true; } }
sb.append('{'); appendFirst("name", f.name()); appendAdditional("index", f.index()); appendAdditional("schema", f.schema()); sb.append('}');
assertThat(values).isNotNull(); assertThat(values.field("C1").name()).isEqualTo("C1"); assertThat(values.field("C1").index()).isEqualTo(0); assertThat(values.field("C1").schema()).isEqualTo(SchemaBuilder.string().build()); assertThat(values.field("C2").name()).isEqualTo("C2"); assertThat(values.field("C2").index()).isEqualTo(1); assertThat(values.field("C2").schema()).isEqualTo(Decimal.builder(3).parameter("connect.decimal.precision", "5").optional().build()); // scale of 3 assertThat(values.field("C3").name()).isEqualTo("C3"); assertThat(values.field("C3").index()).isEqualTo(2); assertThat(values.field("C3").schema()).isEqualTo(Date.builder().optional().build()); // optional date assertThat(values.field("C4").name()).isEqualTo("C4"); assertThat(values.field("C4").index()).isEqualTo(3); assertThat(values.field("C4").schema()).isEqualTo(SchemaBuilder.int32().optional().build()); // JDBC INTEGER = 32 bits assertThat(values.field("C5").index()).isEqualTo(4); assertThat(values.field("C5").schema()).isEqualTo(SchemaBuilder.bytes().build()); // JDBC BINARY = bytes assertThat(values.field("C6").index()).isEqualTo(5); assertThat(values.field("C6").schema()).isEqualTo(SchemaBuilder.int16().build());
/** * Get the underlying raw value for the field without accounting for default values. * @param fieldName the field to get the value of * @return the raw value */ public Object getWithoutDefault(String fieldName) { Field field = lookupField(fieldName); return values[field.index()]; }
public static void assertField(final Field expected, final Field actual, String message) { String prefix = Strings.isNullOrEmpty(message) ? "" : message + ": "; if (null == expected) { assertNull(actual, prefix + "actual should be null."); return; } assertEquals(expected.name(), actual.name(), prefix + "name does not match"); assertEquals(expected.index(), actual.index(), prefix + "name does not match"); assertSchema(expected.schema(), actual.schema(), prefix + "schema does not match"); }
private Object getCheckType(String fieldName, Schema.Type type) { Field field = lookupField(fieldName); if (field.schema().type() != type) throw new DataException("Field '" + fieldName + "' is not of type " + type); return values[field.index()]; }
/** * Get the value of a field, returning the default value if no value has been set yet and a default value is specified * in the field's schema. Because this handles fields of all types, the value is returned as an {@link Object} and * must be cast to a more specific type. * @param field the field to lookup * @return the value for the field */ public Object get(Field field) { Object val = values[field.index()]; if (val == null && field.schema().defaultValue() != null) { val = field.schema().defaultValue(); } return val; }
/** * Set the value of a field. Validates the value, throwing a {@link DataException} if it does not match the field's * {@link Schema}. * @param field the field to set * @param value the value of the field * @return the Struct, to allow chaining of {@link #put(String, Object)} calls */ public Struct put(Field field, Object value) { if (null == field) throw new DataException("field cannot be null."); ConnectSchema.validateValue(field.name(), field.schema(), value); values[field.index()] = value; return this; }
/** * Validates that this struct has filled in all the necessary data with valid values. For required fields * without defaults, this validates that a value has been set and has matching types/schemas. If any validation * fails, throws a DataException. */ public void validate() { for (Field field : schema.fields()) { Schema fieldSchema = field.schema(); Object value = values[field.index()]; if (value == null && (fieldSchema.isOptional() || fieldSchema.defaultValue() != null)) continue; ConnectSchema.validateValue(field.name(), fieldSchema, value); } }
sb.append('{'); appendFirst("name", f.name()); appendAdditional("index", f.index()); appendAdditional("schema", f.schema()); sb.append('}');