Search in sources :

Example 1 with ForeignKeysRequest

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

the class TestReplicationScenarios method testConstraints.

@Test
public void testConstraints() throws IOException {
    String testName = "constraints";
    String dbName = createDB(testName, driver);
    String replDbName = dbName + "_dupe";
    run("CREATE TABLE " + dbName + ".tbl1(a string, b string, primary key (a, b) disable novalidate rely)", driver);
    run("CREATE TABLE " + dbName + ".tbl2(a string, b string, foreign key (a, b) references " + dbName + ".tbl1(a, b) disable novalidate)", driver);
    run("CREATE TABLE " + dbName + ".tbl3(a string, b string not null disable, unique (a) disable)", driver);
    run("CREATE TABLE " + dbName + ".tbl7(a string CHECK (a like 'a%'), price double CHECK (price > 0 AND price <= 1000))", driver);
    run("CREATE TABLE " + dbName + ".tbl8(a string, b int DEFAULT 0)", driver);
    Tuple bootstrapDump = bootstrapLoadAndVerify(dbName, replDbName);
    String replDumpId = bootstrapDump.lastReplId;
    try {
        List<SQLPrimaryKey> pks = metaStoreClientMirror.getPrimaryKeys(new PrimaryKeysRequest(replDbName, "tbl1"));
        assertEquals(pks.size(), 2);
        List<SQLUniqueConstraint> uks = metaStoreClientMirror.getUniqueConstraints(new UniqueConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName, "tbl3"));
        assertEquals(uks.size(), 1);
        List<SQLForeignKey> fks = metaStoreClientMirror.getForeignKeys(new ForeignKeysRequest(null, null, replDbName, "tbl2"));
        assertEquals(fks.size(), 2);
        List<SQLNotNullConstraint> nns = metaStoreClientMirror.getNotNullConstraints(new NotNullConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName, "tbl3"));
        assertEquals(nns.size(), 1);
        List<SQLCheckConstraint> cks = metaStoreClientMirror.getCheckConstraints(new CheckConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName, "tbl7"));
        assertEquals(cks.size(), 2);
        List<SQLDefaultConstraint> dks = metaStoreClientMirror.getDefaultConstraints(new DefaultConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName, "tbl8"));
        assertEquals(dks.size(), 1);
    } catch (TException te) {
        assertNull(te);
    }
    run("CREATE TABLE " + dbName + ".tbl4(a string, b string, primary key (a, b) disable novalidate rely)", driver);
    run("CREATE TABLE " + dbName + ".tbl5(a string, b string, foreign key (a, b) references " + dbName + ".tbl4(a, b) disable novalidate)", driver);
    run("CREATE TABLE " + dbName + ".tbl6(a string, b string not null disable, unique (a) disable)", driver);
    run("CREATE TABLE " + dbName + ".tbl9(a string CHECK (a like 'a%'), price double CHECK (price > 0 AND price <= 1000))", driver);
    run("CREATE TABLE " + dbName + ".tbl10(a string, b int DEFAULT 0)", driver);
    Tuple incrementalDump = incrementalLoadAndVerify(dbName, replDbName);
    replDumpId = incrementalDump.lastReplId;
    String pkName = null;
    String ukName = null;
    String fkName = null;
    String nnName = null;
    String dkName1 = null;
    String ckName1 = null;
    String ckName2 = null;
    try {
        List<SQLPrimaryKey> pks = metaStoreClientMirror.getPrimaryKeys(new PrimaryKeysRequest(replDbName, "tbl4"));
        assertEquals(pks.size(), 2);
        pkName = pks.get(0).getPk_name();
        List<SQLUniqueConstraint> uks = metaStoreClientMirror.getUniqueConstraints(new UniqueConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName, "tbl6"));
        assertEquals(uks.size(), 1);
        ukName = uks.get(0).getUk_name();
        List<SQLForeignKey> fks = metaStoreClientMirror.getForeignKeys(new ForeignKeysRequest(null, null, replDbName, "tbl5"));
        assertEquals(fks.size(), 2);
        fkName = fks.get(0).getFk_name();
        List<SQLNotNullConstraint> nns = metaStoreClientMirror.getNotNullConstraints(new NotNullConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName, "tbl6"));
        assertEquals(nns.size(), 1);
        nnName = nns.get(0).getNn_name();
        List<SQLCheckConstraint> cks = metaStoreClientMirror.getCheckConstraints(new CheckConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName, "tbl9"));
        assertEquals(cks.size(), 2);
        ckName1 = cks.get(0).getDc_name();
        ckName2 = cks.get(1).getDc_name();
        List<SQLDefaultConstraint> dks = metaStoreClientMirror.getDefaultConstraints(new DefaultConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName, "tbl10"));
        assertEquals(dks.size(), 1);
        dkName1 = dks.get(0).getDc_name();
    } catch (TException te) {
        assertNull(te);
    }
    String dkName2 = "custom_dk_name";
    String ckName3 = "customer_ck_name";
    run("ALTER TABLE " + dbName + ".tbl10 CHANGE COLUMN a a string CONSTRAINT " + ckName3 + " CHECK (a like 'a%')", driver);
    run("ALTER TABLE " + dbName + ".tbl10 CHANGE COLUMN b b int CONSTRAINT " + dkName2 + " DEFAULT 1 ENABLE", driver);
    incrementalLoadAndVerify(dbName, replDbName);
    try {
        List<SQLDefaultConstraint> dks = metaStoreClientMirror.getDefaultConstraints(new DefaultConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName, "tbl10"));
        assertEquals(dks.size(), 2);
        assertEquals(dks.get(1).getDefault_value(), "1");
        List<SQLCheckConstraint> cks = metaStoreClientMirror.getCheckConstraints(new CheckConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName, "tbl10"));
        assertEquals(cks.size(), 1);
        assertEquals(cks.get(0).getDc_name(), ckName3);
    } catch (TException te) {
        assertNull(te);
    }
    run("ALTER TABLE " + dbName + ".tbl4 DROP CONSTRAINT `" + pkName + "`", driver);
    run("ALTER TABLE " + dbName + ".tbl4 DROP CONSTRAINT `" + ukName + "`", driver);
    run("ALTER TABLE " + dbName + ".tbl5 DROP CONSTRAINT `" + fkName + "`", driver);
    run("ALTER TABLE " + dbName + ".tbl6 DROP CONSTRAINT `" + nnName + "`", driver);
    run("ALTER TABLE " + dbName + ".tbl9 DROP CONSTRAINT `" + ckName1 + "`", driver);
    run("ALTER TABLE " + dbName + ".tbl9 DROP CONSTRAINT `" + ckName2 + "`", driver);
    run("ALTER TABLE " + dbName + ".tbl10 DROP CONSTRAINT `" + ckName3 + "`", driver);
    run("ALTER TABLE " + dbName + ".tbl10 DROP CONSTRAINT `" + dkName1 + "`", driver);
    run("ALTER TABLE " + dbName + ".tbl10 DROP CONSTRAINT `" + dkName2 + "`", driver);
    incrementalLoadAndVerify(dbName, replDbName);
    try {
        List<SQLPrimaryKey> pks = metaStoreClientMirror.getPrimaryKeys(new PrimaryKeysRequest(replDbName, "tbl4"));
        assertTrue(pks.isEmpty());
        List<SQLUniqueConstraint> uks = metaStoreClientMirror.getUniqueConstraints(new UniqueConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName, "tbl4"));
        assertTrue(uks.isEmpty());
        List<SQLForeignKey> fks = metaStoreClientMirror.getForeignKeys(new ForeignKeysRequest(null, null, replDbName, "tbl5"));
        assertTrue(fks.isEmpty());
        List<SQLNotNullConstraint> nns = metaStoreClientMirror.getNotNullConstraints(new NotNullConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName, "tbl6"));
        assertTrue(nns.isEmpty());
        List<SQLDefaultConstraint> dks = metaStoreClientMirror.getDefaultConstraints(new DefaultConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName, "tbl10"));
        assertTrue(dks.isEmpty());
        List<SQLCheckConstraint> cks = metaStoreClientMirror.getCheckConstraints(new CheckConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName, "tbl9"));
        assertTrue(cks.isEmpty());
        cks = metaStoreClientMirror.getCheckConstraints(new CheckConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName, "tbl10"));
        assertTrue(cks.isEmpty());
        dks = metaStoreClientMirror.getDefaultConstraints(new DefaultConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName, "tbl12"));
        assertTrue(dks.isEmpty());
        cks = metaStoreClientMirror.getCheckConstraints(new CheckConstraintsRequest(DEFAULT_CATALOG_NAME, replDbName, "tbl12"));
        assertTrue(cks.isEmpty());
    } catch (TException te) {
        assertNull(te);
    }
}
Also used : SQLUniqueConstraint(org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint) UniqueConstraintsRequest(org.apache.hadoop.hive.metastore.api.UniqueConstraintsRequest) TException(org.apache.thrift.TException) SQLPrimaryKey(org.apache.hadoop.hive.metastore.api.SQLPrimaryKey) SQLCheckConstraint(org.apache.hadoop.hive.metastore.api.SQLCheckConstraint) SQLForeignKey(org.apache.hadoop.hive.metastore.api.SQLForeignKey) NotNullConstraintsRequest(org.apache.hadoop.hive.metastore.api.NotNullConstraintsRequest) PrimaryKeysRequest(org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest) SQLDefaultConstraint(org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint) SQLNotNullConstraint(org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint) ForeignKeysRequest(org.apache.hadoop.hive.metastore.api.ForeignKeysRequest) DefaultConstraintsRequest(org.apache.hadoop.hive.metastore.api.DefaultConstraintsRequest) CheckConstraintsRequest(org.apache.hadoop.hive.metastore.api.CheckConstraintsRequest) Test(org.junit.Test)

