Search in sources :

Example 1 with HCatEventMessage

use of org.apache.hive.hcatalog.messaging.HCatEventMessage in project hive by apache.

the class TestNotificationListener method onMessage.

@Override
public void onMessage(Message msg) {
    String event;
    try {
        event = msg.getStringProperty(HCatConstants.HCAT_EVENT);
        String format = msg.getStringProperty(HCatConstants.HCAT_MESSAGE_FORMAT);
        String version = msg.getStringProperty(HCatConstants.HCAT_MESSAGE_VERSION);
        String messageBody = ((TextMessage) msg).getText();
        actualMessages.add(event);
        MessageDeserializer deserializer = MessageFactory.getDeserializer(format, version);
        if (event.equals(HCatConstants.HCAT_CREATE_DATABASE_EVENT)) {
            Assert.assertEquals("topic://" + HCatConstants.HCAT_DEFAULT_TOPIC_PREFIX, msg.getJMSDestination().toString());
            CreateDatabaseMessage message = deserializer.getCreateDatabaseMessage(messageBody);
            Assert.assertEquals("mydb", message.getDB());
            HCatEventMessage message2 = MessagingUtils.getMessage(msg);
            Assert.assertTrue("Unexpected message-type.", message2 instanceof CreateDatabaseMessage);
            Assert.assertEquals("mydb", message2.getDB());
        } else if (event.equals(HCatConstants.HCAT_CREATE_TABLE_EVENT)) {
            Assert.assertEquals("topic://hcat.mydb", msg.getJMSDestination().toString());
            CreateTableMessage message = deserializer.getCreateTableMessage(messageBody);
            Assert.assertEquals("mytbl", message.getTable());
            Assert.assertEquals("mydb", message.getDB());
            Assert.assertEquals(TableType.MANAGED_TABLE.toString(), message.getTableType());
            HCatEventMessage message2 = MessagingUtils.getMessage(msg);
            Assert.assertTrue("Unexpected message-type.", message2 instanceof CreateTableMessage);
            Assert.assertEquals("mydb", message2.getDB());
            Assert.assertEquals("mytbl", ((CreateTableMessage) message2).getTable());
        } else if (event.equals(HCatConstants.HCAT_ADD_PARTITION_EVENT)) {
            Assert.assertEquals("topic://hcat.mydb.mytbl", msg.getJMSDestination().toString());
            AddPartitionMessage message = deserializer.getAddPartitionMessage(messageBody);
            Assert.assertEquals("mytbl", message.getTable());
            Assert.assertEquals("mydb", message.getDB());
            Assert.assertEquals(1, message.getPartitions().size());
            Assert.assertEquals("2011", message.getPartitions().get(0).get("b"));
            Assert.assertEquals(TableType.MANAGED_TABLE.toString(), message.getTableType());
            HCatEventMessage message2 = MessagingUtils.getMessage(msg);
            Assert.assertTrue("Unexpected message-type.", message2 instanceof AddPartitionMessage);
            Assert.assertEquals("mydb", message2.getDB());
            Assert.assertEquals("mytbl", ((AddPartitionMessage) message2).getTable());
            Assert.assertEquals(1, ((AddPartitionMessage) message2).getPartitions().size());
            Assert.assertEquals("2011", ((AddPartitionMessage) message2).getPartitions().get(0).get("b"));
        } else if (event.equals(HCatConstants.HCAT_ALTER_PARTITION_EVENT)) {
            Assert.assertEquals("topic://hcat.mydb.mytbl", msg.getJMSDestination().toString());
            // for alter partition events
            AlterPartitionMessage message = deserializer.getAlterPartitionMessage(messageBody);
            Assert.assertEquals("mytbl", message.getTable());
            Assert.assertEquals("mydb", message.getDB());
            Assert.assertEquals(1, message.getKeyValues().size());
            Assert.assertTrue(message.getKeyValues().values().contains("2011"));
            Assert.assertEquals(TableType.MANAGED_TABLE.toString(), message.getTableType());
            HCatEventMessage message2 = MessagingUtils.getMessage(msg);
            Assert.assertTrue("Unexpected message-type.", message2 instanceof AlterPartitionMessage);
            Assert.assertEquals("mydb", message2.getDB());
            Assert.assertEquals("mytbl", ((AlterPartitionMessage) message2).getTable());
            Assert.assertEquals(1, ((AlterPartitionMessage) message2).getKeyValues().size());
            Assert.assertTrue(((AlterPartitionMessage) message2).getKeyValues().values().contains("2011"));
        } else if (event.equals(HCatConstants.HCAT_DROP_PARTITION_EVENT)) {
            Assert.assertEquals("topic://hcat.mydb.mytbl", msg.getJMSDestination().toString());
            DropPartitionMessage message = deserializer.getDropPartitionMessage(messageBody);
            Assert.assertEquals("mytbl", message.getTable());
            Assert.assertEquals("mydb", message.getDB());
            Assert.assertEquals(1, message.getPartitions().size());
            Assert.assertEquals("2011", message.getPartitions().get(0).get("b"));
            Assert.assertEquals(TableType.MANAGED_TABLE.toString(), message.getTableType());
            HCatEventMessage message2 = MessagingUtils.getMessage(msg);
            Assert.assertTrue("Unexpected message-type.", message2 instanceof DropPartitionMessage);
            Assert.assertEquals("mydb", message2.getDB());
            Assert.assertEquals("mytbl", ((DropPartitionMessage) message2).getTable());
            Assert.assertEquals(1, ((DropPartitionMessage) message2).getPartitions().size());
            Assert.assertEquals("2011", ((DropPartitionMessage) message2).getPartitions().get(0).get("b"));
        } else if (event.equals(HCatConstants.HCAT_DROP_TABLE_EVENT)) {
            Assert.assertEquals("topic://hcat.mydb", msg.getJMSDestination().toString());
            DropTableMessage message = deserializer.getDropTableMessage(messageBody);
            Assert.assertEquals("mytbl", message.getTable());
            Assert.assertEquals("mydb", message.getDB());
            Assert.assertEquals(TableType.MANAGED_TABLE.toString(), message.getTableType());
            HCatEventMessage message2 = MessagingUtils.getMessage(msg);
            Assert.assertTrue("Unexpected message-type.", message2 instanceof DropTableMessage);
            Assert.assertEquals("mydb", message2.getDB());
            Assert.assertEquals("mytbl", ((DropTableMessage) message2).getTable());
        } else if (event.equals(HCatConstants.HCAT_DROP_DATABASE_EVENT)) {
            Assert.assertEquals("topic://" + HCatConstants.HCAT_DEFAULT_TOPIC_PREFIX, msg.getJMSDestination().toString());
            DropDatabaseMessage message = deserializer.getDropDatabaseMessage(messageBody);
            Assert.assertEquals("mydb", message.getDB());
            HCatEventMessage message2 = MessagingUtils.getMessage(msg);
            Assert.assertTrue("Unexpected message-type.", message2 instanceof DropDatabaseMessage);
            Assert.assertEquals("mydb", message2.getDB());
        } else if (event.equals(HCatConstants.HCAT_ALTER_TABLE_EVENT)) {
            Assert.assertEquals("topic://hcat.mydb", msg.getJMSDestination().toString());
            AlterTableMessage message = deserializer.getAlterTableMessage(messageBody);
            Assert.assertEquals("mytbl", message.getTable());
            Assert.assertEquals("mydb", message.getDB());
            Assert.assertEquals(TableType.MANAGED_TABLE.toString(), message.getTableType());
            HCatEventMessage message2 = MessagingUtils.getMessage(msg);
            Assert.assertTrue("Unexpected message-type.", message2 instanceof AlterTableMessage);
            Assert.assertEquals("mydb", message2.getDB());
            Assert.assertEquals("mytbl", ((AlterTableMessage) message2).getTable());
        } else if (event.equals(HCatConstants.HCAT_PARTITION_DONE_EVENT)) {
            // TODO: Fill in when PARTITION_DONE_EVENT is supported.
            Assert.assertTrue("Unexpected: HCAT_PARTITION_DONE_EVENT not supported (yet).", false);
        } else {
            Assert.assertTrue("Unexpected event-type: " + event, false);
        }
    } catch (JMSException e) {
        e.printStackTrace(System.err);
        assert false;
    } finally {
        messageReceivedSignal.countDown();
    }
}
Also used : MessageDeserializer(org.apache.hive.hcatalog.messaging.MessageDeserializer) CreateDatabaseMessage(org.apache.hive.hcatalog.messaging.CreateDatabaseMessage) CreateTableMessage(org.apache.hive.hcatalog.messaging.CreateTableMessage) JMSException(javax.jms.JMSException) HCatEventMessage(org.apache.hive.hcatalog.messaging.HCatEventMessage) DropTableMessage(org.apache.hive.hcatalog.messaging.DropTableMessage) DropPartitionMessage(org.apache.hive.hcatalog.messaging.DropPartitionMessage) DropDatabaseMessage(org.apache.hive.hcatalog.messaging.DropDatabaseMessage) AlterTableMessage(org.apache.hive.hcatalog.messaging.AlterTableMessage) AddPartitionMessage(org.apache.hive.hcatalog.messaging.AddPartitionMessage) TextMessage(javax.jms.TextMessage) AlterPartitionMessage(org.apache.hive.hcatalog.messaging.AlterPartitionMessage)

