You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2021/03/10 23:00:32 UTC

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

pabloem commented on a change in pull request #13645:
URL: https://github.com/apache/beam/pull/13645#discussion_r591934268



##########
File path: sdks/java/extensions/ml/src/main/java/org/apache/beam/sdk/extensions/ml/RecommendationAICreateCatalogItem.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.CatalogItem;
+import com.google.cloud.recommendationengine.v1beta1.CatalogName;
+import com.google.cloud.recommendationengine.v1beta1.CatalogServiceClient;
+import com.google.protobuf.util.JsonFormat;
+import java.io.IOException;
+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 CatalogItem}s. It outputs a PCollectionTuple which will contain the successfully created
+ * and failed catalog items.
+ *
+ * <p>It is possible to provide a catalog name to which you want to add the catalog item (defaults
+ * to "default_catalog").
+ */
+@AutoValue
+@SuppressWarnings({"nullness"})
+public abstract class RecommendationAICreateCatalogItem

Review comment:
       in Beam we have these 'SystemIO' transforms, and each transform has a static method that can be used to return the specific PTransform. e.g.:
   
   ```
   BigQueryIO
     - read()
     - write()
   
   FileIO
     - match
     - readMatches
     - write
     - writeAll
     - readAll
   ````
   
   Furthermore, the Beam transforms don't expose their builders, and instead have methods that set parameters and return a fully configured PTransform without needing to call `build()`. Do you think this might make sense for these?
   
   E.g.:
   
   ```
   RecommendationAIIO
    - Method: createCatalogItems   (or createItems?)
    - Method: importCatalogItems
    - Method: importUserEvents
    - Method: writeUserEvents
    - Method: predictAll   (?)
   ```
   
   Your classes could remain the same - they would become perhaps not-public, and accessed via `RecommendationAIIO` class (or some other name)

##########
File path: sdks/java/extensions/ml/src/main/java/org/apache/beam/sdk/extensions/ml/RecommendationAICreateCatalogItem.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.CatalogItem;
+import com.google.cloud.recommendationengine.v1beta1.CatalogName;
+import com.google.cloud.recommendationengine.v1beta1.CatalogServiceClient;
+import com.google.protobuf.util.JsonFormat;
+import java.io.IOException;
+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 CatalogItem}s. It outputs a PCollectionTuple which will contain the successfully created
+ * and failed catalog items.
+ *
+ * <p>It is possible to provide a catalog name to which you want to add the catalog item (defaults
+ * to "default_catalog").
+ */
+@AutoValue
+@SuppressWarnings({"nullness"})
+public abstract class RecommendationAICreateCatalogItem
+    extends PTransform<PCollection<GenericJson>, PCollectionTuple> {
+
+  /** @return ID of Google Cloud project to be used for creating catalog items. */
+  public abstract String projectId();

Review comment:
       the project ID may be provided by pipeline options, so consider making this parameter optional.

##########
File path: sdks/java/extensions/ml/src/test/java/org/apache/beam/sdk/extensions/ml/RecommendationAIUserEventIT.java
##########
@@ -0,0 +1,104 @@
+/*
+ * 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 static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import com.google.api.client.json.GenericJson;
+import com.google.cloud.recommendationengine.v1beta1.UserEvent;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.beam.sdk.extensions.gcp.options.GcpOptions;
+import org.apache.beam.sdk.testing.PAssert;
+import org.apache.beam.sdk.testing.TestPipeline;
+import org.apache.beam.sdk.transforms.Create;
+import org.apache.beam.sdk.transforms.SerializableFunction;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollectionTuple;
+import org.junit.Ignore;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public class RecommendationAIUserEventIT {
+  @Rule public TestPipeline testPipeline = TestPipeline.create();
+
+  public static GenericJson getUserEvent() {
+    GenericJson userInfo = new GenericJson().set("visitorId", "1");
+    return new GenericJson().set("eventType", "page-visit").set("userInfo", userInfo);
+  }
+
+  @Test
+  public void createUserEvent() {
+    String projectId = testPipeline.getOptions().as(GcpOptions.class).getProject();
+
+    PCollectionTuple createUserEventResult =
+        testPipeline
+            .apply(
+                Create.of(Arrays.asList(getUserEvent()))
+                    .withCoder(GenericJsonCoder.of(GenericJson.class)))
+            .apply(RecommendationAIWriteUserEvent.newBuilder().setProjectId(projectId).build());
+    PAssert.that(createUserEventResult.get(RecommendationAIWriteUserEvent.SUCCESS_TAG))
+        .satisfies(new VerifyUserEventResult(1));
+    testPipeline.run().waitUntilFinish();
+  }
+
+  @Ignore("Import method causing issues")
+  @Test
+  public void importUserEvents() {
+    String projectId = testPipeline.getOptions().as(GcpOptions.class).getProject();
+    ArrayList<KV<String, GenericJson>> userEvents = new ArrayList<>();
+    userEvents.add(KV.of("123", getUserEvent()));
+    userEvents.add(KV.of("123", getUserEvent()));
+
+    PCollectionTuple importUserEventResult =
+        testPipeline
+            .apply(Create.of(userEvents))
+            .apply(RecommendationAIImportUserEvents.newBuilder().setProjectId(projectId).build());
+    PAssert.that(importUserEventResult.get(RecommendationAIWriteUserEvent.SUCCESS_TAG))
+        .satisfies(new VerifyUserEventResult(2));
+    testPipeline.run().waitUntilFinish();
+  }

Review comment:
       What's the difference between Import and Write user events?

##########
File path: sdks/java/extensions/ml/src/main/java/org/apache/beam/sdk/extensions/ml/RecommendationAICreateCatalogItem.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.CatalogItem;
+import com.google.cloud.recommendationengine.v1beta1.CatalogName;
+import com.google.cloud.recommendationengine.v1beta1.CatalogServiceClient;
+import com.google.protobuf.util.JsonFormat;
+import java.io.IOException;
+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 CatalogItem}s. It outputs a PCollectionTuple which will contain the successfully created
+ * and failed catalog items.
+ *
+ * <p>It is possible to provide a catalog name to which you want to add the catalog item (defaults
+ * to "default_catalog").
+ */
+@AutoValue
+@SuppressWarnings({"nullness"})
+public abstract class RecommendationAICreateCatalogItem

Review comment:
       let me know what you think about this idea




----------------------------------------------------------------
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:
users@infra.apache.org