igorbernstein2 commented on code in PR #24015:
URL: https://github.com/apache/beam/pull/24015#discussion_r1108811797


##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigtable/BigtableConfigTranslator.java:
##########
@@ -0,0 +1,342 @@
+/*
+ * 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.io.gcp.bigtable;
+
+import com.google.api.gax.batching.BatchingSettings;
+import com.google.api.gax.core.FixedCredentialsProvider;
+import com.google.api.gax.core.NoCredentialsProvider;
+import com.google.api.gax.retrying.RetrySettings;
+import com.google.api.gax.rpc.FixedHeaderProvider;
+import com.google.api.gax.rpc.StubSettings;
+import com.google.auth.Credentials;
+import com.google.auth.oauth2.GoogleCredentials;
+import com.google.auth.oauth2.ServiceAccountJwtAccessCredentials;
+import com.google.cloud.bigtable.config.BigtableOptions;
+import com.google.cloud.bigtable.config.CredentialOptions;
+import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
+import com.google.cloud.bigtable.data.v2.stub.BigtableBatchingCallSettings;
+import io.grpc.internal.GrpcUtil;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.security.GeneralSecurityException;
+import java.security.KeyStore;
+import java.security.PrivateKey;
+import java.util.Objects;
+import javax.annotation.Nonnull;
+import org.apache.beam.sdk.extensions.gcp.options.GcpOptions;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.options.ValueProvider;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Strings;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
+import org.threeten.bp.Duration;
+
+/** Helper class to translate {@link BigtableOptions} to Bigtable Veneer 
settings. */
+@SuppressWarnings({
+  "nullness" // TODO(https://github.com/apache/beam/issues/20497)
+})
+class BigtableConfigTranslator {
+
+  /** Translate BigtableConfig and BigtableReadOptions to Veneer settings. */
+  static BigtableDataSettings translateReadToVeneerSettings(
+      @Nonnull BigtableConfig config,
+      @Nonnull BigtableReadOptions options,
+      @Nonnull PipelineOptions pipelineOptions) {
+    BigtableDataSettings.Builder settings = buildBigtableDataSettings(config, 
pipelineOptions);
+    return configureReadSettings(settings, options);
+  }
+
+  /** Translate BigtableConfig and BigtableWriteOptions to Veneer settings. */
+  static BigtableDataSettings translateWriteToVeneerSettings(
+      @Nonnull BigtableConfig config,
+      @Nonnull BigtableWriteOptions options,
+      @Nonnull PipelineOptions pipelineOptions) {
+
+    BigtableDataSettings.Builder settings = buildBigtableDataSettings(config, 
pipelineOptions);
+    return configureWriteSettings(settings, options);
+  }
+
+  private static BigtableDataSettings.Builder buildBigtableDataSettings(
+      BigtableConfig config, PipelineOptions pipelineOptions) {
+    BigtableDataSettings.Builder dataBuilder;
+    if (!Strings.isNullOrEmpty(config.getEmulatorHost())) {
+      String hostAndPort = config.getEmulatorHost();
+      try {
+        int lastIndexOfCol = hostAndPort.lastIndexOf(":");
+        int port = Integer.parseInt(hostAndPort.substring(lastIndexOfCol + 1));
+        dataBuilder =
+            BigtableDataSettings.newBuilderForEmulator(
+                hostAndPort.substring(0, lastIndexOfCol), port);
+      } catch (NumberFormatException | IndexOutOfBoundsException ex) {
+        throw new RuntimeException("Invalid host/port in BigtableConfig " + 
hostAndPort);
+      }
+    } else {
+      dataBuilder = BigtableDataSettings.newBuilder();
+    }
+
+    // Configure target
+    dataBuilder
+        .setProjectId(Objects.requireNonNull(config.getProjectId().get()))
+        .setInstanceId(Objects.requireNonNull(config.getInstanceId().get()));
+    if (config.getAppProfileId() != null) {
+      
dataBuilder.setAppProfileId(Objects.requireNonNull(config.getAppProfileId().get()));

Review Comment:
   What happens if the value in the ValueProvider is not set? I think your if 
statement needs to check the value as well: if (config.getAppProfileId() != 
null && !Strings.isNotNullOrEmpty(config.getAppProfileId()))



-- 
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: [email protected]

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

Reply via email to