rezarokni commented on a change in pull request #12973:
URL: https://github.com/apache/beam/pull/12973#discussion_r534093432



##########
File path: 
sdks/java/extensions/zetasketch/src/main/java/org/apache/beam/sdk/extensions/zetasketch/ApproximateCountDistinct.java
##########
@@ -0,0 +1,287 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.extensions.zetasketch;
+
+import com.google.auto.value.AutoValue;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.KvCoder;
+import org.apache.beam.sdk.extensions.zetasketch.HllCount.Init.Builder;
+import org.apache.beam.sdk.transforms.Contextful;
+import org.apache.beam.sdk.transforms.Contextful.Fn;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ProcessFunction;
+import org.apache.beam.sdk.transforms.display.DisplayData;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@code PTransform}s for estimating the number of distinct elements in a 
{@code PCollection}, or
+ * the number of distinct values associated with each key in a {@code 
PCollection} of {@code KV}s.
+ *
+ * <p>We make use of the {@link HllCount} implementation for this transform. 
Please use {@link
+ * HllCount} directly if you need access to the sketches.
+ *
+ * <p>If the object is not one of {@link Byte[]} {@link Integer} {@link 
Double} {@link String} make
+ * use of {@link Globally#via} or {@link PerKey#via}
+ *
+ * <h3>Examples</h3>
+ *
+ * <h4>Example 1: Approximate Count of Longs {@code PCollection<Long>} and 
specify precision</h4>
+ *
+ * <pre>{@Code
+ *     p.apply("Int", Create.of(ints)).apply("IntHLL", 
ApproximateCountDistinct.globally()
+ *     .withPercision(PRECISION));
+ *     }</pre>
+ *
+ * <h4>Example 2: Approximate Count of Key Value {@code 
PCollection<KV<Integer,Foo>>}</h4>
+ *
+ * <pre>{@code
+ * PCollection<KV<Integer, Long>> result =
+ * p.apply("Int", Create.of(ints)).apply("LongHLL", 
ApproximateCountDistinct.perKey());
+ * }</pre>
+ *
+ * <h4>Example 3: Approximate Count of Key Value {@code 
PCollection<KV<Integer,Foo>>}</h4>
+ *
+ * <pre>{@code
+ * PCollection<KV<Integer, Foo>> approxResultInteger =
+ *          p.apply("Int", Create.of(Foo))
+ *          .apply("IntHLL", ApproximateCountDistinct.<Integer, KV<Integer, 
Integer>>perKey()
+ *          .via(x -> KV.of(x.getKey(), (long) x.hashCode())));
+ * }</pre>
+ */
+@Experimental
+public class ApproximateCountDistinct {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(ApproximateCountDistinct.class);
+
+  private static final List<TypeDescriptor<?>> HLL_IMPLEMENTED_TYPES =
+      ImmutableList.of(
+          TypeDescriptors.strings(),
+          TypeDescriptors.longs(),
+          TypeDescriptors.integers(),
+          new TypeDescriptor<byte[]>() {});
+
+  public static <T> Globally<T> globally() {
+    return new AutoValue_ApproximateCountDistinct_Globally.Builder<T>()
+        .setPrecision(HllCount.DEFAULT_PRECISION)
+        .build();
+  }
+
+  public static <K, V> PerKey<K, V> perKey() {
+    return new AutoValue_ApproximateCountDistinct_PerKey.Builder<K, V>()
+        .setPrecision(HllCount.DEFAULT_PRECISION)
+        .build();
+  }
+
+  /////////////////////////////////////////////////////////////////////////////
+
+  /**
+   * {@code PTransform} for estimating the number of distinct elements in a 
{@code PCollection}.
+   *
+   * @param <T> the type of the elements in the input {@code PCollection}
+   */
+  @AutoValue
+  public abstract static class Globally<T> extends PTransform<PCollection<T>, 
PCollection<Long>> {
+
+    public abstract int getPrecision();
+
+    public abstract Builder<T> toBuilder();
+
+    @Nullable
+    public abstract Contextful<Fn<T, Long>> getMapping();
+
+    @AutoValue.Builder
+    public abstract static class Builder<T> {
+
+      public abstract Builder<T> setPrecision(int precision);
+
+      public abstract Builder<T> setMapping(Contextful<Fn<T, Long>> value);
+
+      public abstract Globally<T> build();
+    }
+
+    public Globally<T> via(ProcessFunction<T, Long> fn) {
+
+      return toBuilder().setMapping(Contextful.<T, Long>fn(fn)).build();
+    }
+
+    public <V> Globally<V> withPercision(Integer withPercision) {
+      this.toBuilder().setPrecision(withPercision).build();

Review comment:
       Thanx. 

##########
File path: 
sdks/java/extensions/zetasketch/src/main/java/org/apache/beam/sdk/extensions/zetasketch/ApproximateCountDistinct.java
##########
@@ -0,0 +1,287 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.extensions.zetasketch;
+
+import com.google.auto.value.AutoValue;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.KvCoder;
+import org.apache.beam.sdk.extensions.zetasketch.HllCount.Init.Builder;
+import org.apache.beam.sdk.transforms.Contextful;
+import org.apache.beam.sdk.transforms.Contextful.Fn;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ProcessFunction;
+import org.apache.beam.sdk.transforms.display.DisplayData;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@code PTransform}s for estimating the number of distinct elements in a 
{@code PCollection}, or
+ * the number of distinct values associated with each key in a {@code 
PCollection} of {@code KV}s.
+ *
+ * <p>We make use of the {@link HllCount} implementation for this transform. 
Please use {@link
+ * HllCount} directly if you need access to the sketches.
+ *
+ * <p>If the object is not one of {@link Byte[]} {@link Integer} {@link 
Double} {@link String} make
+ * use of {@link Globally#via} or {@link PerKey#via}
+ *
+ * <h3>Examples</h3>
+ *
+ * <h4>Example 1: Approximate Count of Longs {@code PCollection<Long>} and 
specify precision</h4>
+ *
+ * <pre>{@Code
+ *     p.apply("Int", Create.of(ints)).apply("IntHLL", 
ApproximateCountDistinct.globally()
+ *     .withPercision(PRECISION));
+ *     }</pre>
+ *
+ * <h4>Example 2: Approximate Count of Key Value {@code 
PCollection<KV<Integer,Foo>>}</h4>
+ *
+ * <pre>{@code
+ * PCollection<KV<Integer, Long>> result =
+ * p.apply("Int", Create.of(ints)).apply("LongHLL", 
ApproximateCountDistinct.perKey());

Review comment:
       Done.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to