Search in sources :

Example 6 with SQLForeignKeyBuilder

use of org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder in project hive by apache.

the class TestForeignKey method addNoSuchCatalog.

@Test
public void addNoSuchCatalog() throws TException {
    Table parentTable = testTables[0];
    List<SQLPrimaryKey> pk = new SQLPrimaryKeyBuilder().onTable(parentTable).addColumn("col1").build(metaStore.getConf());
    client.addPrimaryKey(pk);
    try {
        List<SQLForeignKey> fk = new SQLForeignKeyBuilder().setTableName(testTables[0].getTableName()).setDbName(testTables[0].getDbName()).setCatName("nosuch").fromPrimaryKey(pk).addColumn("col2").build(metaStore.getConf());
        client.addForeignKey(fk);
        Assert.fail();
    } catch (InvalidObjectException | TApplicationException e) {
    // NOP
    }
}
Also used : SQLPrimaryKeyBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLPrimaryKeyBuilder) 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) InvalidObjectException(org.apache.hadoop.hive.metastore.api.InvalidObjectException) TApplicationException(org.apache.thrift.TApplicationException) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 7 with SQLForeignKeyBuilder

use of org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder in project hive by apache.

the class TestForeignKey method createTableWithConstraints.

@Test
public void createTableWithConstraints() throws TException {
    String constraintName = "ctwckk";
    Table parentTable = testTables[0];
    Table table = new TableBuilder().setTableName("table_with_constraints").setDbName(parentTable.getDbName()).addCol("col1", "int").addCol("col2", "varchar(32)").build(metaStore.getConf());
    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.createTableWithConstraints(table, null, fk, null, null, null, null);
    ForeignKeysRequest rqst = new ForeignKeysRequest(parentTable.getDbName(), parentTable.getTableName(), table.getDbName(), table.getTableName());
    rqst.setCatName(table.getCatName());
    List<SQLForeignKey> fetched = client.getForeignKeys(rqst);
    Assert.assertEquals(fk, fetched);
}
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) TableBuilder(org.apache.hadoop.hive.metastore.client.builder.TableBuilder) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 8 with SQLForeignKeyBuilder

use of org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder in project hive by apache.

the class NonCatCallsWithCatalog method primaryKeyAndForeignKey.

@Test
public void primaryKeyAndForeignKey() 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("test_col1").build(conf);
    client.addPrimaryKey(pk);
    List<SQLForeignKey> fk = new SQLForeignKeyBuilder().fromPrimaryKey(pk).onTable(table).addColumn("test_col1").setConstraintName(constraintName).build(conf);
    client.addForeignKey(fk);
    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 rqst = new ForeignKeysRequest(parentTable.getDbName(), parentTable.getTableName(), table.getDbName(), table.getTableName());
    rqst.setCatName(table.getCatName());
    List<SQLForeignKey> fetched = client.getForeignKeys(rqst);
    Assert.assertEquals(1, fetched.size());
    Assert.assertEquals(table.getDbName(), fetched.get(0).getFktable_db());
    Assert.assertEquals(table.getTableName(), fetched.get(0).getFktable_name());
    Assert.assertEquals(expectedCatalog(), fetched.get(0).getCatName());
    Assert.assertEquals("test_col1", fetched.get(0).getFkcolumn_name());
    Assert.assertEquals(parentTable.getDbName(), fetched.get(0).getPktable_db());
    Assert.assertEquals(parentTable.getTableName(), fetched.get(0).getPktable_name());
    Assert.assertEquals("test_col1", fetched.get(0).getFkcolumn_name());
    Assert.assertEquals(1, fetched.get(0).getKey_seq());
    Assert.assertEquals(parentTable.getTableName() + "_primary_key", fetched.get(0).getPk_name());
    Assert.assertEquals(constraintName, fetched.get(0).getFk_name());
    String table0FkName = fetched.get(0).getFk_name();
    Assert.assertTrue(fetched.get(0).isEnable_cstr());
    Assert.assertFalse(fetched.get(0).isValidate_cstr());
    Assert.assertFalse(fetched.get(0).isRely_cstr());
    Assert.assertEquals(table.getCatName(), fetched.get(0).getCatName());
    // Drop a foreign key
    client.dropConstraint(table.getDbName(), table.getTableName(), table0FkName);
    rqst = new ForeignKeysRequest(parentTable.getDbName(), parentTable.getTableName(), table.getDbName(), table.getTableName());
    rqst.setCatName(table.getCatName());
    fetched = client.getForeignKeys(rqst);
    Assert.assertTrue(fetched.isEmpty());
}
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) PrimaryKeysRequest(org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest) Test(org.junit.Test)

Example 9 with SQLForeignKeyBuilder

use of org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder in project hive by apache.

the class TestForeignKey method createGetDrop2Column.

@Test
public void createGetDrop2Column() throws TException {
    Table parentTable = testTables[1];
    Table table = testTables[0];
    String constraintName = "2colfk";
    // Single column unnamed primary key in default catalog and database
    List<SQLPrimaryKey> pk = new SQLPrimaryKeyBuilder().onTable(parentTable).addColumn("col1").addColumn("col2").build(metaStore.getConf());
    client.addPrimaryKey(pk);
    List<SQLForeignKey> fk = new SQLForeignKeyBuilder().fromPrimaryKey(pk).onTable(table).addColumn("col1").addColumn("col2").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);
    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 10 with SQLForeignKeyBuilder

use of org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder in project hive by apache.

the class TestForeignKey method createTableWithConstraintsInOtherCatalog.

@Test
public void createTableWithConstraintsInOtherCatalog() throws TException {
    String constraintName = "ctwcocfk";
    Table parentTable = testTables[2];
    Table table = new TableBuilder().setTableName("table_with_constraints").inDb(inOtherCatalog).addCol("col1", "int").addCol("col2", "varchar(32)").build(metaStore.getConf());
    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.createTableWithConstraints(table, null, fk, null, null, null, null);
    ForeignKeysRequest rqst = new ForeignKeysRequest(parentTable.getDbName(), parentTable.getTableName(), table.getDbName(), table.getTableName());
    rqst.setCatName(table.getCatName());
    List<SQLForeignKey> fetched = client.getForeignKeys(rqst);
    Assert.assertEquals(fk, fetched);
}
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) TableBuilder(org.apache.hadoop.hive.metastore.client.builder.TableBuilder) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Aggregations

SQLForeignKey (org.apache.hadoop.hive.metastore.api.SQLForeignKey)13 SQLPrimaryKey (org.apache.hadoop.hive.metastore.api.SQLPrimaryKey)13 SQLForeignKeyBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder)13 SQLPrimaryKeyBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLPrimaryKeyBuilder)13 Test (org.junit.Test)13 Table (org.apache.hadoop.hive.metastore.api.Table)12 MetastoreCheckinTest (org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)11 ForeignKeysRequest (org.apache.hadoop.hive.metastore.api.ForeignKeysRequest)7 InvalidObjectException (org.apache.hadoop.hive.metastore.api.InvalidObjectException)4 TApplicationException (org.apache.thrift.TApplicationException)4 TableBuilder (org.apache.hadoop.hive.metastore.client.builder.TableBuilder)3 PrimaryKeysRequest (org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest)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 SQLCheckConstraintBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLCheckConstraintBuilder)2 SQLDefaultConstraintBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLDefaultConstraintBuilder)2 SQLNotNullConstraintBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLNotNullConstraintBuilder)2 SQLUniqueConstraintBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLUniqueConstraintBuilder)2