Search in sources :

Example 1 with DropDatabaseEvent

use of org.apache.hadoop.hive.metastore.events.DropDatabaseEvent in project hive by apache.

the class TestHiveMetaStoreWithEnvironmentContext method testEnvironmentContext.

@Test
public void testEnvironmentContext() throws Exception {
    int listSize = 0;
    List<ListenerEvent> notifyList = DummyListener.notifyList;
    assertEquals(notifyList.size(), listSize);
    msc.createDatabase(db);
    listSize++;
    assertEquals(listSize, notifyList.size());
    CreateDatabaseEvent dbEvent = (CreateDatabaseEvent) (notifyList.get(listSize - 1));
    assert dbEvent.getStatus();
    msc.createTable(table, envContext);
    listSize++;
    assertEquals(notifyList.size(), listSize);
    CreateTableEvent tblEvent = (CreateTableEvent) (notifyList.get(listSize - 1));
    assert tblEvent.getStatus();
    assertEquals(envContext, tblEvent.getEnvironmentContext());
    table = msc.getTable(dbName, tblName);
    partition.getSd().setLocation(table.getSd().getLocation() + "/part1");
    msc.add_partition(partition, envContext);
    listSize++;
    assertEquals(notifyList.size(), listSize);
    AddPartitionEvent partEvent = (AddPartitionEvent) (notifyList.get(listSize - 1));
    assert partEvent.getStatus();
    assertEquals(envContext, partEvent.getEnvironmentContext());
    List<String> partVals = new ArrayList<>();
    partVals.add("2012");
    msc.appendPartition(dbName, tblName, partVals, envContext);
    listSize++;
    assertEquals(notifyList.size(), listSize);
    AddPartitionEvent appendPartEvent = (AddPartitionEvent) (notifyList.get(listSize - 1));
    assert appendPartEvent.getStatus();
    assertEquals(envContext, appendPartEvent.getEnvironmentContext());
    table.setTableName(renamed);
    msc.alter_table_with_environmentContext(dbName, tblName, table, envContext);
    listSize++;
    assertEquals(notifyList.size(), listSize);
    AlterTableEvent alterTableEvent = (AlterTableEvent) notifyList.get(listSize - 1);
    assert alterTableEvent.getStatus();
    assertEquals(envContext, alterTableEvent.getEnvironmentContext());
    table.setTableName(tblName);
    msc.alter_table_with_environmentContext(dbName, renamed, table, envContext);
    listSize++;
    assertEquals(notifyList.size(), listSize);
    List<String> dropPartVals = new ArrayList<>();
    dropPartVals.add("2011");
    msc.dropPartition(dbName, tblName, dropPartVals, envContext);
    listSize++;
    assertEquals(notifyList.size(), listSize);
    DropPartitionEvent dropPartEvent = (DropPartitionEvent) notifyList.get(listSize - 1);
    assert dropPartEvent.getStatus();
    assertEquals(envContext, dropPartEvent.getEnvironmentContext());
    msc.dropPartition(dbName, tblName, "b=2012", true, envContext);
    listSize++;
    assertEquals(notifyList.size(), listSize);
    DropPartitionEvent dropPartByNameEvent = (DropPartitionEvent) notifyList.get(listSize - 1);
    assert dropPartByNameEvent.getStatus();
    assertEquals(envContext, dropPartByNameEvent.getEnvironmentContext());
    msc.dropTable(dbName, tblName, true, false, envContext);
    listSize++;
    assertEquals(notifyList.size(), listSize);
    DropTableEvent dropTblEvent = (DropTableEvent) notifyList.get(listSize - 1);
    assert dropTblEvent.getStatus();
    assertEquals(envContext, dropTblEvent.getEnvironmentContext());
    msc.dropDatabase(dbName);
    listSize++;
    assertEquals(notifyList.size(), listSize);
    DropDatabaseEvent dropDB = (DropDatabaseEvent) notifyList.get(listSize - 1);
    assert dropDB.getStatus();
}
Also used : DropPartitionEvent(org.apache.hadoop.hive.metastore.events.DropPartitionEvent) CreateDatabaseEvent(org.apache.hadoop.hive.metastore.events.CreateDatabaseEvent) ArrayList(java.util.ArrayList) ListenerEvent(org.apache.hadoop.hive.metastore.events.ListenerEvent) AlterTableEvent(org.apache.hadoop.hive.metastore.events.AlterTableEvent) DropDatabaseEvent(org.apache.hadoop.hive.metastore.events.DropDatabaseEvent) CreateTableEvent(org.apache.hadoop.hive.metastore.events.CreateTableEvent) DropTableEvent(org.apache.hadoop.hive.metastore.events.DropTableEvent) AddPartitionEvent(org.apache.hadoop.hive.metastore.events.AddPartitionEvent) MetastoreUnitTest(org.apache.hadoop.hive.metastore.annotation.MetastoreUnitTest) Test(org.junit.Test)

