Search in sources :

Example 1 with AddForeignKeyMessage

use of org.apache.hadoop.hive.metastore.messaging.AddForeignKeyMessage in project hive by apache.

the class AddForeignKeyHandler method handle.

@Override
public List<Task<? extends Serializable>> handle(Context context) throws SemanticException {
    AddForeignKeyMessage msg = deserializer.getAddForeignKeyMessage(context.dmd.getPayload());
    List<SQLForeignKey> fks = null;
    try {
        fks = msg.getForeignKeys();
    } catch (Exception e) {
        if (!(e instanceof SemanticException)) {
            throw new SemanticException("Error reading message members", e);
        } else {
            throw (SemanticException) e;
        }
    }
    List<Task<? extends Serializable>> tasks = new ArrayList<Task<? extends Serializable>>();
    if (fks.isEmpty()) {
        return tasks;
    }
    String actualDbName = context.isDbNameEmpty() ? fks.get(0).getFktable_db() : context.dbName;
    String actualTblName = context.isTableNameEmpty() ? fks.get(0).getFktable_name() : context.tableName;
    for (SQLForeignKey fk : fks) {
        // Otherwise, keep db name
        if (fk.getPktable_db().equals(fk.getFktable_db())) {
            fk.setPktable_db(actualDbName);
        }
        fk.setFktable_db(actualDbName);
        fk.setFktable_name(actualTblName);
    }
    AlterTableDesc addConstraintsDesc = new AlterTableDesc(actualDbName + "." + actualTblName, new ArrayList<SQLPrimaryKey>(), fks, new ArrayList<SQLUniqueConstraint>(), context.eventOnlyReplicationSpec());
    Task<DDLWork> addConstraintsTask = TaskFactory.get(new DDLWork(readEntitySet, writeEntitySet, addConstraintsDesc));
    tasks.add(addConstraintsTask);
    context.log.debug("Added add constrains task : {}:{}", addConstraintsTask.getId(), actualTblName);
    updatedMetadata.set(context.dmd.getEventTo().toString(), actualDbName, actualTblName, null);
    return Collections.singletonList(addConstraintsTask);
}
Also used : AddForeignKeyMessage(org.apache.hadoop.hive.metastore.messaging.AddForeignKeyMessage) SQLUniqueConstraint(org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint) AlterTableDesc(org.apache.hadoop.hive.ql.plan.AlterTableDesc) Task(org.apache.hadoop.hive.ql.exec.Task) Serializable(java.io.Serializable) SQLPrimaryKey(org.apache.hadoop.hive.metastore.api.SQLPrimaryKey) SQLForeignKey(org.apache.hadoop.hive.metastore.api.SQLForeignKey) ArrayList(java.util.ArrayList) SemanticException(org.apache.hadoop.hive.ql.parse.SemanticException) DDLWork(org.apache.hadoop.hive.ql.plan.DDLWork) SemanticException(org.apache.hadoop.hive.ql.parse.SemanticException)

Aggregations

Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 SQLForeignKey (org.apache.hadoop.hive.metastore.api.SQLForeignKey)1 SQLPrimaryKey (org.apache.hadoop.hive.metastore.api.SQLPrimaryKey)1 SQLUniqueConstraint (org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint)1 AddForeignKeyMessage (org.apache.hadoop.hive.metastore.messaging.AddForeignKeyMessage)1 Task (org.apache.hadoop.hive.ql.exec.Task)1 SemanticException (org.apache.hadoop.hive.ql.parse.SemanticException)1 AlterTableDesc (org.apache.hadoop.hive.ql.plan.AlterTableDesc)1 DDLWork (org.apache.hadoop.hive.ql.plan.DDLWork)1