Search in sources :

Example 1 with ConstraintsSerializer

use of org.apache.hadoop.hive.ql.parse.repl.dump.io.ConstraintsSerializer in project hive by apache.

the class ReplDumpTask method dumpConstraintMetadata.

void dumpConstraintMetadata(String dbName, String tblName, Path dbRoot, Hive hiveDb, long tableId) throws Exception {
    try {
        Path constraintsRoot = new Path(dbRoot, ReplUtils.CONSTRAINTS_ROOT_DIR_NAME);
        Path commonConstraintsFile = new Path(constraintsRoot, ConstraintFileType.COMMON.getPrefix() + tblName);
        Path fkConstraintsFile = new Path(constraintsRoot, ConstraintFileType.FOREIGNKEY.getPrefix() + tblName);
        SQLAllTableConstraints tableConstraints = hiveDb.getTableConstraints(dbName, tblName, tableId);
        if (CollectionUtils.isNotEmpty(tableConstraints.getPrimaryKeys()) || CollectionUtils.isNotEmpty(tableConstraints.getUniqueConstraints()) || CollectionUtils.isNotEmpty(tableConstraints.getNotNullConstraints()) || CollectionUtils.isNotEmpty(tableConstraints.getCheckConstraints()) || CollectionUtils.isNotEmpty(tableConstraints.getDefaultConstraints())) {
            try (JsonWriter jsonWriter = new JsonWriter(commonConstraintsFile.getFileSystem(conf), commonConstraintsFile)) {
                ConstraintsSerializer serializer = new ConstraintsSerializer(tableConstraints.getPrimaryKeys(), null, tableConstraints.getUniqueConstraints(), tableConstraints.getNotNullConstraints(), tableConstraints.getDefaultConstraints(), tableConstraints.getCheckConstraints(), conf);
                serializer.writeTo(jsonWriter, null);
            }
        }
        if (CollectionUtils.isNotEmpty(tableConstraints.getForeignKeys())) {
            try (JsonWriter jsonWriter = new JsonWriter(fkConstraintsFile.getFileSystem(conf), fkConstraintsFile)) {
                ConstraintsSerializer serializer = new ConstraintsSerializer(null, tableConstraints.getForeignKeys(), null, null, null, null, conf);
                serializer.writeTo(jsonWriter, null);
            }
        }
    } catch (NoSuchObjectException e) {
        // Bootstrap constraint dump shouldn't fail if the table is dropped/renamed while dumping it.
        // Just log a debug message and skip it.
        LOG.debug(e.getMessage());
    }
}
Also used : Path(org.apache.hadoop.fs.Path) SQLAllTableConstraints(org.apache.hadoop.hive.metastore.api.SQLAllTableConstraints) ConstraintsSerializer(org.apache.hadoop.hive.ql.parse.repl.dump.io.ConstraintsSerializer) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) JsonWriter(org.apache.hadoop.hive.ql.parse.repl.dump.io.JsonWriter)

Example 2 with ConstraintsSerializer

use of org.apache.hadoop.hive.ql.parse.repl.dump.io.ConstraintsSerializer in project hive by apache.

the class ReplDumpTask method dumpConstraintMetadata.

private void dumpConstraintMetadata(String dbName, String tblName, Path dbRoot) throws Exception {
    try {
        Path constraintsRoot = new Path(dbRoot, CONSTRAINTS_ROOT_DIR_NAME);
        Path commonConstraintsFile = new Path(constraintsRoot, ConstraintFileType.COMMON.getPrefix() + tblName);
        Path fkConstraintsFile = new Path(constraintsRoot, ConstraintFileType.FOREIGNKEY.getPrefix() + tblName);
        Hive db = getHive();
        List<SQLPrimaryKey> pks = db.getPrimaryKeyList(dbName, tblName);
        List<SQLForeignKey> fks = db.getForeignKeyList(dbName, tblName);
        List<SQLUniqueConstraint> uks = db.getUniqueConstraintList(dbName, tblName);
        List<SQLNotNullConstraint> nns = db.getNotNullConstraintList(dbName, tblName);
        if ((pks != null && !pks.isEmpty()) || (uks != null && !uks.isEmpty()) || (nns != null && !nns.isEmpty())) {
            try (JsonWriter jsonWriter = new JsonWriter(commonConstraintsFile.getFileSystem(conf), commonConstraintsFile)) {
                ConstraintsSerializer serializer = new ConstraintsSerializer(pks, null, uks, nns, conf);
                serializer.writeTo(jsonWriter, null);
            }
        }
        if (fks != null && !fks.isEmpty()) {
            try (JsonWriter jsonWriter = new JsonWriter(fkConstraintsFile.getFileSystem(conf), fkConstraintsFile)) {
                ConstraintsSerializer serializer = new ConstraintsSerializer(null, fks, null, null, conf);
                serializer.writeTo(jsonWriter, null);
            }
        }
    } catch (NoSuchObjectException e) {
        // Bootstrap constraint dump shouldn't fail if the table is dropped/renamed while dumping it.
        // Just log a debug message and skip it.
        LOG.debug(e.getMessage());
    }
}
Also used : Path(org.apache.hadoop.fs.Path) SQLUniqueConstraint(org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint) SQLPrimaryKey(org.apache.hadoop.hive.metastore.api.SQLPrimaryKey) SQLForeignKey(org.apache.hadoop.hive.metastore.api.SQLForeignKey) JsonWriter(org.apache.hadoop.hive.ql.parse.repl.dump.io.JsonWriter) SQLNotNullConstraint(org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint) Hive(org.apache.hadoop.hive.ql.metadata.Hive) ConstraintsSerializer(org.apache.hadoop.hive.ql.parse.repl.dump.io.ConstraintsSerializer) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException)

Aggregations

Path (org.apache.hadoop.fs.Path)2 NoSuchObjectException (org.apache.hadoop.hive.metastore.api.NoSuchObjectException)2 ConstraintsSerializer (org.apache.hadoop.hive.ql.parse.repl.dump.io.ConstraintsSerializer)2 JsonWriter (org.apache.hadoop.hive.ql.parse.repl.dump.io.JsonWriter)2 SQLAllTableConstraints (org.apache.hadoop.hive.metastore.api.SQLAllTableConstraints)1 SQLForeignKey (org.apache.hadoop.hive.metastore.api.SQLForeignKey)1 SQLNotNullConstraint (org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint)1 SQLPrimaryKey (org.apache.hadoop.hive.metastore.api.SQLPrimaryKey)1 SQLUniqueConstraint (org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint)1 Hive (org.apache.hadoop.hive.ql.metadata.Hive)1