use of org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest in project hive by apache.
the class TestPrimaryKey method createGetDrop2Column.
@Test
public void createGetDrop2Column() throws TException {
// Make sure get on a table with no key returns empty list
Table table = testTables[1];
PrimaryKeysRequest rqst = new PrimaryKeysRequest(table.getDbName(), table.getTableName());
rqst.setCatName(table.getCatName());
List<SQLPrimaryKey> fetched = client.getPrimaryKeys(rqst);
Assert.assertTrue(fetched.isEmpty());
String constraintName = "cgd2cpk";
// Multi-column. Also covers table in non-default database
List<SQLPrimaryKey> pk = new SQLPrimaryKeyBuilder().onTable(table).addColumn("col1").addColumn("col2").setEnable(false).setConstraintName(constraintName).setValidate(true).setRely(true).build(metaStore.getConf());
client.addPrimaryKey(pk);
rqst = new PrimaryKeysRequest(table.getDbName(), table.getTableName());
rqst.setCatName(table.getCatName());
fetched = client.getPrimaryKeys(rqst);
Assert.assertEquals(pk, fetched);
// Drop a named primary key
client.dropConstraint(table.getCatName(), table.getDbName(), table.getTableName(), constraintName);
rqst = new PrimaryKeysRequest(table.getDbName(), table.getTableName());
rqst.setCatName(table.getCatName());
fetched = client.getPrimaryKeys(rqst);
Assert.assertTrue(fetched.isEmpty());
// Make sure I can add it back
client.addPrimaryKey(pk);
}
use of org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest in project hive by apache.
the class TestPrimaryKey method createGetDrop.
@Test
public void createGetDrop() throws TException {
Table table = testTables[0];
// Make sure get on a table with no key returns empty list
PrimaryKeysRequest rqst = new PrimaryKeysRequest(table.getDbName(), table.getTableName());
rqst.setCatName(table.getCatName());
List<SQLPrimaryKey> fetched = client.getPrimaryKeys(rqst);
Assert.assertTrue(fetched.isEmpty());
// Single column unnamed primary key in default catalog and database
List<SQLPrimaryKey> pk = new SQLPrimaryKeyBuilder().onTable(table).addColumn("col1").build(metaStore.getConf());
client.addPrimaryKey(pk);
rqst = new PrimaryKeysRequest(table.getDbName(), table.getTableName());
rqst.setCatName(table.getCatName());
fetched = client.getPrimaryKeys(rqst);
pk.get(0).setPk_name(fetched.get(0).getPk_name());
Assert.assertEquals(pk, fetched);
// Drop a primary key
client.dropConstraint(table.getCatName(), table.getDbName(), table.getTableName(), pk.get(0).getPk_name());
rqst = new PrimaryKeysRequest(table.getDbName(), table.getTableName());
rqst.setCatName(table.getCatName());
fetched = client.getPrimaryKeys(rqst);
Assert.assertTrue(fetched.isEmpty());
// Make sure I can add it back
client.addPrimaryKey(pk);
}
use of org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest in project hive by apache.
the class TestPrimaryKey method createTableWithConstraintsPkInOtherCatalog.
@Test
public void createTableWithConstraintsPkInOtherCatalog() throws TException {
Table table = new TableBuilder().setTableName("table_in_other_catalog_with_constraints").inDb(inOtherCatalog).addCol("col1", "int").addCol("col2", "varchar(32)").build(metaStore.getConf());
List<SQLPrimaryKey> pk = new SQLPrimaryKeyBuilder().onTable(table).addColumn("col1").build(metaStore.getConf());
client.createTableWithConstraints(table, pk, null, null, null, null, null);
PrimaryKeysRequest rqst = new PrimaryKeysRequest(table.getDbName(), table.getTableName());
rqst.setCatName(table.getCatName());
List<SQLPrimaryKey> fetched = client.getPrimaryKeys(rqst);
pk.get(0).setPk_name(fetched.get(0).getPk_name());
Assert.assertEquals(pk, fetched);
client.dropConstraint(table.getCatName(), table.getDbName(), table.getTableName(), pk.get(0).getPk_name());
rqst = new PrimaryKeysRequest(table.getDbName(), table.getTableName());
rqst.setCatName(table.getCatName());
fetched = client.getPrimaryKeys(rqst);
Assert.assertTrue(fetched.isEmpty());
}
use of org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest in project hive by apache.
the class ObjectStore method getPrimaryKeys.
@Override
public List<SQLPrimaryKey> getPrimaryKeys(String catName, String db_name, String tbl_name) throws MetaException {
PrimaryKeysRequest request = new PrimaryKeysRequest(db_name, tbl_name);
request.setCatName(catName);
return getPrimaryKeys(request);
}
use of org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest 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());
}
Aggregations