All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.uber.hoodie.hive.SchemaDifference Maven / Gradle / Ivy

/*
 * Copyright (c) 2016 Uber Technologies, Inc. ([email protected])
 *
 * 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 com.uber.hoodie.hive;

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import parquet.schema.MessageType;

/**
 * Represents the schema difference between the storage schema and hive table schema
 */
public class SchemaDifference {

  private final MessageType storageSchema;
  private final Map tableSchema;
  private final List deleteColumns;
  private final Map updateColumnTypes;
  private final Map addColumnTypes;

  private SchemaDifference(MessageType storageSchema, Map tableSchema,
      List deleteColumns, Map updateColumnTypes,
      Map addColumnTypes) {
    this.storageSchema = storageSchema;
    this.tableSchema = tableSchema;
    this.deleteColumns = ImmutableList.copyOf(deleteColumns);
    this.updateColumnTypes = ImmutableMap.copyOf(updateColumnTypes);
    this.addColumnTypes = ImmutableMap.copyOf(addColumnTypes);
  }

  public List getDeleteColumns() {
    return deleteColumns;
  }

  public Map getUpdateColumnTypes() {
    return updateColumnTypes;
  }

  public Map getAddColumnTypes() {
    return addColumnTypes;
  }

  @Override
  public String toString() {
    return Objects.toStringHelper(this).add("deleteColumns", deleteColumns)
        .add("updateColumnTypes", updateColumnTypes).add("addColumnTypes", addColumnTypes)
        .toString();
  }

  public static Builder newBuilder(MessageType storageSchema, Map tableSchema) {
    return new Builder(storageSchema, tableSchema);
  }

  public boolean isEmpty() {
    return deleteColumns.isEmpty() && updateColumnTypes.isEmpty() && addColumnTypes.isEmpty();
  }

  public static class Builder {

    private final MessageType storageSchema;
    private final Map tableSchema;
    private List deleteColumns;
    private Map updateColumnTypes;
    private Map addColumnTypes;

    public Builder(MessageType storageSchema, Map tableSchema) {
      this.storageSchema = storageSchema;
      this.tableSchema = tableSchema;
      deleteColumns = Lists.newArrayList();
      updateColumnTypes = Maps.newHashMap();
      addColumnTypes = Maps.newHashMap();
    }

    public Builder deleteTableColumn(String column) {
      deleteColumns.add(column);
      return this;
    }

    public Builder updateTableColumn(String column, String storageColumnType) {
      updateColumnTypes.put(column, storageColumnType);
      return this;
    }

    public Builder addTableColumn(String name, String type) {
      addColumnTypes.put(name, type);
      return this;
    }

    public SchemaDifference build() {
      return new SchemaDifference(storageSchema, tableSchema, deleteColumns, updateColumnTypes,
          addColumnTypes);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy