nastra commented on code in PR #12808:
URL: https://github.com/apache/iceberg/pull/12808#discussion_r2054374124


##########
bigquery/src/main/java/org/apache/iceberg/gcp/bigquery/BigQueryMetastoreCatalog.java:
##########
@@ -0,0 +1,402 @@
+/*
+ * 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.iceberg.gcp.bigquery;
+
+import com.google.api.services.bigquery.model.Dataset;
+import com.google.api.services.bigquery.model.DatasetList.Datasets;
+import com.google.api.services.bigquery.model.DatasetReference;
+import com.google.api.services.bigquery.model.ExternalCatalogDatasetOptions;
+import com.google.api.services.bigquery.model.TableReference;
+import com.google.cloud.ServiceOptions;
+import com.google.cloud.bigquery.BigQueryOptions;
+import java.io.IOException;
+import java.security.GeneralSecurityException;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import org.apache.hadoop.conf.Configurable;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.BaseMetastoreCatalog;
+import org.apache.iceberg.CatalogProperties;
+import org.apache.iceberg.CatalogUtil;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableOperations;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.SupportsNamespaces;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.NoSuchNamespaceException;
+import org.apache.iceberg.exceptions.NoSuchTableException;
+import org.apache.iceberg.exceptions.ServiceFailureException;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.io.FileIO;
+import 
org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.base.Strings;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.util.LocationUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Iceberg Bigquery Metastore Catalog implementation. */
+public final class BigQueryMetastoreCatalog extends BaseMetastoreCatalog
+    implements SupportsNamespaces, Configurable {
+
+  // User provided properties.
+  public static final String PROPERTIES_KEY_GCP_PROJECT = "gcp-project";
+  public static final String PROPERTIES_KEY_GCP_LOCATION = "gcp-location";
+  public static final String PROPERTIES_KEY_FILTER_UNSUPPORTED_TABLES = 
"filter-unsupported-tables";
+  public static final String PROPERTIES_KEY_TESTING_ENABLED = 
"testing-enabled";
+
+  public static final String HIVE_METASTORE_WAREHOUSE_DIR = 
"hive.metastore.warehouse.dir";
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(BigQueryMetastoreCatalog.class);
+
+  private static final String DEFAULT_GCP_LOCATION = "us";
+
+  private String catalogPluginName;
+  private Map<String, String> catalogProperties;
+  private FileIO fileIO;
+  private Configuration conf;
+  private String projectId;
+  private String location;
+  private BigQueryMetastoreClient client;
+  private boolean filterUnsupportedTables;
+
+  // Must have a no-arg constructor to be dynamically loaded
+  // initialize(String name, Map<String, String> properties) will be called to 
complete
+  // initialization
+  public BigQueryMetastoreCatalog() {}
+
+  @Override
+  public void initialize(String inputName, Map<String, String> properties) {
+    if (!properties.containsKey(PROPERTIES_KEY_GCP_PROJECT)) {
+      throw new ValidationException("GCP project must be specified");
+    }
+    projectId = properties.get(PROPERTIES_KEY_GCP_PROJECT);
+    location = properties.getOrDefault(PROPERTIES_KEY_GCP_LOCATION, 
DEFAULT_GCP_LOCATION);
+    boolean testingEnabled =
+        
Boolean.parseBoolean(properties.getOrDefault(PROPERTIES_KEY_TESTING_ENABLED, 
"false"));
+
+    BigQueryOptions options =
+        BigQueryOptions.newBuilder()
+            .setProjectId(projectId)
+            .setLocation(location)
+            .setRetrySettings(ServiceOptions.getDefaultRetrySettings())
+            .build();
+
+    try {
+      if (testingEnabled) {
+        client = new FakeBigQueryMetastoreClient(options);
+
+      } else {
+        client = new BigQueryMetastoreClientImpl(options);
+      }
+    } catch (IOException e) {
+      throw new ServiceFailureException(e, "Creating BigQuery client failed");
+    } catch (GeneralSecurityException e) {
+      throw new ValidationException(e, "Creating BigQuery client failed due to 
a security issue");
+    }
+    initialize(inputName, properties, projectId, location, client);
+  }
+
+  @VisibleForTesting
+  void initialize(
+      String inputName,
+      Map<String, String> properties,
+      String initialProjectId,
+      String initialLocation,
+      BigQueryMetastoreClient bigQueryMetaStoreClient) {
+    this.catalogPluginName = inputName;
+    this.catalogProperties = ImmutableMap.copyOf(properties);
+    this.projectId = initialProjectId;
+    this.location = initialLocation;
+    this.client =
+        Preconditions.checkNotNull(bigQueryMetaStoreClient, "BigQuery client 
can not be null");

Review Comment:
   IMO it's better to use `Preconditions.checkArgument(null != ..., "Invalid 
BigQuery client: null")` because it indicates that a wrong argument was passed 
in a clearer way than a NPE



-- 
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: issues-unsubscr...@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to