stankiewicz commented on code in PR #41769:
URL: https://github.com/apache/beam/pull/41769#discussion_r4771410106
##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/SpannerWriteSchemaTransformProvider.java:
##########
@@ -143,6 +188,63 @@ private static class NoOutputDoFn<T> extends DoFn<T, Row> {
public void process(@SuppressWarnings("unused") ProcessContext c) {}
}
+ /** Builds the {@link SpannerIO.Write} that this transform applies, from
its configuration. */
+ @VisibleForTesting
+ SpannerIO.Write buildWrite() {
+ boolean handleErrors =
ErrorHandling.hasOutput(configuration.getErrorHandling());
+ SpannerIO.Write write =
+ SpannerIO.write()
+ .withProjectId(configuration.getProjectId())
+ .withDatabaseId(configuration.getDatabaseId())
+ .withInstanceId(configuration.getInstanceId())
+ .withFailureMode(handleErrors ? FailureMode.REPORT_FAILURES :
FailureMode.FAIL_FAST);
+
+ String priority = configuration.getPriority();
+ if (priority != null) {
+ write =
+ write.withSpannerConfig(
+ write
+ .getSpannerConfig()
+
.withRpcPriority(RpcPriority.valueOf(priority.toUpperCase(Locale.ROOT))));
+ }
+ Long maxCommitDelayMillis = configuration.getMaxCommitDelayMillis();
+ if (maxCommitDelayMillis != null) {
+ write = write.withMaxCommitDelay(maxCommitDelayMillis);
+ }
+ String transactionTag = configuration.getTransactionTag();
+ if (!Strings.isNullOrEmpty(transactionTag)) {
+ write = write.withTransactionTag(transactionTag);
+ }
+ Boolean excludeTxnFromChangeStreams =
configuration.getExcludeTxnFromChangeStreams();
+ if (excludeTxnFromChangeStreams != null) {
+ write =
write.withExcludeTxnFromChangeStreams(excludeTxnFromChangeStreams);
+ }
+ if (serviceFactory != null) {
+ write = write.withServiceFactory(serviceFactory);
+ }
+ if (Boolean.TRUE.equals(configuration.getEmitWriteResults())) {
+ // Turn on commit stats on the Write itself instead of using
withWriteResults(), which
+ // returns only the WriteResults. This way the same SpannerWriteResult
also keeps the
+ // failed mutations, so the write results and error_handling can be
used together.
+ write = write.toBuilder().setCommitStats(true).build();
+ }
+ return write;
+ }
+
+ static Row writeResultsToRow(WriteResults writeResults) {
+ return Row.withSchema(WRITE_RESULTS_SCHEMA)
+ .withFieldValue("mutation_count", writeResults.getMutationCount())
+ .withFieldValue("commit_timestamp",
toInstant(writeResults.getCommitTimestamp()))
+ .withFieldValue("snapshot_timestamp",
toInstant(writeResults.getSnapshotTimestamp()))
+ .build();
+ }
+
+ private static @Nullable Instant toInstant(@Nullable Timestamp timestamp) {
+ return timestamp == null
+ ? null
+ : Instant.ofEpochSecond(timestamp.getSeconds(),
timestamp.getNanos());
Review Comment:
Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos())
.truncatedTo(ChronoUnit.MICROS);
otherwise MicrosInstant will throw AssertionError
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]