Example 2 with HCatEventMessage

use of org.apache.hive.hcatalog.messaging.HCatEventMessage in project hive by apache.

the class TestMsgBusConnection method testConnection.

@Test
public void testConnection() throws Exception {
    driver.run("create database testconndb");
    Message msg = consumer.receive(TIMEOUT);
    assertTrue("Expected TextMessage", msg instanceof TextMessage);
    assertEquals(HCatConstants.HCAT_CREATE_DATABASE_EVENT, msg.getStringProperty(HCatConstants.HCAT_EVENT));
    assertEquals("topic://planetlab.hcat", msg.getJMSDestination().toString());
    HCatEventMessage messageObject = MessagingUtils.getMessage(msg);
    assertEquals("testconndb", messageObject.getDB());
    broker.stop();
    runQuery("drop database testconndb cascade");
    broker.start(true);
    connectClient();
    runQuery("create database testconndb");
    msg = consumer.receive(TIMEOUT);
    assertEquals(HCatConstants.HCAT_CREATE_DATABASE_EVENT, msg.getStringProperty(HCatConstants.HCAT_EVENT));
    assertEquals("topic://planetlab.hcat", msg.getJMSDestination().toString());
    assertEquals("testconndb", messageObject.getDB());
    driver.run("drop database testconndb cascade");
    msg = consumer.receive(TIMEOUT);
    assertEquals(HCatConstants.HCAT_DROP_DATABASE_EVENT, msg.getStringProperty(HCatConstants.HCAT_EVENT));
    assertEquals("topic://planetlab.hcat", msg.getJMSDestination().toString());
    assertEquals("testconndb", messageObject.getDB());
}
Also used : HCatEventMessage(org.apache.hive.hcatalog.messaging.HCatEventMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) TextMessage(javax.jms.TextMessage) HCatEventMessage(org.apache.hive.hcatalog.messaging.HCatEventMessage) Test(org.junit.Test)

