Search in sources :

Example 1 with AlterTableRenameDesc

use of org.apache.hadoop.hive.ql.ddl.table.misc.rename.AlterTableRenameDesc in project hive by apache.

the class RenameTableHandler method handle.

@Override
public List<Task<?>> handle(Context context) throws SemanticException {
    AlterTableMessage msg = deserializer.getAlterTableMessage(context.dmd.getPayload());
    try {
        Table tableObjBefore = msg.getTableObjBefore();
        Table tableObjAfter = msg.getTableObjAfter();
        String oldDbName = tableObjBefore.getDbName();
        String newDbName = tableObjAfter.getDbName();
        if (!context.isDbNameEmpty()) {
            // newDbName must be the same
            if (!oldDbName.equalsIgnoreCase(newDbName)) {
                throw new SemanticException("Cannot replicate an event renaming a table across" + " databases into a db level load " + oldDbName + "->" + newDbName);
            } else {
                // both were the same, and can be replaced by the new db we're loading into.
                oldDbName = context.dbName;
                newDbName = context.dbName;
            }
        }
        TableName oldName = TableName.fromString(tableObjBefore.getTableName(), null, oldDbName);
        TableName newName = TableName.fromString(tableObjAfter.getTableName(), null, newDbName);
        ReplicationSpec replicationSpec = context.eventOnlyReplicationSpec();
        AlterTableRenameDesc renameTableDesc = new AlterTableRenameDesc(oldName, replicationSpec, false, newName.getNotEmptyDbTable());
        renameTableDesc.setWriteId(msg.getWriteId());
        Task<DDLWork> renameTableTask = TaskFactory.get(new DDLWork(readEntitySet, writeEntitySet, renameTableDesc, true, context.getDumpDirectory(), context.getMetricCollector()), context.hiveConf);
        context.log.debug("Added rename table task : {}:{}->{}", renameTableTask.getId(), oldName.getNotEmptyDbTable(), newName.getNotEmptyDbTable());
        // oldDbName and newDbName *will* be the same if we're here
        updatedMetadata.set(context.dmd.getEventTo().toString(), newDbName, tableObjAfter.getTableName(), null);
        // if so. If that should ever change, this will need reworking.
        return ReplUtils.addChildTask(renameTableTask);
    } catch (Exception e) {
        throw (e instanceof SemanticException) ? (SemanticException) e : new SemanticException("Error reading message members", e);
    }
}
Also used : TableName(org.apache.hadoop.hive.common.TableName) ReplicationSpec(org.apache.hadoop.hive.ql.parse.ReplicationSpec) Table(org.apache.hadoop.hive.metastore.api.Table) DDLWork(org.apache.hadoop.hive.ql.ddl.DDLWork) AlterTableMessage(org.apache.hadoop.hive.metastore.messaging.AlterTableMessage) AlterTableRenameDesc(org.apache.hadoop.hive.ql.ddl.table.misc.rename.AlterTableRenameDesc) SemanticException(org.apache.hadoop.hive.ql.parse.SemanticException) SemanticException(org.apache.hadoop.hive.ql.parse.SemanticException)

Aggregations

TableName (org.apache.hadoop.hive.common.TableName)1 Table (org.apache.hadoop.hive.metastore.api.Table)1 AlterTableMessage (org.apache.hadoop.hive.metastore.messaging.AlterTableMessage)1 DDLWork (org.apache.hadoop.hive.ql.ddl.DDLWork)1 AlterTableRenameDesc (org.apache.hadoop.hive.ql.ddl.table.misc.rename.AlterTableRenameDesc)1 ReplicationSpec (org.apache.hadoop.hive.ql.parse.ReplicationSpec)1 SemanticException (org.apache.hadoop.hive.ql.parse.SemanticException)1