-
Notifications
You must be signed in to change notification settings - Fork 971
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added sample implmentation and cross functional UT for custom transfo…
…rmation (#1732) * Added sample implmentation and cross functional UT for custom transformation * added UTs * minor fix
- Loading branch information
1 parent
0701be1
commit d7db191
Showing
3 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
v2/spanner-custom-shard/src/main/java/com/custom/CustomTransformationFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (C) 2024 Google LLC | ||
* | ||
* Licensed 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 com.custom; | ||
|
||
import com.google.cloud.teleport.v2.spanner.exceptions.InvalidTransformationException; | ||
import com.google.cloud.teleport.v2.spanner.utils.ISpannerMigrationTransformer; | ||
import com.google.cloud.teleport.v2.spanner.utils.MigrationTransformationRequest; | ||
import com.google.cloud.teleport.v2.spanner.utils.MigrationTransformationResponse; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* This is a sample class to be implemented by the customer. All the relevant dependencies have been | ||
* added and users need to implement the toSpannerRow() and/or toSourceRow() method for forward and | ||
* reverse replication flows respectively | ||
*/ | ||
public class CustomTransformationFetcher implements ISpannerMigrationTransformer { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(CustomShardIdFetcher.class); | ||
|
||
@Override | ||
public void init(String customParameters) { | ||
LOG.info("init called with {}", customParameters); | ||
} | ||
|
||
@Override | ||
public MigrationTransformationResponse toSpannerRow(MigrationTransformationRequest request) | ||
throws InvalidTransformationException { | ||
if (request.getTableName().equals("Customers")) { | ||
Map<String, Object> requestRow = request.getRequestRow(); | ||
Map<String, Object> responseRow = new HashMap<>(); | ||
|
||
responseRow.put( | ||
"full_name", requestRow.get("first_name") + " " + requestRow.get("last_name")); | ||
responseRow.put("migration_shard_id", request.getShardId() + "_" + requestRow.get("id")); | ||
MigrationTransformationResponse response = | ||
new MigrationTransformationResponse(responseRow, false); | ||
return response; | ||
} | ||
return new MigrationTransformationResponse(null, false); | ||
} | ||
|
||
@Override | ||
public MigrationTransformationResponse toSourceRow(MigrationTransformationRequest request) | ||
throws InvalidTransformationException { | ||
if (request.getTableName().equals("Customers")) { | ||
Map<String, Object> requestRow = request.getRequestRow(); | ||
Map<String, Object> responseRow = new HashMap<>(); | ||
String fullName = (String) requestRow.get("full_name"); | ||
String[] nameParts = fullName.split(" ", 2); | ||
responseRow.put("first_name", nameParts[0]); | ||
responseRow.put("last_name", nameParts[1]); | ||
String migrationShardId = (String) requestRow.get("migration_shard_id"); | ||
String[] idParts = migrationShardId.split("_", 2); | ||
responseRow.put("id", idParts[1]); | ||
MigrationTransformationResponse response = | ||
new MigrationTransformationResponse(responseRow, false); | ||
return response; | ||
} | ||
return new MigrationTransformationResponse(null, false); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
v2/spanner-custom-shard/src/test/java/com/custom/CustomTransformationFetcherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (C) 2024 Google LLC | ||
* | ||
* Licensed 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 com.custom; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import com.google.cloud.teleport.v2.spanner.exceptions.InvalidTransformationException; | ||
import com.google.cloud.teleport.v2.spanner.utils.MigrationTransformationRequest; | ||
import com.google.cloud.teleport.v2.spanner.utils.MigrationTransformationResponse; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.junit.Test; | ||
|
||
/** Tests for CustomTransformationFetcher class. */ | ||
public class CustomTransformationFetcherTest { | ||
@Test | ||
public void endToEndTest() throws InvalidTransformationException { | ||
CustomTransformationFetcher customTransformationFetcher = new CustomTransformationFetcher(); | ||
Map<String, Object> requestRow = new HashMap<>(); | ||
requestRow.put("first_name", "abc"); | ||
requestRow.put("last_name", "xyz"); | ||
requestRow.put("id", "123"); | ||
MigrationTransformationRequest request = | ||
new MigrationTransformationRequest("Customers", requestRow, "ls1", ""); | ||
MigrationTransformationResponse response = customTransformationFetcher.toSpannerRow(request); | ||
MigrationTransformationResponse response2 = | ||
customTransformationFetcher.toSourceRow( | ||
new MigrationTransformationRequest("Customers", response.getResponseRow(), "ls1", "")); | ||
assertEquals(request.getRequestRow(), response2.getResponseRow()); | ||
} | ||
|
||
@Test | ||
public void testToSourceRowInvalidTableName() throws InvalidTransformationException { | ||
CustomTransformationFetcher customTransformationFetcher = new CustomTransformationFetcher(); | ||
Map<String, Object> requestRow = new HashMap<>(); | ||
requestRow.put("first_name", "abc"); | ||
requestRow.put("last_name", "xyz"); | ||
requestRow.put("id", "123"); | ||
MigrationTransformationRequest request = | ||
new MigrationTransformationRequest("xyz", requestRow, "ls1", ""); | ||
MigrationTransformationResponse response = customTransformationFetcher.toSourceRow(request); | ||
assertEquals(response.getResponseRow(), null); | ||
} | ||
|
||
@Test | ||
public void testToSpannerRowInvalidTableName() throws InvalidTransformationException { | ||
CustomTransformationFetcher customTransformationFetcher = new CustomTransformationFetcher(); | ||
Map<String, Object> requestRow = new HashMap<>(); | ||
requestRow.put("first_name", "abc"); | ||
requestRow.put("last_name", "xyz"); | ||
requestRow.put("id", "123"); | ||
MigrationTransformationRequest request = | ||
new MigrationTransformationRequest("xyz", requestRow, "ls1", ""); | ||
MigrationTransformationResponse response = customTransformationFetcher.toSpannerRow(request); | ||
assertEquals(response.getResponseRow(), null); | ||
} | ||
} |