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.

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample


To learn how to install and use the client library for Cloud Spanner, see the 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