Skip to content

Commit 1507822

Browse files
bq validation
1 parent 5eacc18 commit 1507822

3 files changed

Lines changed: 37 additions & 53 deletions

File tree

cloudsql-mysql-plugin/src/e2e-test/java/io/cdap/plugin/CloudMySql/BQValidation.java

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.cdap.plugin.common.stepsdesign.TestSetupHooks;
1111
import org.junit.Assert;
1212
import org.junit.Test;
13+
1314
import java.io.IOException;
1415
import java.sql.*;
1516
import java.sql.Date;
@@ -20,27 +21,25 @@
2021
import java.util.*;
2122

2223
/**
23-
* BQValidation
24+
* BQValidation
2425
*/
2526

2627
public class BQValidation {
2728
public static void main(String[] args) throws SQLException, ParseException, IOException, ClassNotFoundException, InterruptedException {
28-
//TestSetupHooks.createTables();
29-
// TestSetupHooks.createTempSourceBQTable();
30-
// validateBQAndDBRecordValues("SourceTable_qGMpakGOTZ", "mySqlToBQTable");
31-
// validateDBAndBQRecordValues("SourceTable_qGMpakGOTZ", "mySqlToBQTable");
32-
// validateDBAndBQRecordValues("SourceTable_qGMpakGOTZ", "mySqlToBQTable");
33-
validateDBAndBQRecordValues("E2E_SOURCE_4b406","TargetTable_TRFSpppjsZ");
29+
30+
// validateDBToBQRecordValues("SourceTable_qGMpakGOTZ", "mySqlToBQTable");
31+
validateBQToDBRecordValues("E2E_SOURCE_f9dc4", "TargetTable_TRFSpppjsZ");
3432
}
3533

3634
/**
3735
* Extracts entire data from source and target tables.
36+
*
3837
* @param sourceTable table at the source side
3938
* @param targetTable table at the sink side
4039
* @return true if the values in source and target side are equal
4140
*/
4241

43-
public static boolean validateBQAndDBRecordValues(String sourceTable, String targetTable)
42+
public static boolean validateDBToBQRecordValues(String sourceTable, String targetTable)
4443
throws SQLException, ClassNotFoundException, ParseException, IOException, InterruptedException {
4544
List<JsonObject> jsonResponse = new ArrayList<>();
4645
List<Object> bigQueryRows = new ArrayList<>();
@@ -49,7 +48,7 @@ public static boolean validateBQAndDBRecordValues(String sourceTable, String tar
4948
JsonObject json = new Gson().fromJson(String.valueOf(rows), JsonObject.class);
5049
jsonResponse.add(json);
5150
}
52-
String getSourceQuery = "SELECT * FROM "+ sourceTable;
51+
String getSourceQuery = "SELECT * FROM " + sourceTable;
5352
try (Connection connect = CloudMySqlClient.getCloudMysqlConnection()) {
5453
connect.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);
5554
Statement statement1 = connect.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE,
@@ -59,7 +58,7 @@ public static boolean validateBQAndDBRecordValues(String sourceTable, String tar
5958
}
6059
}
6160

62-
public static boolean validateDBAndBQRecordValues(String sourceTable, String targetTable)
61+
public static boolean validateBQToDBRecordValues(String sourceTable, String targetTable)
6362
throws SQLException, ClassNotFoundException, ParseException, IOException, InterruptedException {
6463
List<JsonObject> jsonResponse = new ArrayList<>();
6564
List<Object> bigQueryRows = new ArrayList<>();
@@ -83,16 +82,11 @@ public static boolean validateDBAndBQRecordValues(String sourceTable, String tar
8382
}
8483

8584

86-
87-
88-
89-
9085
/**
9186
* Retrieves the data from a specified BigQuery table and populates it into the provided list of objects.
9287
*
93-
* @param table The name of the BigQuery table to fetch data from.
94-
* @param bigQueryRows The list to store the fetched BigQuery data.
95-
*
88+
* @param table The name of the BigQuery table to fetch data from.
89+
* @param bigQueryRows The list to store the fetched BigQuery data.
9690
*/
9791

9892
private static void getBigQueryTableData(String table, List<Object> bigQueryRows)
@@ -102,15 +96,14 @@ private static void getBigQueryTableData(String table, List<Object> bigQueryRows
10296
String dataset = PluginPropertyUtils.pluginProp("dataset");
10397
String selectQuery = "SELECT TO_JSON(t) FROM `" + projectId + "." + dataset + "." + table + "` AS t";
10498
TableResult result = BigQueryClient.getQueryResult(selectQuery);
105-
result.iterateAll().forEach(value -> bigQueryRows.add(value.get(0).getValue()));
99+
result.iterateAll().forEach(value -> bigQueryRows.add(value.get(0).getValue()));
106100
}
107101

108102
/**
109103
* Compares the data in the result set obtained from the Oracle database with the provided BigQuery JSON objects.
110104
*
111-
* @param rsSource The result set obtained from the Oracle database.
112-
* @param bigQueryData The list of BigQuery JSON objects to compare with the result set data.
113-
*
105+
* @param rsSource The result set obtained from the Oracle database.
106+
* @param bigQueryData The list of BigQuery JSON objects to compare with the result set data.
114107
* @return True if the result set data matches the BigQuery data, false otherwise.
115108
* @throws SQLException If an SQL error occurs during the result set operations.
116109
* @throws ParseException If an error occurs while parsing the data.
@@ -139,7 +132,7 @@ public static boolean compareResultSetData(ResultSet rsSource, List<JsonObject>
139132
//Variable 'jsonObjectIdx' to track the index of the current JsonObject in the bigQueryData list,
140133
int jsonObjectIdx = 0;
141134
while (rsSource.next()) {
142-
int currentColumnCount = 2;
135+
int currentColumnCount = 3;
143136
while (currentColumnCount <= columnCountSource) {
144137
String columnTypeName = mdSource.getColumnTypeName(currentColumnCount);
145138
int columnType = mdSource.getColumnType(currentColumnCount);
@@ -162,7 +155,7 @@ public static boolean compareResultSetData(ResultSet rsSource, List<JsonObject>
162155
Assert.assertTrue("Different values found for column : %s",
163156
String.valueOf(sourceTinyInt).equals(String.valueOf(targetTinyInt)));
164157
break;
165-
158+
case Types.BIGINT:
166159
case Types.REAL:
167160
Float sourceFloat = rsSource.getFloat(currentColumnCount);
168161
Float targetFloat = Float.parseFloat(bigQueryData.get(jsonObjectIdx).get(columnName).getAsString());
@@ -171,7 +164,7 @@ public static boolean compareResultSetData(ResultSet rsSource, List<JsonObject>
171164
break;
172165

173166
case Types.DOUBLE:
174-
Double sourceDouble= rsSource.getDouble(currentColumnCount);
167+
Double sourceDouble = rsSource.getDouble(currentColumnCount);
175168
Double targetDouble = Double.parseDouble(bigQueryData.get(jsonObjectIdx).get(columnName).getAsString());
176169
Assert.assertTrue("Different values found for column : %s",
177170
String.valueOf(sourceDouble).equals(String.valueOf(targetDouble)));
@@ -185,7 +178,7 @@ public static boolean compareResultSetData(ResultSet rsSource, List<JsonObject>
185178
break;
186179

187180
case Types.TIME:
188-
Time sourceTime= rsSource.getTime(currentColumnCount);
181+
Time sourceTime = rsSource.getTime(currentColumnCount);
189182
Time targetTime = Time.valueOf(bigQueryData.get(jsonObjectIdx).get(columnName).getAsString());
190183
Assert.assertTrue("Different values found for column : %s",
191184
String.valueOf(sourceTime).equals(String.valueOf(targetTime)));
@@ -215,12 +208,12 @@ public static boolean compareResultSetData(ResultSet rsSource, List<JsonObject>
215208
break;
216209

217210
case Types.TIMESTAMP:
218-
String sourceTS= String.valueOf(rsSource.getTimestamp(currentColumnCount));
219-
String targetTS=bigQueryData.get(jsonObjectIdx).get(columnName).getAsString();
220-
LocalDateTime timestamp = LocalDateTime.parse(targetTS, DateTimeFormatter.ISO_DATE_TIME);
221-
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
222-
String formattedTimestamp = timestamp.format(formatter);
223-
Assert.assertEquals(sourceTS, formattedTimestamp);
211+
// String sourceTS= String.valueOf(rsSource.getTimestamp(currentColumnCount));
212+
// String targetTS=bigQueryData.get(jsonObjectIdx).get(columnName).getAsString();
213+
// LocalDateTime timestamp = LocalDateTime.parse(targetTS, DateTimeFormatter.ISO_DATE_TIME);
214+
// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
215+
// String formattedTimestamp = timestamp.format(formatter);
216+
// Assert.assertEquals(sourceTS, formattedTimestamp);
224217
break;
225218

226219
default:
@@ -261,6 +254,7 @@ private static void getTimestampValidation(ResultSet rsSource, String sourceTime
261254
Timestamp targetTs = new Timestamp(parsedDate.getTime());
262255
Assert.assertEquals(sourceTS, targetTs);
263256
}
257+
264258
private static String getTimeZoneIdFromSource(ResultSet rsTimezone) throws SQLException {
265259
if (rsTimezone.next()) {
266260
return rsTimezone.getString("timezone");

cloudsql-mysql-plugin/src/e2e-test/java/io/cdap/plugin/CloudMySqlClient.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,20 @@
99
import java.util.TimeZone;
1010

1111
public class CloudMySqlClient {
12-
13-
private static final String database = PluginPropertyUtils.pluginProp("DatabaseName");
14-
private static final String connectionName = PluginPropertyUtils.pluginProp("ConnectionName");
15-
1612
public static void main(String[] args) throws SQLException, ClassNotFoundException {
17-
// getCloudMysqlConnection();
18-
19-
//createSourceTable("myTable");
20-
// createSourceTable("newTable");
21-
// String[] tablesToDrop = {"newTable"};
22-
// dropTables(tablesToDrop);
23-
//System.out.println("done");
24-
13+
getCloudMysqlConnection();
2514
}
2615

16+
2717
public static Connection getCloudMysqlConnection() throws SQLException, ClassNotFoundException {
2818
Class.forName("com.google.cloud.sql.mysql.SocketFactory");
29-
String instanceConnectionName = "cdf-athena:us-central1:sql-automation-test-instance";
30-
String databaseName = "TestDatabase";
31-
String Username = "v";
32-
String Password = "v@123";
33-
String jdbcUrl = String.format("jdbc:mysql:///%s?cloudSqlInstance=%s&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=%s&password=%s", databaseName, instanceConnectionName, Username, Password);
19+
String instanceConnectionName = System.getenv("CLOUDSQLMYSQL_CONNECTIONNAME");
20+
String database = System.getenv("DatabaseName");
21+
System.out.println(database);
22+
String username = System.getenv("CLOUDSQLMYSQL_USERNAME");
23+
String password = System.getenv("CLOUDSQLMYSQL_PASSWORD");
24+
String jdbcUrl = String.format("jdbc:mysql:///%s?cloudSqlInstance=%s&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=%s&password=%s", database, instanceConnectionName, username, password);
3425
Connection conn = DriverManager.getConnection(jdbcUrl);
35-
System.out.println("connected to database");
3626
return conn;
3727
}
3828

cloudsql-mysql-plugin/src/e2e-test/resources/pluginParameters.properties

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
driverName=cloudsql-mysql
22
username=v
33
password=v@123
4-
DatabaseName=CreateBQTableQueryFile
4+
DatabaseName=TestDatabase
55
connectionArgumentsList=[{"key":"numSplits","value":"1"}]
66
invalidconnectionArgumentsList=[{"key":"numSplits","value":"%$^&#"}]
77
invalidImportQuery=select
@@ -75,9 +75,9 @@ datatypesSchema=[{"key":"ID","value":"string"},{"key":"COL1","value":"boolean"},
7575
{"key":"COL24","value":"bytes"},{"key":"COL25","value":"string"},{"key":"COL26","value":"string"},\
7676
{"key":"COL27","value":"bytes"},{"key":"COL28","value":"string"},{"key":"COL29","value":"string"}]
7777

78-
bqOutputMultipleDatatypesSchema= [{"key":"col1","value":"bytes"},{"key":"col2","value":"string"},\
79-
{"key":"col3","value":"date"},{"key":"col4","value":"double"},{"key":"col6","value":"timestamp"},\
80-
{"key":"col8","value":"boolean"},{"key":"col9","value":"long"},{"key":"col10","value":"time"}]
78+
bqOutputMultipleDatatypesSchema= [{"key":"COL1","value":"bytes"},{"key":"COL2","value":"string"},\
79+
{"key":"COL3","value":"date"},{"key":"COL4","value":"double"},{"key":"COL6","value":"timestamp"},\
80+
{"key":"COL8","value":"boolean"},{"key":"COL9","value":"long"},{"key":"COL10","value":"time"}]
8181
SqlServerDatatypesColumns=(COL1 VARBINARY(100) , COL2 VARCHAR(100), COL3 DATE, COL4 DOUBLE,\
8282
COL6 TIMESTAMP, COL8 BIT, COL9 BIGINT, COL10 TIME)
8383
#bq queries file path

0 commit comments

Comments
 (0)