Dataflow write group

Use the Dataflow connector to write data by using a MutationGroup class to ensure that a group of mutations is applied atomically.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample


To learn how to install and use the client library for Cloud Spanner, see Cloud Spanner client libraries.

PCollection<MutationGroup> mutations = suspiciousUserIds
    .apply(MapElements.via(new SimpleFunction<String, MutationGroup>() {

      public MutationGroup apply(String userId) {
        // Immediately block the user.
        Mutation userMutation = Mutation.newUpdateBuilder("Users")
        long generatedId = Hashing.sha1().newHasher()
            .putString(userId, Charsets.UTF_8)

        // Add an entry to pending review requests.
        Mutation pendingReview = Mutation.newInsertOrUpdateBuilder("PendingReviews")
            .set("id").to(generatedId)  // Must be deterministically generated.
            .set("action").to("REVIEW ACCOUNT")
            .set("note").to("Suspicious activity detected.")

        return MutationGroup.create(userMutation, pendingReview);


What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.