You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "kennknowles (via GitHub)" <gi...@apache.org> on 2023/04/26 02:46:34 UTC

[GitHub] [beam] kennknowles commented on a diff in pull request #13645: [BEAM-11289] [Java] Integrate Google Cloud Recommendations AI functio…

kennknowles commented on code in PR #13645:
URL: https://github.com/apache/beam/pull/13645#discussion_r1177279972


##########
sdks/java/extensions/ml/src/main/java/org/apache/beam/sdk/extensions/ml/RecommendationAIWriteUserEvent.java:
##########
@@ -0,0 +1,146 @@
+/*
+ * 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.ml;
+
+import com.google.api.client.json.GenericJson;
+import com.google.api.gax.rpc.ApiException;
+import com.google.auto.value.AutoValue;
+import com.google.cloud.recommendationengine.v1beta1.EventStoreName;
+import com.google.cloud.recommendationengine.v1beta1.UserEvent;
+import com.google.cloud.recommendationengine.v1beta1.UserEventServiceClient;
+import com.google.protobuf.util.JsonFormat;
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
+import javax.annotation.Nullable;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.PCollectionTuple;
+import org.apache.beam.sdk.values.TupleTag;
+import org.apache.beam.sdk.values.TupleTagList;
+import org.json.JSONObject;
+
+/**
+ * A {@link PTransform} using the Recommendations AI API (https://cloud.google.com/recommendations).
+ * Takes an input {@link PCollection} of {@link GenericJson}s and converts them to and creates
+ * {@link UserEvent}s.
+ *
+ * <p>It is possible to provide a catalog name to which you want to add the user event (defaults to
+ * "default_catalog"). It is possible to provide a event store to which you want to add the user
+ * event (defaults to "default_event_store").
+ */
+@AutoValue
+@SuppressWarnings({"nullness"})
+public abstract class RecommendationAIWriteUserEvent
+    extends PTransform<PCollection<GenericJson>, PCollectionTuple> {
+
+  public static final TupleTag<UserEvent> SUCCESS_TAG = new TupleTag<UserEvent>() {};
+  public static final TupleTag<UserEvent> FAILURE_TAG = new TupleTag<UserEvent>() {};
+
+  static Builder newBuilder() {
+    return new AutoValue_RecommendationAIWriteUserEvent.Builder()
+        .setCatalogName("default_catalog")
+        .setEventStore("default_event_store");
+  }
+
+  /** @return ID of Google Cloud project to be used for creating user events. */
+  public abstract @Nullable String projectId();
+
+  /** @return Name of the catalog where the user events will be created. */
+  public abstract @Nullable String catalogName();
+
+  /** @return Name of the event store where the user events will be created. */
+  public abstract @Nullable String eventStore();
+
+  /**
+   * The transform converts the contents of input PCollection into {@link UserEvent}s and then calls
+   * the Recommendation AI service to create the user event.
+   *
+   * @param input input PCollection
+   * @return PCollectionTuple with successful and failed {@link UserEvent}s
+   */
+  @Override
+  public PCollectionTuple expand(PCollection<GenericJson> input) {
+    return input.apply(
+        ParDo.of(new WriteUserEvent(projectId(), catalogName(), eventStore()))

Review Comment:
   Based on the usage here, it seems like `projectId` cannot actually be null. I see that it is explicitly nullable, though. Am I missing something?



-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org