Example 2 with ForeignKeysRequest

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

the class GetCrossReferenceOperation method runInternal.

@Override
public void runInternal() throws HiveSQLException {
    setState(OperationState.RUNNING);
    log.info("Fetching cross reference metadata");
    try {
        IMetaStoreClient metastoreClient = getParentSession().getMetaStoreClient();
        ForeignKeysRequest fkReq = new ForeignKeysRequest(parentSchemaName, parentTableName, foreignSchemaName, foreignTableName);
        List<SQLForeignKey> fks = metastoreClient.getForeignKeys(fkReq);
        if (fks == null) {
            return;
        }
        for (SQLForeignKey fk : fks) {
            Object[] rowData = new Object[] { parentCatalogName, fk.getPktable_db(), fk.getPktable_name(), fk.getPkcolumn_name(), foreignCatalogName, fk.getFktable_db(), fk.getFktable_name(), fk.getFkcolumn_name(), fk.getKey_seq(), fk.getUpdate_rule(), fk.getDelete_rule(), fk.getFk_name(), fk.getPk_name(), 0 };
            rowSet.addRow(rowData);
            if (log.isDebugEnabled()) {
                String debugMessage = getDebugMessage("cross reference", RESULT_SET_SCHEMA);
                log.debug(debugMessage, rowData);
            }
        }
        if (log.isDebugEnabled() && rowSet.numRows() == 0) {
            log.debug("No cross reference metadata has been returned.");
        }
        setState(OperationState.FINISHED);
        log.info("Fetching cross reference metadata has been successfully finished");
    } catch (Exception e) {
        setState(OperationState.ERROR);
        throw new HiveSQLException(e);
    }
}
Also used : ForeignKeysRequest(org.apache.hadoop.hive.metastore.api.ForeignKeysRequest) SQLForeignKey(org.apache.hadoop.hive.metastore.api.SQLForeignKey) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) IMetaStoreClient(org.apache.hadoop.hive.metastore.IMetaStoreClient) HiveSQLException(org.apache.hive.service.cli.HiveSQLException)

