matthiasa4 commented on a change in pull request #13645: URL: https://github.com/apache/beam/pull/13645#discussion_r601733599
########## 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: Good point. The API was returning faulty responses (hence the test is currently Ignored) - it would require inspecting the `OperationFuture`-object returned from the API which should return the original resources as per the [docs](https://cloud.google.com/recommendations-ai/docs/reference/rest/v1beta1/projects.locations.catalogs.eventStores.operations#Operation) (so measuring the size of the array returned should be the way to go here). Should I remove the test for now or leave a comment with this information? ########## 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 reason for keeping it explicit was that the Recommendation AI APIs may be used in another project from where the Beam pipeline is running (or not in Google Cloud at all). Thoughts? ########## 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: I think I get the idea: the API would be cleaner and more centralised into the `RecommendationAIIO` class which makes sense to me. I got my inspiration from AnnotateText in the ml package which takes a similar approach to me. Will have a think a come with a proposal. -- 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]
