forked from GoogleCloudDataproc/spark-spanner-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
Implement overwrite mode #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
MaxKsyunz
wants to merge
10
commits into
integ/save_mode_simple
Choose a base branch
from
dev/save_mode_simple_override
base: integ/save_mode_simple
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7be6f38
Implement overwrite mode as `delete from ... where true` sent as part…
MaxKsyunz 78b312e
Fix-up formatting
MaxKsyunz ff9c8ec
Fix-up formatting II
MaxKsyunz f6063ad
No comments, no problems.
MaxKsyunz 897dc68
Update spark-3.1-spanner-lib/src/main/java/com/google/cloud/spark/spa…
MaxKsyunz 4565ae7
Replace print statements with logs.
MaxKsyunz 820feee
add support for overwriteMode option for Overwrite save mode, support…
MaxKsyunz 8fba7ef
Update spark-3.1-spanner-lib/src/main/java/com/google/cloud/spark/spa…
MaxKsyunz 511c411
Rework recreate mode to use same DDL generation code as SpannerCatalo…
MaxKsyunz c87acb3
Update spark-3.1-spanner-lib/src/main/java/com/google/cloud/spark/spa…
MaxKsyunz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,19 +14,127 @@ | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| package com.google.cloud.spark.spanner; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import com.google.cloud.spanner.DatabaseAdminClient; | ||||||||||||||||||||||||
| import com.google.cloud.spanner.DatabaseClient; | ||||||||||||||||||||||||
| import com.google.cloud.spanner.DatabaseId; | ||||||||||||||||||||||||
| import com.google.cloud.spanner.Dialect; | ||||||||||||||||||||||||
| import com.google.cloud.spanner.Spanner; | ||||||||||||||||||||||||
| import com.google.cloud.spanner.SpannerException; | ||||||||||||||||||||||||
| import com.google.cloud.spanner.Statement; | ||||||||||||||||||||||||
| import com.google.cloud.spanner.connection.Connection; | ||||||||||||||||||||||||
| import java.util.Collections; | ||||||||||||||||||||||||
| import java.util.concurrent.ExecutionException; | ||||||||||||||||||||||||
| import org.apache.spark.sql.connector.catalog.Identifier; | ||||||||||||||||||||||||
| import org.apache.spark.sql.connector.write.BatchWrite; | ||||||||||||||||||||||||
| import org.apache.spark.sql.connector.write.LogicalWriteInfo; | ||||||||||||||||||||||||
| import org.apache.spark.sql.connector.write.SupportsTruncate; | ||||||||||||||||||||||||
| import org.apache.spark.sql.connector.write.WriteBuilder; | ||||||||||||||||||||||||
| import org.apache.spark.sql.types.StructType; | ||||||||||||||||||||||||
| import org.apache.spark.sql.util.CaseInsensitiveStringMap; | ||||||||||||||||||||||||
| import org.slf4j.Logger; | ||||||||||||||||||||||||
| import org.slf4j.LoggerFactory; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public class SpannerWriteBuilder implements WriteBuilder { | ||||||||||||||||||||||||
| public class SpannerWriteBuilder implements WriteBuilder, SupportsTruncate { | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| private static final Logger log = LoggerFactory.getLogger(SpannerWriteBuilder.class); | ||||||||||||||||||||||||
| private final LogicalWriteInfo info; | ||||||||||||||||||||||||
| private final StructType schema; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public SpannerWriteBuilder(LogicalWriteInfo info) { | ||||||||||||||||||||||||
| this.info = info; | ||||||||||||||||||||||||
| this.schema = info.schema(); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||
| public BatchWrite buildForBatch() { | ||||||||||||||||||||||||
| return new SpannerBatchWrite(info); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||
| public WriteBuilder truncate() { | ||||||||||||||||||||||||
| CaseInsensitiveStringMap opts = new CaseInsensitiveStringMap(this.info.options()); | ||||||||||||||||||||||||
| String overwriteMode = opts.getOrDefault("overwriteMode", "truncate"); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (overwriteMode.equalsIgnoreCase("recreate")) { | ||||||||||||||||||||||||
| recreateTable(opts); | ||||||||||||||||||||||||
| } else if (overwriteMode.equalsIgnoreCase("truncate")) { | ||||||||||||||||||||||||
| truncateTable(opts); | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| throw new SpannerConnectorException( | ||||||||||||||||||||||||
| SpannerErrorCode.INVALID_ARGUMENT, | ||||||||||||||||||||||||
| "Unsupported overwriteMode '" | ||||||||||||||||||||||||
| + overwriteMode | ||||||||||||||||||||||||
| + "'. Supported modes are 'recreate' and 'truncate'."); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return this; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| private void recreateTable(CaseInsensitiveStringMap opts) { | ||||||||||||||||||||||||
| String instanceId = SpannerUtils.getRequiredOption(opts, "instanceId"); | ||||||||||||||||||||||||
| String databaseId = SpannerUtils.getRequiredOption(opts, "databaseId"); | ||||||||||||||||||||||||
| String tableName = SpannerUtils.getRequiredOption(opts, "table"); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| try (Spanner spanner = SpannerUtils.buildSpannerOptions(opts).getService()) { | ||||||||||||||||||||||||
| DatabaseAdminClient dbAdminClient = spanner.getDatabaseAdminClient(); | ||||||||||||||||||||||||
| Dialect dialect; | ||||||||||||||||||||||||
| try (Connection conn = SpannerUtils.connectionFromProperties(opts.asCaseSensitiveMap())) { | ||||||||||||||||||||||||
| dialect = conn.getDialect(); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| SpannerInformationSchema schemaInfo = SpannerInformationSchema.create(dialect); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| String dropDdl = schemaInfo.dropTableDdl(tableName); | ||||||||||||||||||||||||
| dbAdminClient | ||||||||||||||||||||||||
| .updateDatabaseDdl(instanceId, databaseId, Collections.singletonList(dropDdl), null) | ||||||||||||||||||||||||
| .get(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Create the table. | ||||||||||||||||||||||||
| Identifier ident = Identifier.of(new String[0], tableName); | ||||||||||||||||||||||||
| String createDdl = schemaInfo.createTableDdl(ident, this.schema); | ||||||||||||||||||||||||
| dbAdminClient | ||||||||||||||||||||||||
| .updateDatabaseDdl(instanceId, databaseId, Collections.singletonList(createDdl), null) | ||||||||||||||||||||||||
| .get(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| } catch (InterruptedException | ExecutionException e) { | ||||||||||||||||||||||||
| throw new SpannerConnectorException( | ||||||||||||||||||||||||
| SpannerErrorCode.DDL_EXCEPTION, "Error recreating table " + tableName, e); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
Comment on lines
+98
to
+101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When an
Suggested change
|
||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| private void truncateTable(CaseInsensitiveStringMap opts) { | ||||||||||||||||||||||||
| String projectId = SpannerUtils.getRequiredOption(opts, "projectId"); | ||||||||||||||||||||||||
| String instanceId = SpannerUtils.getRequiredOption(opts, "instanceId"); | ||||||||||||||||||||||||
| String databaseId = SpannerUtils.getRequiredOption(opts, "databaseId"); | ||||||||||||||||||||||||
| String tableName = SpannerUtils.getRequiredOption(opts, "table"); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| try (Spanner spanner = SpannerUtils.buildSpannerOptions(opts).getService()) { | ||||||||||||||||||||||||
| DatabaseClient dbClient = | ||||||||||||||||||||||||
| spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId)); | ||||||||||||||||||||||||
| Dialect dialect = dbClient.getDialect(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| SpannerInformationSchema informationSchema = SpannerInformationSchema.create(dialect); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| truncateTable(dbClient, tableName, informationSchema); | ||||||||||||||||||||||||
| } catch (Exception e) { | ||||||||||||||||||||||||
| throw new SpannerConnectorException( | ||||||||||||||||||||||||
| SpannerErrorCode.DDL_EXCEPTION, "Error truncating table " + tableName, e); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| private long truncateTable( | ||||||||||||||||||||||||
| DatabaseClient dbClient, String tableName, SpannerInformationSchema informationSchema) { | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Statement statement = informationSchema.truncateTableDml(tableName); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| // Execute partitioned update. This is a blocking call. | ||||||||||||||||||||||||
| long deletedRowCount = dbClient.executePartitionedUpdate(statement); | ||||||||||||||||||||||||
| log.info("Successfully deleted " + deletedRowCount + " rows."); | ||||||||||||||||||||||||
| return deletedRowCount; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| } catch (SpannerException e) { | ||||||||||||||||||||||||
| log.error("Failed to execute Partitioned DML on table: " + tableName, e); | ||||||||||||||||||||||||
| throw e; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.