Example 3 with ForeignKeysRequest

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

the class NonCatCallsWithCatalog method createTableWithConstraints.

@Test
public void createTableWithConstraints() throws TException {
    Table parentTable = testTables[2];
    Table table = new TableBuilder().setTableName("table_in_other_catalog_with_constraints").addCol("col1", "int").addCol("col2", "varchar(32)").addCol("col3", "int").addCol("col4", "int").addCol("col5", "int").addCol("col6", "int").build(conf);
    table.unsetCatName();
    List<SQLPrimaryKey> parentPk = new SQLPrimaryKeyBuilder().onTable(parentTable).addColumn("test_col1").build(conf);
    for (SQLPrimaryKey pkcol : parentPk) {
        pkcol.unsetCatName();
    }
    client.addPrimaryKey(parentPk);
    List<SQLPrimaryKey> pk = new SQLPrimaryKeyBuilder().onTable(table).addColumn("col2").build(conf);
    for (SQLPrimaryKey pkcol : pk) {
        pkcol.unsetCatName();
    }
    List<SQLForeignKey> fk = new SQLForeignKeyBuilder().fromPrimaryKey(parentPk).onTable(table).addColumn("col1").build(conf);
    for (SQLForeignKey fkcol : fk) {
        fkcol.unsetCatName();
    }
    List<SQLDefaultConstraint> dv = new SQLDefaultConstraintBuilder().onTable(table).addColumn("col3").setDefaultVal(0).build(conf);
    for (SQLDefaultConstraint dccol : dv) {
        dccol.unsetCatName();
    }
    List<SQLNotNullConstraint> nn = new SQLNotNullConstraintBuilder().onTable(table).addColumn("col4").build(conf);
    for (SQLNotNullConstraint nncol : nn) {
        nncol.unsetCatName();
    }
    List<SQLUniqueConstraint> uc = new SQLUniqueConstraintBuilder().onTable(table).addColumn("col5").build(conf);
    for (SQLUniqueConstraint uccol : uc) {
        uccol.unsetCatName();
    }
    List<SQLCheckConstraint> cc = new SQLCheckConstraintBuilder().onTable(table).addColumn("col6").setCheckExpression("> 0").build(conf);
    for (SQLCheckConstraint cccol : cc) {
        cccol.unsetCatName();
    }
    client.createTableWithConstraints(table, pk, fk, uc, nn, dv, cc);
    PrimaryKeysRequest pkRqst = new PrimaryKeysRequest(parentTable.getDbName(), parentTable.getTableName());
    pkRqst.setCatName(parentTable.getCatName());
    List<SQLPrimaryKey> pkFetched = client.getPrimaryKeys(pkRqst);
    Assert.assertEquals(1, pkFetched.size());
    Assert.assertEquals(expectedCatalog(), pkFetched.get(0).getCatName());
    Assert.assertEquals(parentTable.getDbName(), pkFetched.get(0).getTable_db());
    Assert.assertEquals(parentTable.getTableName(), pkFetched.get(0).getTable_name());
    Assert.assertEquals("test_col1", pkFetched.get(0).getColumn_name());
    Assert.assertEquals(1, pkFetched.get(0).getKey_seq());
    Assert.assertTrue(pkFetched.get(0).isEnable_cstr());
    Assert.assertFalse(pkFetched.get(0).isValidate_cstr());
    Assert.assertFalse(pkFetched.get(0).isRely_cstr());
    Assert.assertEquals(parentTable.getCatName(), pkFetched.get(0).getCatName());
    ForeignKeysRequest fkRqst = new ForeignKeysRequest(parentTable.getDbName(), parentTable.getTableName(), table.getDbName(), table.getTableName());
    fkRqst.setCatName(table.getCatName());
    List<SQLForeignKey> fkFetched = client.getForeignKeys(fkRqst);
    Assert.assertEquals(1, fkFetched.size());
    Assert.assertEquals(expectedCatalog(), fkFetched.get(0).getCatName());
    Assert.assertEquals(table.getDbName(), fkFetched.get(0).getFktable_db());
    Assert.assertEquals(table.getTableName(), fkFetched.get(0).getFktable_name());
    Assert.assertEquals("col1", fkFetched.get(0).getFkcolumn_name());
    Assert.assertEquals(parentTable.getDbName(), fkFetched.get(0).getPktable_db());
    Assert.assertEquals(parentTable.getTableName(), fkFetched.get(0).getPktable_name());
    Assert.assertEquals(1, fkFetched.get(0).getKey_seq());
    Assert.assertEquals(parentTable.getTableName() + "_primary_key", fkFetched.get(0).getPk_name());
    Assert.assertTrue(fkFetched.get(0).isEnable_cstr());
    Assert.assertFalse(fkFetched.get(0).isValidate_cstr());
    Assert.assertFalse(fkFetched.get(0).isRely_cstr());
    Assert.assertEquals(table.getCatName(), fkFetched.get(0).getCatName());
    NotNullConstraintsRequest nnRqst = new NotNullConstraintsRequest(table.getCatName(), table.getDbName(), table.getTableName());
    List<SQLNotNullConstraint> nnFetched = client.getNotNullConstraints(nnRqst);
    Assert.assertEquals(1, nnFetched.size());
    Assert.assertEquals(table.getDbName(), nnFetched.get(0).getTable_db());
    Assert.assertEquals(table.getTableName(), nnFetched.get(0).getTable_name());
    Assert.assertEquals("col4", nnFetched.get(0).getColumn_name());
    Assert.assertEquals(table.getTableName() + "_not_null_constraint", nnFetched.get(0).getNn_name());
    Assert.assertTrue(nnFetched.get(0).isEnable_cstr());
    Assert.assertFalse(nnFetched.get(0).isValidate_cstr());
    Assert.assertFalse(nnFetched.get(0).isRely_cstr());
    Assert.assertEquals(table.getCatName(), nnFetched.get(0).getCatName());
    UniqueConstraintsRequest ucRqst = new UniqueConstraintsRequest(table.getCatName(), table.getDbName(), table.getTableName());
    List<SQLUniqueConstraint> ucFetched = client.getUniqueConstraints(ucRqst);
    Assert.assertEquals(1, ucFetched.size());
    Assert.assertEquals(table.getDbName(), ucFetched.get(0).getTable_db());
    Assert.assertEquals(table.getTableName(), ucFetched.get(0).getTable_name());
    Assert.assertEquals("col5", ucFetched.get(0).getColumn_name());
    Assert.assertEquals(1, ucFetched.get(0).getKey_seq());
    Assert.assertEquals(table.getTableName() + "_unique_constraint", ucFetched.get(0).getUk_name());
    Assert.assertTrue(ucFetched.get(0).isEnable_cstr());
    Assert.assertFalse(ucFetched.get(0).isValidate_cstr());
    Assert.assertFalse(ucFetched.get(0).isRely_cstr());
    Assert.assertEquals(table.getCatName(), ucFetched.get(0).getCatName());
    DefaultConstraintsRequest dcRqst = new DefaultConstraintsRequest(table.getCatName(), table.getDbName(), table.getTableName());
    List<SQLDefaultConstraint> dcFetched = client.getDefaultConstraints(dcRqst);
    Assert.assertEquals(1, dcFetched.size());
    Assert.assertEquals(expectedCatalog(), dcFetched.get(0).getCatName());
    Assert.assertEquals(table.getDbName(), dcFetched.get(0).getTable_db());
    Assert.assertEquals(table.getTableName(), dcFetched.get(0).getTable_name());
    Assert.assertEquals("col3", dcFetched.get(0).getColumn_name());
    Assert.assertEquals("0", dcFetched.get(0).getDefault_value());
    Assert.assertEquals(table.getTableName() + "_default_value", dcFetched.get(0).getDc_name());
    Assert.assertTrue(dcFetched.get(0).isEnable_cstr());
    Assert.assertFalse(dcFetched.get(0).isValidate_cstr());
    Assert.assertFalse(dcFetched.get(0).isRely_cstr());
    Assert.assertEquals(table.getCatName(), dcFetched.get(0).getCatName());
    CheckConstraintsRequest ccRqst = new CheckConstraintsRequest(table.getCatName(), table.getDbName(), table.getTableName());
    List<SQLCheckConstraint> ccFetched = client.getCheckConstraints(ccRqst);
    Assert.assertEquals(1, ccFetched.size());
    Assert.assertEquals(expectedCatalog(), ccFetched.get(0).getCatName());
    Assert.assertEquals(table.getDbName(), ccFetched.get(0).getTable_db());
    Assert.assertEquals(table.getTableName(), ccFetched.get(0).getTable_name());
    Assert.assertEquals("col6", ccFetched.get(0).getColumn_name());
    Assert.assertEquals("> 0", ccFetched.get(0).getCheck_expression());
    Assert.assertEquals(table.getTableName() + "_check_constraint", ccFetched.get(0).getDc_name());
    Assert.assertTrue(ccFetched.get(0).isEnable_cstr());
    Assert.assertFalse(ccFetched.get(0).isValidate_cstr());
    Assert.assertFalse(ccFetched.get(0).isRely_cstr());
    Assert.assertEquals(table.getCatName(), ccFetched.get(0).getCatName());
}
Also used : SQLForeignKey(org.apache.hadoop.hive.metastore.api.SQLForeignKey) NotNullConstraintsRequest(org.apache.hadoop.hive.metastore.api.NotNullConstraintsRequest) TableBuilder(org.apache.hadoop.hive.metastore.client.builder.TableBuilder) SQLDefaultConstraint(org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint) SQLNotNullConstraint(org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint) SQLDefaultConstraintBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLDefaultConstraintBuilder) SQLUniqueConstraintBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLUniqueConstraintBuilder) SQLPrimaryKeyBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLPrimaryKeyBuilder) SQLUniqueConstraint(org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint) UniqueConstraintsRequest(org.apache.hadoop.hive.metastore.api.UniqueConstraintsRequest) SQLForeignKeyBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder) SQLPrimaryKey(org.apache.hadoop.hive.metastore.api.SQLPrimaryKey) SQLCheckConstraintBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLCheckConstraintBuilder) Table(org.apache.hadoop.hive.metastore.api.Table) SQLCheckConstraint(org.apache.hadoop.hive.metastore.api.SQLCheckConstraint) PrimaryKeysRequest(org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest) ForeignKeysRequest(org.apache.hadoop.hive.metastore.api.ForeignKeysRequest) DefaultConstraintsRequest(org.apache.hadoop.hive.metastore.api.DefaultConstraintsRequest) SQLNotNullConstraintBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLNotNullConstraintBuilder) CheckConstraintsRequest(org.apache.hadoop.hive.metastore.api.CheckConstraintsRequest) Test(org.junit.Test)

