protected List<SchemaAndValueField> schemaAndValuesForGeomTypes() { Schema pointSchema = Point.builder().optional().build(); return Collections.singletonList(new SchemaAndValueField("p", pointSchema, Point.createValue(pointSchema, 1, 1))); }
/** * Create a value for this schema using WKB * @param pointSchema a {@link Schema} instance which represents a point; may not be null * @param wkb the original Well-Known binary representation of the coordinate; may not be null * @param srid the coordinate reference system identifier; null if unset/unknown * @return a {@link Struct} which represents a Connect value for this schema; never null */ public static Struct createValue(Schema geomSchema, byte[] wkb, Integer srid) throws IllegalArgumentException { Struct result = Geometry.createValue(geomSchema, wkb, srid); double[] pt = parseWKBPoint(wkb); result.put(X_FIELD, pt[0]); result.put(Y_FIELD, pt[1]); return result; } }
/** * Creates a value for this schema using 2 given coordinates. * * @param pointSchema a {@link Schema} instance which represents a point; may not be null * @param x the X coordinate of the point; may not be null * @param y the Y coordinate of the point; may not be null * @return a {@link Struct} which represents a Connect value for this schema; never null */ public static Struct createValue(Schema geomSchema, double x, double y){ // turn the specified points byte[] wkb = buildWKBPoint(x, y); Struct result = Geometry.createValue(geomSchema, wkb, null); result.put(X_FIELD, x); result.put(Y_FIELD, y); return result; }
return io.debezium.data.geometry.Point.builder();
/** * Converts a value representing a Postgres point for a column, to a Kafka Connect value. * * @param column the JDBC column; never null * @param fieldDefn the Connect field definition for this column; never null * @param data a data for the point column, either coming from the JDBC driver or logical decoding plugin * @return a value which will be used by Connect to represent the actual point value */ protected Object convertPoint(Column column, Field fieldDefn, Object data) { return convertValue(column, fieldDefn, data, Point.createValue(fieldDefn.schema(), 0, 0), (r) -> { final Schema schema = fieldDefn.schema(); if (data instanceof PGpoint) { PGpoint pgPoint = (PGpoint) data; r.deliver(Point.createValue(schema, pgPoint.x, pgPoint.y)); } else if (data instanceof String) { String dataString = data.toString(); try { PGpoint pgPoint = new PGpoint(dataString); r.deliver(Point.createValue(schema, pgPoint.x, pgPoint.y)); } catch (SQLException e) { logger.warn("Error converting the string '{}' to a PGPoint type for the column '{}'", dataString, column); } } else if (data instanceof PgProto.Point) { r.deliver(Point.createValue(schema, ((PgProto.Point) data).getX(), ((PgProto.Point) data).getY())); } }); }
return Uuid.builder(); case PgOid.POINT: return Point.builder(); case PgOid.MONEY: return Decimal.builder(column.scale().get());
/** * Convert the a value representing a POINT {@code byte[]} value to a Point value used in a {@link SourceRecord}. * * @param column the column in which the value appears * @param fieldDefn the field definition for the {@link SourceRecord}'s {@link Schema}; never null * @param data the data; may be null * @return the converted value, or null if the conversion could not be made and the column allows nulls * @throws IllegalArgumentException if the value could not be converted but the column does not allow nulls */ protected Object convertPoint(Column column, Field fieldDefn, Object data){ final MySqlGeometry empty = MySqlGeometry.createEmpty(); return convertValue(column, fieldDefn, data, io.debezium.data.geometry.Geometry.createValue(fieldDefn.schema(), empty.getWkb(), empty.getSrid()), (r) -> { if (data instanceof byte[]) { // The binlog utility sends a byte array for any Geometry type, we will use our own binaryParse to parse the byte to WKB, hence // to the suitable class MySqlGeometry mySqlGeometry = MySqlGeometry.fromBytes((byte[]) data); if (mySqlGeometry.isPoint()) { r.deliver(io.debezium.data.geometry.Point.createValue(fieldDefn.schema(), mySqlGeometry.getWkb(), mySqlGeometry.getSrid())); } else { throw new ConnectException("Failed to parse and read a value of type POINT on " + column); } } }); }
protected List<SchemaAndValueField> schemaAndValuesForGeomTypes() { Schema pointSchema = Point.builder().optional().build(); return Collections.singletonList(new SchemaAndValueField("p", pointSchema, Point.createValue(pointSchema, 1, 1))); }
Json.builder().optional().build(), Json.builder().optional().build(), Xml.builder().optional().build(), Uuid.builder().optional().build()); assertTableSchema("public.geom_table", "p", Point.builder().optional().build()); assertTableSchema("public.tstzrange_table", "unbounded_exclusive_range, bounded_inclusive_range", Schema.OPTIONAL_STRING_SCHEMA, Schema.OPTIONAL_STRING_SCHEMA);
/** * Converts a value representing a Postgres point for a column, to a Kafka Connect value. * * @param column the JDBC column; never null * @param fieldDefn the Connect field definition for this column; never null * @param data a data for the point column, either coming from the JDBC driver or logical decoding plugin * @return a value which will be used by Connect to represent the actual point value */ protected Object convertPoint(Column column, Field fieldDefn, Object data) { return convertValue(column, fieldDefn, data, Point.createValue(fieldDefn.schema(), 0, 0), (r) -> { final Schema schema = fieldDefn.schema(); if (data instanceof PGpoint) { PGpoint pgPoint = (PGpoint) data; r.deliver(Point.createValue(schema, pgPoint.x, pgPoint.y)); } else if (data instanceof String) { String dataString = data.toString(); try { PGpoint pgPoint = new PGpoint(dataString); r.deliver(Point.createValue(schema, pgPoint.x, pgPoint.y)); } catch (SQLException e) { logger.warn("Error converting the string '{}' to a PGPoint type for the column '{}'", dataString, column); } } else if (data instanceof PgProto.Point) { r.deliver(Point.createValue(schema, ((PgProto.Point) data).getX(), ((PgProto.Point) data).getY())); } }); }
@Test public void shouldConvertMySqlBytesToPoint() throws Exception { byte[] mysqlBytes = DatatypeConverter.parseHexBinary("000000000101000000e3a59bc420f01b4015a143a69d383240"); //This represents 'POINT(6.9845 18.22115554)' MySqlGeometry geom = MySqlGeometry.fromBytes(mysqlBytes); assertTrue(geom.isPoint()); assertEquals(geom.getSrid(), null); double[] coords = Point.parseWKBPoint(geom.getWkb()); assertEquals(coords[0], 6.9845, 0.0001); assertEquals(coords[1], 18.22115554, 0.0001); }
/** * Creates a value for this schema using 2 given coordinates. * * @param pointSchema a {@link Schema} instance which represents a point; may not be null * @param x the X coordinate of the point; may not be null * @param y the Y coordinate of the point; may not be null * @return a {@link Struct} which represents a Connect value for this schema; never null */ public static Struct createValue(Schema geomSchema, double x, double y){ // turn the specified points byte[] wkb = buildWKBPoint(x, y); Struct result = Geometry.createValue(geomSchema, wkb, null); result.put(X_FIELD, x); result.put(Y_FIELD, y); return result; }
return Uuid.builder(); case PgOid.POINT: return Point.builder(); case PgOid.MONEY: return Decimal.builder(column.scale().get());
/** * Create a value for this schema using WKB * @param pointSchema a {@link Schema} instance which represents a point; may not be null * @param wkb the original Well-Known binary representation of the coordinate; may not be null * @param srid the coordinate reference system identifier; null if unset/unknown * @return a {@link Struct} which represents a Connect value for this schema; never null */ public static Struct createValue(Schema geomSchema, byte[] wkb, Integer srid) throws IllegalArgumentException { Struct result = Geometry.createValue(geomSchema, wkb, srid); double[] pt = parseWKBPoint(wkb); result.put(X_FIELD, pt[0]); result.put(Y_FIELD, pt[1]); return result; } }
Json.builder().optional().build(), Json.builder().optional().build(), Xml.builder().optional().build(), Uuid.builder().optional().build()); assertTableSchema("public.geom_table", "p", Point.builder().optional().build()); assertTableSchema("public.tstzrange_table", "unbounded_exclusive_range, bounded_inclusive_range", Schema.OPTIONAL_STRING_SCHEMA, Schema.OPTIONAL_STRING_SCHEMA);
@Test public void shouldConvertMySqlBytesToPoint() throws Exception { byte[] mysqlBytes = DatatypeConverter.parseHexBinary("000000000101000000e3a59bc420f01b4015a143a69d383240"); //This represents 'POINT(6.9845 18.22115554)' MySqlGeometry geom = MySqlGeometry.fromBytes(mysqlBytes); assertTrue(geom.isPoint()); assertEquals(geom.getSrid(), null); double[] coords = Point.parseWKBPoint(geom.getWkb()); assertEquals(coords[0], 6.9845, 0.0001); assertEquals(coords[1], 18.22115554, 0.0001); }