use of org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest in project hive by apache.
the class GetPrimaryKeysOperation method runInternal.
@Override
public void runInternal() throws HiveSQLException {
setState(OperationState.RUNNING);
LOG.info("Fetching primary key metadata");
try {
IMetaStoreClient metastoreClient = getParentSession().getMetaStoreClient();
PrimaryKeysRequest sqlReq = new PrimaryKeysRequest(schemaName, tableName);
List<SQLPrimaryKey> pks = metastoreClient.getPrimaryKeys(sqlReq);
if (pks == null) {
return;
}
for (SQLPrimaryKey pk : pks) {
Object[] rowData = new Object[] { catalogName, pk.getTable_db(), pk.getTable_name(), pk.getColumn_name(), pk.getKey_seq(), pk.getPk_name() };
rowSet.addRow(rowData);
if (LOG.isDebugEnabled()) {
String debugMessage = getDebugMessage("primary key", RESULT_SET_SCHEMA);
LOG.debug(debugMessage, rowData);
}
}
if (LOG.isDebugEnabled() && rowSet.numRows() == 0) {
LOG.debug("No primary key metadata has been returned.");
}
setState(OperationState.FINISHED);
LOG.info("Fetching primary key metadata has been successfully finished");
} catch (Exception e) {
setState(OperationState.ERROR);
throw new HiveSQLException(e);
}
}
use of org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest in project hive by apache.
the class TestPrimaryKey method createTableWithConstraintsPk.
@Test
public void createTableWithConstraintsPk() throws TException {
String constraintName = "ctwcpk";
Table table = new TableBuilder().setTableName("table_with_constraints").addCol("col1", "int").addCol("col2", "varchar(32)").build(metaStore.getConf());
List<SQLPrimaryKey> pk = new SQLPrimaryKeyBuilder().onTable(table).addColumn("col1").setConstraintName(constraintName).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);
Assert.assertEquals(pk, fetched);
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());
}
use of org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest in project hive by apache.
the class TestPrimaryKey method doubleAddPrimaryKey.
@Test
public void doubleAddPrimaryKey() 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);
try {
pk = new SQLPrimaryKeyBuilder().onTable(table).addColumn("col2").build(metaStore.getConf());
client.addPrimaryKey(pk);
Assert.fail();
} catch (MetaException e) {
Assert.assertTrue(e.getMessage().contains("Primary key already exists for"));
}
}
use of org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest in project hive by apache.
the class TestPrimaryKey method getNoSuchCatalog.
@Test
public void getNoSuchCatalog() throws TException {
PrimaryKeysRequest rqst = new PrimaryKeysRequest(testTables[0].getTableName(), testTables[0].getTableName());
rqst.setCatName("nosuch");
List<SQLPrimaryKey> pk = client.getPrimaryKeys(rqst);
Assert.assertTrue(pk.isEmpty());
}
use of org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest in project hive by apache.
the class TestPrimaryKey method inOtherCatalog.
@Test
public void inOtherCatalog() throws TException {
PrimaryKeysRequest rqst = new PrimaryKeysRequest(testTables[2].getDbName(), testTables[2].getTableName());
rqst.setCatName(testTables[2].getCatName());
List<SQLPrimaryKey> fetched = client.getPrimaryKeys(rqst);
Assert.assertTrue(fetched.isEmpty());
String constraintName = "ocpk";
// Table in non 'hive' catalog
List<SQLPrimaryKey> pk = new SQLPrimaryKeyBuilder().onTable(testTables[2]).addColumn("col1").setConstraintName(constraintName).build(metaStore.getConf());
client.addPrimaryKey(pk);
rqst = new PrimaryKeysRequest(testTables[2].getDbName(), testTables[2].getTableName());
rqst.setCatName(testTables[2].getCatName());
fetched = client.getPrimaryKeys(rqst);
Assert.assertEquals(pk, fetched);
client.dropConstraint(testTables[2].getCatName(), testTables[2].getDbName(), testTables[2].getTableName(), constraintName);
rqst = new PrimaryKeysRequest(testTables[2].getDbName(), testTables[2].getTableName());
rqst.setCatName(testTables[2].getCatName());
fetched = client.getPrimaryKeys(rqst);
Assert.assertTrue(fetched.isEmpty());
}
Aggregations