azexcy commented on code in PR #23168: URL: https://github.com/apache/shardingsphere/pull/23168#discussion_r1059699874
########## kernel/data-pipeline/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/util/ColumnValueConvertUtil.java: ########## @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.data.pipeline.cdc.util; + +import com.google.protobuf.BoolValue; +import com.google.protobuf.ByteString; +import com.google.protobuf.BytesValue; +import com.google.protobuf.DoubleValue; +import com.google.protobuf.FloatValue; +import com.google.protobuf.Int32Value; +import com.google.protobuf.Int64Value; +import com.google.protobuf.Message; +import com.google.protobuf.StringValue; +import lombok.extern.slf4j.Slf4j; +import org.apache.shardingsphere.data.pipeline.cdc.protocol.response.BigDecimalValue; +import org.apache.shardingsphere.data.pipeline.cdc.protocol.response.BigIntegerValue; +import org.apache.shardingsphere.data.pipeline.cdc.protocol.response.ClobValue; +import org.apache.shardingsphere.data.pipeline.cdc.protocol.response.NullValue; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.sql.Clob; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZonedDateTime; +import java.util.Date; + +/** + * Column value convert util. + */ +@Slf4j +public final class ColumnValueConvertUtil { + + /** + * Convert java object to protobuf message. + * + * @param object object + * @return protobuf message + */ + public static Message convertToProtobufMessage(final Object object) { + if (null == object) { + return NullValue.newBuilder().build(); + } + if (object instanceof Integer) { + return Int32Value.newBuilder().setValue((int) object).build(); + } + if (object instanceof Short) { + return Int32Value.newBuilder().setValue(((Short) object).intValue()).build(); + } + if (object instanceof Byte) { + return Int32Value.newBuilder().setValue(((Byte) object).intValue()).build(); + } + if (object instanceof Long) { + return Int64Value.newBuilder().setValue((long) object).build(); + } + if (object instanceof BigInteger) { + return BigIntegerValue.newBuilder().setValue(ByteString.copyFrom(((BigInteger) object).toByteArray())).build(); + } + if (object instanceof Float) { + return FloatValue.newBuilder().setValue((float) object).build(); + } + if (object instanceof Double) { + return DoubleValue.newBuilder().setValue((double) object).build(); + } + if (object instanceof BigDecimal) { + return BigDecimalValue.newBuilder().setValue(object.toString()).build(); + } + if (object instanceof String) { + return StringValue.newBuilder().setValue(object.toString()).build(); + } + if (object instanceof Boolean) { + return BoolValue.newBuilder().setValue((boolean) object).build(); + } + if (object instanceof byte[]) { + return BytesValue.newBuilder().setValue(ByteString.copyFrom((byte[]) object)).build(); + } + if (object instanceof Date) { + return converToProtobufTimestamp((Date) object); + } + if (object instanceof LocalDateTime) { + return converToProtobufTimestamp(Timestamp.valueOf((LocalDateTime) object)); + } + if (object instanceof LocalDate) { + return converToProtobufTimestamp(Timestamp.valueOf(((LocalDate) object).atStartOfDay())); + } + if (object instanceof ZonedDateTime) { + return converToProtobufTimestamp(Timestamp.valueOf(((ZonedDateTime) object).toLocalDateTime())); + } + if (object instanceof Instant) { + Instant instant = (Instant) object; + return com.google.protobuf.Timestamp.newBuilder().setSeconds(instant.getEpochSecond()).setNanos(instant.getNano()).build(); + } + if (object instanceof Clob) { + Clob clob = (Clob) object; + try { + return ClobValue.newBuilder().setValue(clob.getSubString(1, (int) clob.length())).build(); + } catch (final SQLException ex) { + log.error("get clob length failed", ex); + throw new RuntimeException(ex); + } + } + log.warn(" {} can't convert to protobuf message, value {}", object.getClass().getName(), object); + throw new UnsupportedOperationException(String.format("Not support convert %s to protobuf message now", object.getClass())); Review Comment: There is a solution to convert to json, first convert java Object to json ``` private static Message fromJson(final String json) { Builder structBuilder = Struct.newBuilder(); try { JsonFormat.parser().ignoringUnknownFields().merge(json, structBuilder); } catch (final InvalidProtocolBufferException ex) { throw new RuntimeException(ex); } return structBuilder.build(); } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
