|
40 | 40 | import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.DataChangeRecord; |
41 | 41 | import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.HeartbeatRecord; |
42 | 42 | import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.InitialPartition; |
| 43 | +import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.PartitionStartRecord; |
| 44 | +import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.PartitionEndRecord; |
| 45 | +import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.PartitionEventRecord; |
43 | 46 | import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.Mod; |
44 | 47 | import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.ModType; |
45 | 48 | import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.PartitionMetadata; |
46 | 49 | import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.TypeCode; |
47 | 50 | import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.ValueCaptureType; |
| 51 | +import java.util.Arrays; |
48 | 52 | import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Sets; |
| 53 | +import org.slf4j.Logger; |
| 54 | +import org.slf4j.LoggerFactory; |
49 | 55 |
|
50 | 56 | /** |
51 | 57 | * This class is responsible for transforming a {@link Struct} to a {@link List} of {@link |
|
56 | 62 | */ |
57 | 63 | public class ChangeStreamRecordMapper { |
58 | 64 |
|
| 65 | + private static final Logger LOG = LoggerFactory.getLogger(ChangeStreamRecordMapper.class); |
59 | 66 | private static final String DATA_CHANGE_RECORD_COLUMN = "data_change_record"; |
60 | 67 | private static final String HEARTBEAT_RECORD_COLUMN = "heartbeat_record"; |
61 | 68 | private static final String CHILD_PARTITIONS_RECORD_COLUMN = "child_partitions_record"; |
@@ -223,12 +230,45 @@ public List<ChangeStreamRecord> toChangeStreamRecords( |
223 | 230 | return Collections.singletonList( |
224 | 231 | toChangeStreamRecordJson(partition, resultSet.getPgJsonb(0), resultSetMetadata)); |
225 | 232 | } |
| 233 | + |
| 234 | + if (containOnlyProtoType(resultSet.getCurrentRowAsStruct())) { |
| 235 | + return Arrays.asList( |
| 236 | + toV2ChangeStreamRecord(partition, resultSetMetadata, resultSet.getCurrentRowAsStruct())); |
| 237 | + } |
| 238 | + |
226 | 239 | // In GoogleSQL, change stream records are returned as an array of structs. |
227 | 240 | return resultSet.getCurrentRowAsStruct().getStructList(0).stream() |
228 | 241 | .flatMap(struct -> toChangeStreamRecord(partition, struct, resultSetMetadata)) |
229 | 242 | .collect(Collectors.toList()); |
230 | 243 | } |
231 | 244 |
|
| 245 | + boolean containOnlyProtoType(Struct currentRow){ |
| 246 | + LOG.info("changliiu containOnlyProtoType1"); |
| 247 | + if(currentRow.getColumnCount() !=1 || currentRow.isNull(0)){ |
| 248 | + LOG.info("changliiu containOnlyProtoType2"); |
| 249 | + return false; |
| 250 | + } |
| 251 | + com.google.cloud.spanner.Type columnType = currentRow.getColumnType(i); |
| 252 | + if (columnType.getCode() == com.google.cloud.spanner.Type.Code.PROTO) { |
| 253 | + LOG.info("changliiu containOnlyProtoType3"); |
| 254 | + return true; |
| 255 | + } |
| 256 | + LOG.info("changliiu containOnlyProtoType4"); |
| 257 | + return false; |
| 258 | + } |
| 259 | + |
| 260 | + ChangeStreamRecord toV2ChangeStreamRecord(PartitionMetadata partition, |
| 261 | + ChangeStreamResultSetMetadata resultSetMetadata, |
| 262 | + Struct currentRow) { |
| 263 | + com.google.spanner.v1.ChangeStreamRecord changeStreamRecord = |
| 264 | + currentRow.getProtoMessage(0, |
| 265 | + com.google.spanner.v1.ChangeStreamRecord.getDefaultInstance()); |
| 266 | + // todo finish this. |
| 267 | + LOG.info("changliiu toV2ChangeStreamRecord"); |
| 268 | + return new PartitionEndRecord(Timestamp.MIN_VALUE, "xxxx", |
| 269 | + changeStreamRecordMetadataFrom(partition, Timestamp.MIN_VALUE, resultSetMetadata)); |
| 270 | + } |
| 271 | + |
232 | 272 | Stream<ChangeStreamRecord> toChangeStreamRecord( |
233 | 273 | PartitionMetadata partition, Struct row, ChangeStreamResultSetMetadata resultSetMetadata) { |
234 | 274 |
|
|
0 commit comments