Example 3 with HCatEventMessage

use of org.apache.hive.hcatalog.messaging.HCatEventMessage in project oozie by apache.

the class HCatMessageHandler method process.

/**
 * Process JMS message produced by HCat.
 *
 * @param msg : to be processed
 */
@Override
public void process(Message msg) {
    try {
        HCatEventMessage hcatMsg = MessagingUtils.getMessage(msg);
        if (hcatMsg.getEventType().equals(HCatEventMessage.EventType.ADD_PARTITION)) {
            // Parse msg components
            AddPartitionMessage partMsg = (AddPartitionMessage) hcatMsg;
            String db = partMsg.getDB();
            String table = partMsg.getTable();
            LOG.info("Partition available event: db [{0}]  table [{1}] partitions [{2}]", db, table, partMsg.getPartitions());
            List<Map<String, String>> partitions = partMsg.getPartitions();
            for (int i = 0; i < partitions.size(); i++) {
                pdmService.partitionAvailable(this.server, db, table, partitions.get(i));
            }
        } else {
            LOG.debug("Ignoring message of event type [{0}] ", hcatMsg.getEventType());
        }
    } catch (Exception e) {
        LOG.warn("Error processing JMS message", e);
    }
}
Also used : AddPartitionMessage(org.apache.hive.hcatalog.messaging.AddPartitionMessage) Map(java.util.Map) HCatEventMessage(org.apache.hive.hcatalog.messaging.HCatEventMessage)