Example 4 with ForeignKeysRequest

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

the class TestForeignKey method inOtherCatalog.

@Test
public void inOtherCatalog() throws TException {
    Table parentTable = testTables[2];
    Table table = testTables[3];
    String constraintName = "othercatfk";
    // Single column unnamed primary key in default catalog and database
    List<SQLPrimaryKey> pk = new SQLPrimaryKeyBuilder().onTable(parentTable).addColumn("col1").build(metaStore.getConf());
    client.addPrimaryKey(pk);
    List<SQLForeignKey> fk = new SQLForeignKeyBuilder().fromPrimaryKey(pk).onTable(table).addColumn("col1").setConstraintName(constraintName).build(metaStore.getConf());
    client.addForeignKey(fk);
    ForeignKeysRequest rqst = new ForeignKeysRequest(parentTable.getDbName(), parentTable.getTableName(), table.getDbName(), table.getTableName());
    rqst.setCatName(table.getCatName());
    List<SQLForeignKey> fetched = client.getForeignKeys(rqst);
    fk.get(0).setFk_name(fetched.get(0).getFk_name());
    Assert.assertEquals(fk, fetched);
    // Drop a foreign key
    client.dropConstraint(table.getCatName(), table.getDbName(), table.getTableName(), constraintName);
    rqst = new ForeignKeysRequest(parentTable.getDbName(), parentTable.getTableName(), table.getDbName(), table.getTableName());
    rqst.setCatName(table.getCatName());
    fetched = client.getForeignKeys(rqst);
    Assert.assertTrue(fetched.isEmpty());
    // Make sure I can add it back
    client.addForeignKey(fk);
}
Also used : SQLPrimaryKeyBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLPrimaryKeyBuilder) SQLForeignKeyBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder) ForeignKeysRequest(org.apache.hadoop.hive.metastore.api.ForeignKeysRequest) SQLPrimaryKey(org.apache.hadoop.hive.metastore.api.SQLPrimaryKey) Table(org.apache.hadoop.hive.metastore.api.Table) SQLForeignKey(org.apache.hadoop.hive.metastore.api.SQLForeignKey) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 5 with ForeignKeysRequest

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

