l -> l.stream() .map(kv -> WriteGrouped.decode(kv.getValue())) .collect(Collectors.toList())) .collect(Collectors.toList());
mutationGroups .stream() .map(mg -> KV.of((byte[]) null, WriteGrouped.encode(mg))) .collect(Collectors.toList());
mutationGroups .stream() .map(mg -> KV.of((byte[]) null, WriteGrouped.encode(mg))) .collect(Collectors.toList());
.getValue() .stream() .map(kv -> WriteGrouped.decode(kv.getValue())) .collect(Collectors.toList()); assertThat(
MutationGroup mg = decode(kv.getValue());
@ProcessElement public void processElement(ProcessContext c) throws Exception { SpannerSchema spannerSchema = c.sideInput(schemaView); MutationKeyEncoder encoder = new MutationKeyEncoder(spannerSchema); MutationGroup mg = c.element(); long groupSize = MutationSizeEstimator.sizeOf(mg); long groupCells = MutationCellCounter.countOf(spannerSchema, mg); synchronized (this) { if (((batchCells + groupCells) > maxNumMutations) || (batchSizeBytes + groupSize) > maxBatchSizeBytes) { c.output(sortAndGetList()); initSorter(); } mutationsToSort.add(KV.of(encoder.encodeTableNameAndKey(mg.primary()), encode(mg))); batchSizeBytes += groupSize; batchCells += groupCells; } } }
/** Same transform but can be applied to {@link PCollection} of {@link MutationGroup}. */ public WriteGrouped grouped() { return new WriteGrouped(this); }
@Override public SpannerWriteResult expand(PCollection<Mutation> input) { getSpannerConfig().validate(); return input .apply("To mutation group", ParDo.of(new ToMutationGroupFn())) .apply("Write mutations to Cloud Spanner", new WriteGrouped(this)); }