-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
source-mysql : chunking queries impl (#29109)
- Loading branch information
Showing
9 changed files
with
306 additions
and
92 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
...tors/source-mysql/src/main/java/io/airbyte/integrations/source/mysql/MySqlQueryUtils.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,73 @@ | ||
package io.airbyte.integrations.source.mysql; | ||
|
||
import static io.airbyte.integrations.source.relationaldb.RelationalDbQueryUtils.getFullyQualifiedTableNameWithQuoting; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.google.common.base.Preconditions; | ||
import io.airbyte.db.jdbc.JdbcDatabase; | ||
import io.airbyte.db.jdbc.JdbcUtils; | ||
import io.airbyte.protocol.models.AirbyteStreamNameNamespacePair; | ||
import io.airbyte.protocol.models.v0.ConfiguredAirbyteStream; | ||
import java.sql.SQLException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class MySqlQueryUtils { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(MySqlQueryUtils.class); | ||
public record TableSizeInfo(Long tableSize, Long avgRowLength) { } | ||
|
||
public static final String TABLE_ESTIMATE_QUERY = | ||
""" | ||
SELECT | ||
(data_length + index_length) as %s, | ||
AVG_ROW_LENGTH as %s | ||
FROM | ||
information_schema.tables | ||
WHERE | ||
table_schema = '%s' AND table_name = '%s'; | ||
"""; | ||
|
||
public static final String TABLE_SIZE_BYTES_COL = "TotalSizeBytes"; | ||
public static final String AVG_ROW_LENGTH = "AVG_ROW_LENGTH"; | ||
|
||
public static Map<AirbyteStreamNameNamespacePair, TableSizeInfo> getTableSizeInfoForStreams(final JdbcDatabase database, | ||
final List<ConfiguredAirbyteStream> streams, | ||
final String quoteString) { | ||
final Map<AirbyteStreamNameNamespacePair, TableSizeInfo> tableSizeInfoMap = new HashMap<>(); | ||
streams.forEach(stream -> { | ||
try { | ||
final String name = stream.getStream().getName(); | ||
final String namespace = stream.getStream().getNamespace(); | ||
final String fullTableName = | ||
getFullyQualifiedTableNameWithQuoting(name, namespace, quoteString); | ||
final List<JsonNode> tableEstimateResult = getTableEstimate(database, namespace, name); | ||
Preconditions.checkState(tableEstimateResult.size() == 1); | ||
final long tableEstimateBytes = tableEstimateResult.get(0).get(TABLE_SIZE_BYTES_COL).asLong(); | ||
final long avgTableRowSizeBytes = tableEstimateResult.get(0).get(AVG_ROW_LENGTH).asLong(); | ||
LOGGER.info("Stream {} size estimate is {}, average row size estimate is {}", fullTableName, tableEstimateBytes, avgTableRowSizeBytes); | ||
final TableSizeInfo tableSizeInfo = new TableSizeInfo(tableEstimateBytes, avgTableRowSizeBytes); | ||
final AirbyteStreamNameNamespacePair namespacePair = | ||
new AirbyteStreamNameNamespacePair(stream.getStream().getName(), stream.getStream().getNamespace()); | ||
tableSizeInfoMap.put(namespacePair, tableSizeInfo); | ||
} catch (final SQLException e) { | ||
LOGGER.warn("Error occurred while attempting to estimate sync size", e); | ||
} | ||
}); | ||
return tableSizeInfoMap; | ||
} | ||
|
||
private static List<JsonNode> getTableEstimate(final JdbcDatabase database, final String namespace, final String name) | ||
throws SQLException { | ||
// Construct the table estimate query. | ||
final String tableEstimateQuery = | ||
String.format(TABLE_ESTIMATE_QUERY, TABLE_SIZE_BYTES_COL, AVG_ROW_LENGTH, namespace, name); | ||
LOGGER.info("table estimate query: {}", tableEstimateQuery); | ||
final List<JsonNode> jsonNodes = database.bufferedResultSetQuery(conn -> conn.createStatement().executeQuery(tableEstimateQuery), | ||
resultSet -> JdbcUtils.getDefaultSourceOperations().rowToJson(resultSet)); | ||
Preconditions.checkState(jsonNodes.size() == 1); | ||
return jsonNodes; | ||
} | ||
} |
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
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
Oops, something went wrong.