[ 
https://issues.apache.org/jira/browse/BEAM-5427?focusedWorklogId=150954&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-150954
 ]

ASF GitHub Bot logged work on BEAM-5427:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 03/Oct/18 22:14
            Start Date: 03/Oct/18 22:14
    Worklog Time Spent: 10m 
      Work Description: swegner closed pull request #6439: [BEAM-5427] Fix and 
update sample code for CombineFn.
URL: https://github.com/apache/beam/pull/6439
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/Combine.java 
b/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/Combine.java
index 2c65f94d09a..20314fcba3c 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/Combine.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/Combine.java
@@ -254,10 +254,26 @@ private Combine() {
    *
    * <pre>{@code
    * public class AverageFn extends CombineFn<Integer, AverageFn.Accum, 
Double> {
-   *   public static class Accum {
+   *   public static class Accum implements Serializable {
    *     int sum = 0;
    *     int count = 0;
+   *
+   *    {@literal@}Override
+   *     public boolean equals(Object other) {
+   *       if (other == null) return false;
+   *       if (other == this) return true;
+   *       if (!(other instanceof Accum))return false;
+   *
+   *
+   *       Accum o = (Accum)other;
+   *       if (this.sum != o.sum || this.count != o.count) {
+   *         return false;
+   *       } else {
+   *         return true;
+   *       }
+   *     }
    *   }
+   *
    *   public Accum createAccumulator() {
    *     return new Accum();
    *   }
@@ -289,6 +305,24 @@ private Combine() {
    * arbitrary tree structure. Commutativity is required because any order of 
the input values is
    * ignored when breaking up input values into groups.
    *
+   * <h3>Note on Data Encoding</h3>
+   *
+   * <p>Some form of data encoding is required when using custom types in a 
CombineFn which do not
+   * have well-known coders. The sample code above uses a custom Accumulator 
which gets coder by
+   * implementing {@link java.io.Serializable}. By doing this, we are relying 
on the generic {@link
+   * org.apache.beam.sdk.coders.CoderProvider}, which is able to provide a 
coder for any {@link
+   * java.io.Serializable} if applicable. In cases where {@link 
java.io.Serializable} is not
+   * efficient, or inapplicable, in general there are two alternatives for 
encoding:
+   *
+   * <ul>
+   *   <li>Default {@link org.apache.beam.sdk.coders.CoderRegistry}. For 
example, implement a coder
+   *       class explicitly and use the {@code @DefaultCoder} tag. See the 
{@link
+   *       org.apache.beam.sdk.coders.CoderRegistry} for the numerous ways in 
which to bind a type
+   *       to a coder.
+   *   <li>CombineFn specific way. While extending CombineFn, overwrite both 
{@link
+   *       #getAccumulatorCoder} and {@link #getDefaultOutputCoder}.
+   * </ul>
+   *
    * @param <InputT> type of input values
    * @param <AccumT> type of mutable accumulator values
    * @param <OutputT> type of output values


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 150954)
    Time Spent: 3h 50m  (was: 3h 40m)

> Fix sample code (AverageFn) in Combine.java
> -------------------------------------------
>
>                 Key: BEAM-5427
>                 URL: https://issues.apache.org/jira/browse/BEAM-5427
>             Project: Beam
>          Issue Type: Improvement
>          Components: examples-java
>            Reporter: Ruoyun Huang
>            Assignee: Ruoyun Huang
>            Priority: Minor
>          Time Spent: 3h 50m
>  Remaining Estimate: 0h
>
> Sample code missing coder. 
> In its current state, job run fails with Coder missing error. 



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to