io.cdap.plugin.gcp.bigquery.sink.lib.BigQueryTableSchema Maven / Gradle / Ivy
/*
* Copyright © 2023 Cask Data, Inc.
*
* Licensed 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 io.cdap.plugin.gcp.bigquery.sink.lib;
import com.google.api.services.bigquery.model.TableFieldSchema;
import com.google.api.services.bigquery.model.TableSchema;
import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.List;
/**
* Wrapper for BigQuery {@link TableSchema}.
*
* This class is used to avoid client code to depend on BigQuery API classes, so that there is no
* potential conflict between different versions of BigQuery API libraries in the client.
*
* @see TableSchema
* - Moved from com.google.cloud.hadoop.io.bigquery bigquery-connector library
* - Update the table regex as per the requirement of flexible column names
* TODO(PLUGIN-1725): Migrate to Spark BigQuery connector.
*/
public class BigQueryTableSchema {
private final TableSchema tableSchema;
public BigQueryTableSchema() {
this.tableSchema = new TableSchema();
}
BigQueryTableSchema(TableSchema tableSchema) {
Preconditions.checkNotNull(tableSchema, "tableSchema is null.");
this.tableSchema = tableSchema;
}
/** @see TableSchema#setFields(List) */
public BigQueryTableSchema setFields(List bigQueryTableFields) {
Preconditions.checkArgument(!bigQueryTableFields.isEmpty(), "Empty fields.");
List fields = new ArrayList<>(bigQueryTableFields.size());
for (BigQueryTableFieldSchema bigQueryTableField : bigQueryTableFields) {
fields.add(bigQueryTableField.get());
}
tableSchema.setFields(fields);
return this;
}
@Override
public int hashCode() {
return tableSchema.hashCode();
}
@Override
public boolean equals(Object object) {
if (!(object instanceof com.google.cloud.hadoop.io.bigquery.output.BigQueryTableSchema)) {
return false;
}
BigQueryTableSchema another = (BigQueryTableSchema) object;
return tableSchema.equals(another.tableSchema);
}
TableSchema get() {
return tableSchema;
}
static BigQueryTableSchema wrap(TableSchema tableSchema) {
return new BigQueryTableSchema(tableSchema);
}
}