Example 4 with HCatEventMessage

use of org.apache.hive.hcatalog.messaging.HCatEventMessage in project hive by apache.

the class NotificationListener method send.

/**
 * @param hCatEventMessage The HCatEventMessage being sent over JMS, this method is threadsafe
 * @param topicName is the name on message broker on which message is sent.
 * @param retries the number of retry attempts
 */
protected void send(HCatEventMessage hCatEventMessage, String topicName, int retries) {
    try {
        if (session.get() == null) {
            // Need to reconnect
            throw new JMSException("Invalid JMS session");
        }
        Destination topic = createTopic(topicName);
        Message msg = session.get().createTextMessage(hCatEventMessage.toString());
        msg.setStringProperty(HCatConstants.HCAT_EVENT, hCatEventMessage.getEventType().toString());
        msg.setStringProperty(HCatConstants.HCAT_MESSAGE_VERSION, messageFactory.getVersion());
        msg.setStringProperty(HCatConstants.HCAT_MESSAGE_FORMAT, messageFactory.getMessageFormat());
        MessageProducer producer = createProducer(topic);
        producer.send(msg);
        // Message must be transacted before we return.
        session.get().commit();
    } catch (Exception e) {
        if (retries >= 0) {
            // this may happen if we were able to establish connection once, but its no longer valid
            LOG.error("Seems like connection is lost. Will retry. Retries left : " + retries + ". error was:", e);
            testAndCreateConnection();
            send(hCatEventMessage, topicName, retries - 1);
        } else {
            // Gobble up the exception. Message delivery is best effort.
            LOG.error("Failed to send message on topic: " + topicName + " event: " + hCatEventMessage.getEventType() + " after retries: " + NUM_RETRIES, e);
        }
    }
}
Also used : Destination(javax.jms.Destination) HCatEventMessage(org.apache.hive.hcatalog.messaging.HCatEventMessage) Message(javax.jms.Message) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) NamingException(javax.naming.NamingException) TException(org.apache.thrift.TException) JMSException(javax.jms.JMSException)

Aggregations

HCatEventMessage (org.apache.hive.hcatalog.messaging.HCatEventMessage)4 JMSException (javax.jms.JMSException)2 Message (javax.jms.Message)2 TextMessage (javax.jms.TextMessage)2 AddPartitionMessage (org.apache.hive.hcatalog.messaging.AddPartitionMessage)2 Map (java.util.Map)1 Destination (javax.jms.Destination)1 MessageProducer (javax.jms.MessageProducer)1 NamingException (javax.naming.NamingException)1 MetaException (org.apache.hadoop.hive.metastore.api.MetaException)1 AlterPartitionMessage (org.apache.hive.hcatalog.messaging.AlterPartitionMessage)1 AlterTableMessage (org.apache.hive.hcatalog.messaging.AlterTableMessage)1 CreateDatabaseMessage (org.apache.hive.hcatalog.messaging.CreateDatabaseMessage)1 CreateTableMessage (org.apache.hive.hcatalog.messaging.CreateTableMessage)1 DropDatabaseMessage (org.apache.hive.hcatalog.messaging.DropDatabaseMessage)1 DropPartitionMessage (org.apache.hive.hcatalog.messaging.DropPartitionMessage)1 DropTableMessage (org.apache.hive.hcatalog.messaging.DropTableMessage)1 MessageDeserializer (org.apache.hive.hcatalog.messaging.MessageDeserializer)1 TException (org.apache.thrift.TException)1 Test (org.junit.Test)1