Search in sources :

Example 11 with PrimaryKeysRequest

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);
    }
}
Also used : SQLPrimaryKey(org.apache.hadoop.hive.metastore.api.SQLPrimaryKey) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) IMetaStoreClient(org.apache.hadoop.hive.metastore.IMetaStoreClient) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) PrimaryKeysRequest(org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest)

Example 12 with PrimaryKeysRequest

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());
}
Also used : SQLPrimaryKeyBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLPrimaryKeyBuilder) SQLPrimaryKey(org.apache.hadoop.hive.metastore.api.SQLPrimaryKey) Table(org.apache.hadoop.hive.metastore.api.Table) TableBuilder(org.apache.hadoop.hive.metastore.client.builder.TableBuilder) PrimaryKeysRequest(org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 13 with PrimaryKeysRequest

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"));
    }
}
Also used : SQLPrimaryKeyBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLPrimaryKeyBuilder) SQLPrimaryKey(org.apache.hadoop.hive.metastore.api.SQLPrimaryKey) Table(org.apache.hadoop.hive.metastore.api.Table) PrimaryKeysRequest(org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 14 with PrimaryKeysRequest

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());
}
Also used : SQLPrimaryKey(org.apache.hadoop.hive.metastore.api.SQLPrimaryKey) PrimaryKeysRequest(org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 15 with PrimaryKeysRequest

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());
}
Also used : SQLPrimaryKeyBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLPrimaryKeyBuilder) SQLPrimaryKey(org.apache.hadoop.hive.metastore.api.SQLPrimaryKey) PrimaryKeysRequest(org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Aggregations

PrimaryKeysRequest (org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest)16 SQLPrimaryKey (org.apache.hadoop.hive.metastore.api.SQLPrimaryKey)15 Test (org.junit.Test)13 MetastoreCheckinTest (org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)9 Table (org.apache.hadoop.hive.metastore.api.Table)8 SQLPrimaryKeyBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLPrimaryKeyBuilder)8 ForeignKeysRequest (org.apache.hadoop.hive.metastore.api.ForeignKeysRequest)3 SQLForeignKey (org.apache.hadoop.hive.metastore.api.SQLForeignKey)3 TableBuilder (org.apache.hadoop.hive.metastore.client.builder.TableBuilder)3 IMetaStoreClient (org.apache.hadoop.hive.metastore.IMetaStoreClient)2 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 SQLForeignKeyBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder)2 HiveSQLException (org.apache.hive.service.cli.HiveSQLException)2