Skip to content

Commit 97cf4f2

Browse files
amygdaladavorbonaci
authored andcommitted
Clean up GameStats session branch for blog post
----Release Notes---- [] ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=113285110
1 parent b08ec20 commit 97cf4f2

File tree

1 file changed

+8
-7
lines changed
  • examples/src/main/java8/com/google/cloud/dataflow/examples/complete/game

1 file changed

+8
-7
lines changed

examples/src/main/java8/com/google/cloud/dataflow/examples/complete/game/GameStats.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.google.cloud.dataflow.sdk.options.Validation;
2828
import com.google.cloud.dataflow.sdk.runners.DataflowPipelineRunner;
2929
import com.google.cloud.dataflow.sdk.transforms.Aggregator;
30+
import com.google.cloud.dataflow.sdk.transforms.Combine;
3031
import com.google.cloud.dataflow.sdk.transforms.DoFn;
3132
import com.google.cloud.dataflow.sdk.transforms.DoFn.RequiresWindowAccess;
3233
import com.google.cloud.dataflow.sdk.transforms.MapElements;
@@ -309,26 +310,26 @@ public void processElement(ProcessContext c) {
309310

310311

311312
// [START DocInclude_SessionCalc]
312-
// Calculate the total score for the users per session-- that is, a burst of activity
313-
// separated by a gap from further activity. Find and record the mean session lengths.
313+
// Detect user sessions-- that is, a burst of activity separated by a gap from further
314+
// activity. Find and record the mean session lengths.
314315
// This information could help the game designers track the changing user engagement
315316
// as their set of games changes.
316317
userEvents
317318
.apply(Window.named("WindowIntoSessions")
318319
.<KV<String, Integer>>into(
319320
Sessions.withGapDuration(Duration.standardMinutes(options.getSessionGap())))
320-
.withOutputTimeFn(OutputTimeFns.outputAtEndOfWindow())
321-
.withAllowedLateness(Duration.ZERO))
322-
.apply("UserSessionSum", Sum.<String>integersPerKey())
321+
.withOutputTimeFn(OutputTimeFns.outputAtEndOfWindow()))
322+
// For this use, we care only about the existence of the session, not any particular
323+
// information aggregated over it, so the following is an efficient way to do that.
324+
.apply(Combine.perKey(x -> 0))
323325
// Get the duration per session.
324326
.apply("UserSessionActivity", ParDo.of(new UserSessionInfoFn()))
325327
// [END DocInclude_SessionCalc]
326328
// [START DocInclude_Rewindow]
327329
// Re-window to process groups of session sums according to when the sessions complete.
328330
.apply(Window.named("WindowToExtractSessionMean")
329331
.<Integer>into(
330-
FixedWindows.of(Duration.standardMinutes(options.getUserActivityWindowDuration())))
331-
.withAllowedLateness(Duration.ZERO))
332+
FixedWindows.of(Duration.standardMinutes(options.getUserActivityWindowDuration()))))
332333
// Find the mean session duration in each window.
333334
.apply(Mean.<Integer>globally().withoutDefaults())
334335
// Write this info to a BigQuery table.

0 commit comments

Comments
 (0)