Search in sources :

Example 1 with MutationGroup

use of org.apache.beam.sdk.io.gcp.spanner.MutationGroup in project java-docs-samples by GoogleCloudPlatform.

the class SpannerGroupWrite method main.

public static void main(String[] args) {
    Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class);
    Pipeline p = Pipeline.create(options);
    String instanceId = options.getInstanceId();
    String databaseId = options.getDatabaseId();
    String usersIdFile = options.getSuspiciousUsersFile();
    PCollection<String> suspiciousUserIds = p.apply(TextIO.read().from(usersIdFile));
    final Timestamp timestamp = Timestamp.now();
    // [START spanner_dataflow_writegroup]
    PCollection<MutationGroup> mutations = suspiciousUserIds.apply(MapElements.via(new SimpleFunction<String, MutationGroup>() {

        @Override
        public MutationGroup apply(String userId) {
            // Immediately block the user.
            Mutation userMutation = Mutation.newUpdateBuilder("Users").set("id").to(userId).set("state").to("BLOCKED").build();
            long generatedId = Hashing.sha1().newHasher().putString(userId, Charsets.UTF_8).putLong(timestamp.getSeconds()).putLong(timestamp.getNanos()).hash().asLong();
            // Add an entry to pending review requests.
            Mutation pendingReview = Mutation.newInsertOrUpdateBuilder("PendingReviews").set("id").to(// Must be deterministically generated.
            generatedId).set("userId").to(userId).set("action").to("REVIEW ACCOUNT").set("note").to("Suspicious activity detected.").build();
            return MutationGroup.create(userMutation, pendingReview);
        }
    }));
    mutations.apply(SpannerIO.write().withInstanceId(instanceId).withDatabaseId(databaseId).grouped());
    // [END spanner_dataflow_writegroup]
    p.run().waitUntilFinish();
}
Also used : PipelineOptions(org.apache.beam.sdk.options.PipelineOptions) MutationGroup(org.apache.beam.sdk.io.gcp.spanner.MutationGroup) SimpleFunction(org.apache.beam.sdk.transforms.SimpleFunction) Mutation(com.google.cloud.spanner.Mutation) Timestamp(com.google.cloud.Timestamp) Pipeline(org.apache.beam.sdk.Pipeline)

Aggregations

Timestamp (com.google.cloud.Timestamp)1 Mutation (com.google.cloud.spanner.Mutation)1 Pipeline (org.apache.beam.sdk.Pipeline)1 MutationGroup (org.apache.beam.sdk.io.gcp.spanner.MutationGroup)1 PipelineOptions (org.apache.beam.sdk.options.PipelineOptions)1 SimpleFunction (org.apache.beam.sdk.transforms.SimpleFunction)1