Example 2 with DropDatabaseEvent

use of org.apache.hadoop.hive.metastore.events.DropDatabaseEvent in project hive by apache.

the class TestMetaStoreEventListener method testListener.

@Test
public void testListener() throws Exception {
    int listSize = 0;
    List<ListenerEvent> notifyList = DummyListener.notifyList;
    List<PreEventContext> preNotifyList = DummyPreListener.notifyList;
    assertEquals(notifyList.size(), listSize);
    assertEquals(preNotifyList.size(), listSize);
    Database db = new DatabaseBuilder().setName(dbName).build();
    msc.createDatabase(db);
    listSize++;
    PreCreateDatabaseEvent preDbEvent = (PreCreateDatabaseEvent) (preNotifyList.get(preNotifyList.size() - 1));
    db = msc.getDatabase(dbName);
    assertEquals(listSize, notifyList.size());
    assertEquals(listSize + 1, preNotifyList.size());
    validateCreateDb(db, preDbEvent.getDatabase());
    CreateDatabaseEvent dbEvent = (CreateDatabaseEvent) (notifyList.get(listSize - 1));
    Assert.assertTrue(dbEvent.getStatus());
    validateCreateDb(db, dbEvent.getDatabase());
    Table table = new TableBuilder().setDbName(db).setTableName(tblName).addCol("a", "string").addPartCol("b", "string").build();
    msc.createTable(table);
    PreCreateTableEvent preTblEvent = (PreCreateTableEvent) (preNotifyList.get(preNotifyList.size() - 1));
    listSize++;
    Table tbl = msc.getTable(dbName, tblName);
    validateCreateTable(tbl, preTblEvent.getTable());
    assertEquals(notifyList.size(), listSize);
    CreateTableEvent tblEvent = (CreateTableEvent) (notifyList.get(listSize - 1));
    Assert.assertTrue(tblEvent.getStatus());
    validateCreateTable(tbl, tblEvent.getTable());
    Partition part = new PartitionBuilder().fromTable(table).addValue("2011").build();
    msc.add_partition(part);
    listSize++;
    assertEquals(notifyList.size(), listSize);
    PreAddPartitionEvent prePartEvent = (PreAddPartitionEvent) (preNotifyList.get(preNotifyList.size() - 1));
    AddPartitionEvent partEvent = (AddPartitionEvent) (notifyList.get(listSize - 1));
    Assert.assertTrue(partEvent.getStatus());
    part = msc.getPartition("hive2038", "tmptbl", "b=2011");
    Partition partAdded = partEvent.getPartitionIterator().next();
    validateAddPartition(part, partAdded);
    validateTableInAddPartition(tbl, partEvent.getTable());
    validateAddPartition(part, prePartEvent.getPartitions().get(0));
    // Test adding multiple partitions in a single partition-set, atomically.
    int currentTime = (int) System.currentTimeMillis();
    HiveMetaStoreClient hmsClient = new HiveMetaStoreClient(conf);
    table = hmsClient.getTable(dbName, "tmptbl");
    Partition partition1 = new Partition(Arrays.asList("20110101"), dbName, "tmptbl", currentTime, currentTime, table.getSd(), table.getParameters());
    Partition partition2 = new Partition(Arrays.asList("20110102"), dbName, "tmptbl", currentTime, currentTime, table.getSd(), table.getParameters());
    Partition partition3 = new Partition(Arrays.asList("20110103"), dbName, "tmptbl", currentTime, currentTime, table.getSd(), table.getParameters());
    hmsClient.add_partitions(Arrays.asList(partition1, partition2, partition3));
    ++listSize;
    AddPartitionEvent multiplePartitionEvent = (AddPartitionEvent) (notifyList.get(listSize - 1));
    assertEquals("Unexpected table value.", table, multiplePartitionEvent.getTable());
    List<Partition> multiParts = Lists.newArrayList(multiplePartitionEvent.getPartitionIterator());
    assertEquals("Unexpected number of partitions in event!", 3, multiParts.size());
    assertEquals("Unexpected partition value.", partition1.getValues(), multiParts.get(0).getValues());
    assertEquals("Unexpected partition value.", partition2.getValues(), multiParts.get(1).getValues());
    assertEquals("Unexpected partition value.", partition3.getValues(), multiParts.get(2).getValues());
    part.setLastAccessTime((int) (System.currentTimeMillis() / 1000));
    msc.alter_partition(dbName, tblName, part);
    listSize++;
    assertEquals(notifyList.size(), listSize);
    PreAlterPartitionEvent preAlterPartEvent = (PreAlterPartitionEvent) preNotifyList.get(preNotifyList.size() - 1);
    // the partition did not change,
    // so the new partition should be similar to the original partition
    Partition origP = msc.getPartition(dbName, tblName, "b=2011");
    AlterPartitionEvent alterPartEvent = (AlterPartitionEvent) notifyList.get(listSize - 1);
    Assert.assertTrue(alterPartEvent.getStatus());
    validateAlterPartition(origP, origP, alterPartEvent.getOldPartition().getDbName(), alterPartEvent.getOldPartition().getTableName(), alterPartEvent.getOldPartition().getValues(), alterPartEvent.getNewPartition());
    validateAlterPartition(origP, origP, preAlterPartEvent.getDbName(), preAlterPartEvent.getTableName(), preAlterPartEvent.getNewPartition().getValues(), preAlterPartEvent.getNewPartition());
    List<String> part_vals = new ArrayList<>();
    part_vals.add("c=2012");
    int preEventListSize;
    preEventListSize = preNotifyList.size() + 1;
    Partition newPart = msc.appendPartition(dbName, tblName, part_vals);
    listSize++;
    assertEquals(notifyList.size(), listSize);
    assertEquals(preNotifyList.size(), preEventListSize);
    AddPartitionEvent appendPartEvent = (AddPartitionEvent) (notifyList.get(listSize - 1));
    Partition partAppended = appendPartEvent.getPartitionIterator().next();
    validateAddPartition(newPart, partAppended);
    PreAddPartitionEvent preAppendPartEvent = (PreAddPartitionEvent) (preNotifyList.get(preNotifyList.size() - 1));
    validateAddPartition(newPart, preAppendPartEvent.getPartitions().get(0));
    Table renamedTable = new Table(table);
    renamedTable.setTableName(renamed);
    msc.alter_table(dbName, tblName, renamedTable);
    listSize++;
    assertEquals(notifyList.size(), listSize);
    PreAlterTableEvent preAlterTableE = (PreAlterTableEvent) preNotifyList.get(preNotifyList.size() - 1);
    renamedTable = msc.getTable(dbName, renamed);
    AlterTableEvent alterTableE = (AlterTableEvent) notifyList.get(listSize - 1);
    Assert.assertTrue(alterTableE.getStatus());
    validateAlterTable(tbl, renamedTable, alterTableE.getOldTable(), alterTableE.getNewTable());
    validateAlterTable(tbl, renamedTable, preAlterTableE.getOldTable(), preAlterTableE.getNewTable());
    // change the table name back
    table = new Table(renamedTable);
    table.setTableName(tblName);
    msc.alter_table(dbName, renamed, table);
    listSize++;
    assertEquals(notifyList.size(), listSize);
    table = msc.getTable(dbName, tblName);
    table.getSd().addToCols(new FieldSchema("c", "int", ""));
    msc.alter_table(dbName, tblName, table);
    listSize++;
    assertEquals(notifyList.size(), listSize);
    preAlterTableE = (PreAlterTableEvent) preNotifyList.get(preNotifyList.size() - 1);
    Table altTable = msc.getTable(dbName, tblName);
    alterTableE = (AlterTableEvent) notifyList.get(listSize - 1);
    Assert.assertTrue(alterTableE.getStatus());
    validateAlterTableColumns(tbl, altTable, alterTableE.getOldTable(), alterTableE.getNewTable());
    validateAlterTableColumns(tbl, altTable, preAlterTableE.getOldTable(), preAlterTableE.getNewTable());
    Map<String, String> kvs = new HashMap<>(1);
    kvs.put("b", "2011");
    msc.markPartitionForEvent("hive2038", "tmptbl", kvs, PartitionEventType.LOAD_DONE);
    listSize++;
    assertEquals(notifyList.size(), listSize);
    LoadPartitionDoneEvent partMarkEvent = (LoadPartitionDoneEvent) notifyList.get(listSize - 1);
    Assert.assertTrue(partMarkEvent.getStatus());
    validateLoadPartitionDone("tmptbl", kvs, partMarkEvent.getTable().getTableName(), partMarkEvent.getPartitionName());
    PreLoadPartitionDoneEvent prePartMarkEvent = (PreLoadPartitionDoneEvent) preNotifyList.get(preNotifyList.size() - 1);
    validateLoadPartitionDone("tmptbl", kvs, prePartMarkEvent.getTableName(), prePartMarkEvent.getPartitionName());
    msc.dropPartition(dbName, tblName, Collections.singletonList("2011"));
    listSize++;
    assertEquals(notifyList.size(), listSize);
    PreDropPartitionEvent preDropPart = (PreDropPartitionEvent) preNotifyList.get(preNotifyList.size() - 1);
    DropPartitionEvent dropPart = (DropPartitionEvent) notifyList.get(listSize - 1);
    Assert.assertTrue(dropPart.getStatus());
    validateDropPartition(Collections.singletonList(part).iterator(), dropPart.getPartitionIterator());
    validateTableInDropPartition(tbl, dropPart.getTable());
    validateDropPartition(Collections.singletonList(part).iterator(), preDropPart.getPartitionIterator());
    validateTableInDropPartition(tbl, preDropPart.getTable());
    msc.dropTable(dbName, tblName);
    listSize++;
    assertEquals(notifyList.size(), listSize);
    PreDropTableEvent preDropTbl = (PreDropTableEvent) preNotifyList.get(preNotifyList.size() - 1);
    DropTableEvent dropTbl = (DropTableEvent) notifyList.get(listSize - 1);
    Assert.assertTrue(dropTbl.getStatus());
    validateDropTable(tbl, dropTbl.getTable());
    validateDropTable(tbl, preDropTbl.getTable());
    msc.dropDatabase(dbName);
    listSize++;
    assertEquals(notifyList.size(), listSize);
    PreDropDatabaseEvent preDropDB = (PreDropDatabaseEvent) preNotifyList.get(preNotifyList.size() - 1);
    DropDatabaseEvent dropDB = (DropDatabaseEvent) notifyList.get(listSize - 1);
    Assert.assertTrue(dropDB.getStatus());
    validateDropDb(db, dropDB.getDatabase());
    validateDropDb(db, preDropDB.getDatabase());
    msc.setMetaConf("metastore.try.direct.sql", "false");
    ConfigChangeEvent event = (ConfigChangeEvent) notifyList.get(notifyList.size() - 1);
    assertEquals("metastore.try.direct.sql", event.getKey());
    assertEquals("true", event.getOldValue());
    assertEquals("false", event.getNewValue());
}
Also used : PreAddPartitionEvent(org.apache.hadoop.hive.metastore.events.PreAddPartitionEvent) HashMap(java.util.HashMap) CreateDatabaseEvent(org.apache.hadoop.hive.metastore.events.CreateDatabaseEvent) PreCreateDatabaseEvent(org.apache.hadoop.hive.metastore.events.PreCreateDatabaseEvent) PreAlterPartitionEvent(org.apache.hadoop.hive.metastore.events.PreAlterPartitionEvent) FieldSchema(org.apache.hadoop.hive.metastore.api.FieldSchema) ArrayList(java.util.ArrayList) PreAlterTableEvent(org.apache.hadoop.hive.metastore.events.PreAlterTableEvent) PreLoadPartitionDoneEvent(org.apache.hadoop.hive.metastore.events.PreLoadPartitionDoneEvent) TableBuilder(org.apache.hadoop.hive.metastore.client.builder.TableBuilder) PreDropDatabaseEvent(org.apache.hadoop.hive.metastore.events.PreDropDatabaseEvent) ListenerEvent(org.apache.hadoop.hive.metastore.events.ListenerEvent) AlterTableEvent(org.apache.hadoop.hive.metastore.events.AlterTableEvent) PreAlterTableEvent(org.apache.hadoop.hive.metastore.events.PreAlterTableEvent) DropDatabaseEvent(org.apache.hadoop.hive.metastore.events.DropDatabaseEvent) PreDropDatabaseEvent(org.apache.hadoop.hive.metastore.events.PreDropDatabaseEvent) PreCreateTableEvent(org.apache.hadoop.hive.metastore.events.PreCreateTableEvent) CreateTableEvent(org.apache.hadoop.hive.metastore.events.CreateTableEvent) PartitionBuilder(org.apache.hadoop.hive.metastore.client.builder.PartitionBuilder) PreLoadPartitionDoneEvent(org.apache.hadoop.hive.metastore.events.PreLoadPartitionDoneEvent) LoadPartitionDoneEvent(org.apache.hadoop.hive.metastore.events.LoadPartitionDoneEvent) PreDropTableEvent(org.apache.hadoop.hive.metastore.events.PreDropTableEvent) DropTableEvent(org.apache.hadoop.hive.metastore.events.DropTableEvent) Database(org.apache.hadoop.hive.metastore.api.Database) PreCreateTableEvent(org.apache.hadoop.hive.metastore.events.PreCreateTableEvent) Partition(org.apache.hadoop.hive.metastore.api.Partition) PreDropPartitionEvent(org.apache.hadoop.hive.metastore.events.PreDropPartitionEvent) DropPartitionEvent(org.apache.hadoop.hive.metastore.events.DropPartitionEvent) Table(org.apache.hadoop.hive.metastore.api.Table) AlterPartitionEvent(org.apache.hadoop.hive.metastore.events.AlterPartitionEvent) PreAlterPartitionEvent(org.apache.hadoop.hive.metastore.events.PreAlterPartitionEvent) PreCreateDatabaseEvent(org.apache.hadoop.hive.metastore.events.PreCreateDatabaseEvent) DatabaseBuilder(org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder) PreDropPartitionEvent(org.apache.hadoop.hive.metastore.events.PreDropPartitionEvent) PreEventContext(org.apache.hadoop.hive.metastore.events.PreEventContext) PreDropTableEvent(org.apache.hadoop.hive.metastore.events.PreDropTableEvent) ConfigChangeEvent(org.apache.hadoop.hive.metastore.events.ConfigChangeEvent) PreAddPartitionEvent(org.apache.hadoop.hive.metastore.events.PreAddPartitionEvent) AddPartitionEvent(org.apache.hadoop.hive.metastore.events.AddPartitionEvent) MetastoreUnitTest(org.apache.hadoop.hive.metastore.annotation.MetastoreUnitTest) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)2 MetastoreUnitTest (org.apache.hadoop.hive.metastore.annotation.MetastoreUnitTest)2 AddPartitionEvent (org.apache.hadoop.hive.metastore.events.AddPartitionEvent)2 AlterTableEvent (org.apache.hadoop.hive.metastore.events.AlterTableEvent)2 CreateDatabaseEvent (org.apache.hadoop.hive.metastore.events.CreateDatabaseEvent)2 CreateTableEvent (org.apache.hadoop.hive.metastore.events.CreateTableEvent)2 DropDatabaseEvent (org.apache.hadoop.hive.metastore.events.DropDatabaseEvent)2 DropPartitionEvent (org.apache.hadoop.hive.metastore.events.DropPartitionEvent)2 DropTableEvent (org.apache.hadoop.hive.metastore.events.DropTableEvent)2 ListenerEvent (org.apache.hadoop.hive.metastore.events.ListenerEvent)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 Database (org.apache.hadoop.hive.metastore.api.Database)1 FieldSchema (org.apache.hadoop.hive.metastore.api.FieldSchema)1 Partition (org.apache.hadoop.hive.metastore.api.Partition)1 Table (org.apache.hadoop.hive.metastore.api.Table)1 DatabaseBuilder (org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder)1 PartitionBuilder (org.apache.hadoop.hive.metastore.client.builder.PartitionBuilder)1 TableBuilder (org.apache.hadoop.hive.metastore.client.builder.TableBuilder)1 AlterPartitionEvent (org.apache.hadoop.hive.metastore.events.AlterPartitionEvent)1