the class TestForeignKey method createGetDrop.

@Test
public void createGetDrop() throws TException {
    Table parentTable = testTables[1];
    Table table = testTables[0];
    // Make sure get on a table with no key returns empty list
    ForeignKeysRequest rqst = new ForeignKeysRequest(parentTable.getDbName(), parentTable.getTableName(), table.getDbName(), table.getTableName());
    rqst.setCatName(table.getCatName());
    List<SQLForeignKey> fetched = client.getForeignKeys(rqst);
    Assert.assertTrue(fetched.isEmpty());
    // Single column unnamed primary key in default catalog and database
    List<SQLPrimaryKey> pk = new SQLPrimaryKeyBuilder().onTable(parentTable).addColumn("col1").build(metaStore.getConf());
    client.addPrimaryKey(pk);
    List<SQLForeignKey> fk = new SQLForeignKeyBuilder().fromPrimaryKey(pk).onTable(table).addColumn("col1").build(metaStore.getConf());
    client.addForeignKey(fk);
    rqst = new ForeignKeysRequest(parentTable.getDbName(), parentTable.getTableName(), table.getDbName(), table.getTableName());
    rqst.setCatName(table.getCatName());
    fetched = client.getForeignKeys(rqst);
    fk.get(0).setFk_name(fetched.get(0).getFk_name());
    Assert.assertEquals(fk, fetched);
    // Drop a foreign key
    client.dropConstraint(table.getCatName(), table.getDbName(), table.getTableName(), fk.get(0).getFk_name());
    rqst = new ForeignKeysRequest(parentTable.getDbName(), parentTable.getTableName(), table.getDbName(), table.getTableName());
    rqst.setCatName(table.getCatName());
    fetched = client.getForeignKeys(rqst);
    Assert.assertTrue(fetched.isEmpty());
    // Make sure I can add it back
    client.addForeignKey(fk);
}
Also used : SQLPrimaryKeyBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLPrimaryKeyBuilder) ForeignKeysRequest(org.apache.hadoop.hive.metastore.api.ForeignKeysRequest) SQLForeignKeyBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder) SQLPrimaryKey(org.apache.hadoop.hive.metastore.api.SQLPrimaryKey) Table(org.apache.hadoop.hive.metastore.api.Table) SQLForeignKey(org.apache.hadoop.hive.metastore.api.SQLForeignKey) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Aggregations

