gobblin.hive.HiveTable Maven / Gradle / Ivy
 The newest version!
        
        /*
 * Copyright (C) 2014-2016 LinkedIn Corp. All rights reserved.
 *
 * 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.
 */
package gobblin.hive;
import java.util.List;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.reflect.TypeToken;
import gobblin.annotation.Alpha;
import gobblin.configuration.State;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
 * A class that represents a Hive table.
 *
 * 
 *   This class is used in {@link gobblin.hive.spec.HiveSpec} instead of Hive's
 *   {@link org.apache.hadoop.hive.metastore.api.Table} class to minimize the dependency on the hive-metastore API
 *   (since it is unstable and may go through backward incompatible changes). {@link HiveTable} and Hive's
 *   {@link org.apache.hadoop.hive.metastore.api.Table} can be converted to each other using
 *   {@link gobblin.hive.metastore.HiveMetaStoreUtils}.
 * 
 *
 * @author Ziyang Liu
 */
@Getter
@Alpha
@ToString
public class HiveTable extends HiveRegistrationUnit {
  @Setter
  private List partitionKeys;
  private Optional owner;
  private Optional tableType;
  private Optional retention;
  private HiveTable(Builder builder) {
    super(builder);
    this.partitionKeys = ImmutableList. copyOf(builder.partitionKeys);
  }
  @SuppressWarnings("serial")
  @Override
  protected void populateTablePartitionFields(State state) {
    super.populateTablePartitionFields(state);
    this.owner = populateField(state, HiveConstants.OWNER, new TypeToken() {});
    this.tableType = populateField(state, HiveConstants.TABLE_TYPE, new TypeToken() {});
    this.retention = populateField(state, HiveConstants.RETENTION, new TypeToken() {});
  }
  public void setOwner(String owner) {
    this.owner = Optional.of(owner);
  }
  public void setTableType(String tableType) {
    this.tableType = Optional.of(tableType);
  }
  public void setRetention(long retention) {
    this.retention = Optional.of(retention);
  }
  @Override
  protected void updateTablePartitionFields(State state, String key, Object value) {
    super.updateTablePartitionFields(state, key, value);
    boolean isExistingField = true;
    switch (key) {
      case HiveConstants.OWNER:
        this.owner = Optional.of((String) value);
        break;
      case HiveConstants.TABLE_TYPE:
        this.tableType = Optional.of((String) value);
        break;
      case HiveConstants.RETENTION:
        this.retention = Optional.of((Long) value);
        break;
      default:
        isExistingField = false;
    }
    if (isExistingField) {
      state.removeProp(key);
    }
  }
  public static class Builder extends HiveRegistrationUnit.Builder {
    private List partitionKeys = Lists.newArrayList();
    public Builder withPartitionKeys(List partitionKeys) {
      this.partitionKeys = partitionKeys;
      return this;
    }
    @Override
    public HiveTable build() {
      return new HiveTable(this);
    }
  }
}
               © 2015 - 2025 Weber Informatics LLC | Privacy Policy