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();
}
Aggregations