ForeignKeysRequest (org.apache.hadoop.hive.metastore.api.ForeignKeysRequest)10 SQLForeignKey (org.apache.hadoop.hive.metastore.api.SQLForeignKey)9 SQLPrimaryKey (org.apache.hadoop.hive.metastore.api.SQLPrimaryKey)8 Test (org.junit.Test)8 Table (org.apache.hadoop.hive.metastore.api.Table)7 SQLForeignKeyBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder)7 SQLPrimaryKeyBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLPrimaryKeyBuilder)7 MetastoreCheckinTest (org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)5 PrimaryKeysRequest (org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest)3 TableBuilder (org.apache.hadoop.hive.metastore.client.builder.TableBuilder)3 CheckConstraintsRequest (org.apache.hadoop.hive.metastore.api.CheckConstraintsRequest)2 DefaultConstraintsRequest (org.apache.hadoop.hive.metastore.api.DefaultConstraintsRequest)2 NotNullConstraintsRequest (org.apache.hadoop.hive.metastore.api.NotNullConstraintsRequest)2 SQLCheckConstraint (org.apache.hadoop.hive.metastore.api.SQLCheckConstraint)2 SQLDefaultConstraint (org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint)2 SQLNotNullConstraint (org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint)2 SQLUniqueConstraint (org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint)2 UniqueConstraintsRequest (org.apache.hadoop.hive.metastore.api.UniqueConstraintsRequest)2 IMetaStoreClient (org.apache.hadoop.hive.metastore.IMetaStoreClient)1 SQLCheckConstraintBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLCheckConstraintBuilder)1