Search in sources :

Example 16 with SQLUniqueConstraint

use of org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint in project hive by apache.

the class AddPrimaryKeyHandler method handle.

@Override
public List<Task<? extends Serializable>> handle(Context context) throws SemanticException {
    AddPrimaryKeyMessage msg = deserializer.getAddPrimaryKeyMessage(context.dmd.getPayload());
    List<SQLPrimaryKey> pks = null;
    try {
        pks = msg.getPrimaryKeys();
    } 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 (pks.isEmpty()) {
        return tasks;
    }
    String actualDbName = context.isDbNameEmpty() ? pks.get(0).getTable_db() : context.dbName;
    String actualTblName = context.isTableNameEmpty() ? pks.get(0).getTable_name() : context.tableName;
    for (SQLPrimaryKey pk : pks) {
        pk.setTable_db(actualDbName);
        pk.setTable_name(actualTblName);
    }
    AlterTableDesc addConstraintsDesc = new AlterTableDesc(actualDbName + "." + actualTblName, pks, new ArrayList<SQLForeignKey>(), 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 : SQLUniqueConstraint(org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint) AddPrimaryKeyMessage(org.apache.hadoop.hive.metastore.messaging.AddPrimaryKeyMessage) AlterTableDesc(org.apache.hadoop.hive.ql.plan.AlterTableDesc) SQLPrimaryKey(org.apache.hadoop.hive.metastore.api.SQLPrimaryKey) Task(org.apache.hadoop.hive.ql.exec.Task) Serializable(java.io.Serializable) 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)

Example 17 with SQLUniqueConstraint

use of org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint in project hive by apache.

the class AddUniqueConstraintHandler method handle.

@Override
public List<Task<? extends Serializable>> handle(Context context) throws SemanticException {
    AddUniqueConstraintMessage msg = deserializer.getAddUniqueConstraintMessage(context.dmd.getPayload());
    List<SQLUniqueConstraint> uks = null;
    try {
        uks = msg.getUniqueConstraints();
    } 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 (uks.isEmpty()) {
        return tasks;
    }
    String actualDbName = context.isDbNameEmpty() ? uks.get(0).getTable_db() : context.dbName;
    String actualTblName = context.isTableNameEmpty() ? uks.get(0).getTable_name() : context.tableName;
    for (SQLUniqueConstraint uk : uks) {
        uk.setTable_db(actualDbName);
        uk.setTable_name(actualTblName);
    }
    AlterTableDesc addConstraintsDesc = new AlterTableDesc(actualDbName + "." + actualTblName, new ArrayList<SQLPrimaryKey>(), new ArrayList<SQLForeignKey>(), uks, 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 : 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) AddUniqueConstraintMessage(org.apache.hadoop.hive.metastore.messaging.AddUniqueConstraintMessage) SemanticException(org.apache.hadoop.hive.ql.parse.SemanticException)

Aggregations

SQLUniqueConstraint (org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint)17 SQLNotNullConstraint (org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint)12 SQLPrimaryKey (org.apache.hadoop.hive.metastore.api.SQLPrimaryKey)12 SQLForeignKey (org.apache.hadoop.hive.metastore.api.SQLForeignKey)11 ArrayList (java.util.ArrayList)10 SQLCheckConstraint (org.apache.hadoop.hive.metastore.api.SQLCheckConstraint)10 SQLDefaultConstraint (org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint)10 DDLWork (org.apache.hadoop.hive.ql.plan.DDLWork)7 AlterTableDesc (org.apache.hadoop.hive.ql.plan.AlterTableDesc)6 MConstraint (org.apache.hadoop.hive.metastore.model.MConstraint)5 Serializable (java.io.Serializable)4 Task (org.apache.hadoop.hive.ql.exec.Task)4 Table (org.apache.hadoop.hive.ql.metadata.Table)3 SemanticException (org.apache.hadoop.hive.ql.parse.SemanticException)3 Query (javax.jdo.Query)2 Path (org.apache.hadoop.fs.Path)2 FieldSchema (org.apache.hadoop.hive.metastore.api.FieldSchema)2 InvalidObjectException (org.apache.hadoop.hive.metastore.api.InvalidObjectException)2 MetaException (org.apache.hadoop.hive.metastore.api.MetaException)2 MColumnDescriptor (org.apache.hadoop.hive.metastore.model.MColumnDescriptor)2