Search in sources :

Example 1 with Transaction

use of org.apache.rocketmq.hbase.sink.Transaction in project rocketmq-externals by apache.

the class ReplicatorTest method insertData.

/**
 * Adds data to the previously created HBase table.
 *
 * @throws IOException
 */
private Transaction insertData(int numberOfRecords) throws IOException {
    final Transaction transaction = new Transaction(numberOfRecords);
    try (Table hTable = ConnectionFactory.createConnection(utility.getConfiguration()).getTable(TABLE_NAME)) {
        for (int i = 0; i < numberOfRecords; i++) {
            final byte[] rowKey = toBytes(String.format(ROWKEY, i));
            final byte[] family = toBytes(COLUMN_FAMILY);
            final Put put = new Put(rowKey);
            put.addColumn(family, toBytes(QUALIFIER), toBytes(VALUE));
            hTable.put(put);
            List<Cell> cells = put.getFamilyCellMap().get(family);
            transaction.addRow(TABLE_NAME_STR, rowKey, cells);
        }
    }
    return transaction;
}
Also used : Table(org.apache.hadoop.hbase.client.Table) Transaction(org.apache.rocketmq.hbase.sink.Transaction) Cell(org.apache.hadoop.hbase.Cell) Put(org.apache.hadoop.hbase.client.Put)

Example 2 with Transaction

use of org.apache.rocketmq.hbase.sink.Transaction in project rocketmq-externals by apache.

the class ReplicatorTest method testCustomReplicationEndpoint.

/**
 * This method tests the replicator by writing data from hbase to rocketmq and reading it back.
 *
 * @throws IOException
 * @throws ReplicationException
 * @throws InterruptedException
 * @throws MQClientException
 * @throws RemotingException
 * @throws MQBrokerException
 */
@Test
public void testCustomReplicationEndpoint() throws IOException, ReplicationException, InterruptedException, MQClientException, RemotingException, MQBrokerException {
    final DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(CONSUMER_GROUP_NAME);
    try {
        createTestTable();
        final Map<TableName, List<String>> tableCfs = new HashMap<>();
        List<String> cfs = new ArrayList<>();
        cfs.add(COLUMN_FAMILY);
        tableCfs.put(TABLE_NAME, cfs);
        addPeer(utility.getConfiguration(), PEER_NAME, tableCfs);
        // wait for new peer to be added
        Thread.sleep(500);
        final int numberOfRecords = 10;
        final Transaction inTransaction = insertData(numberOfRecords);
        // wait for data to be replicated
        Thread.sleep(500);
        consumer.setNamesrvAddr(NAMESERVER);
        consumer.setMessageModel(MessageModel.valueOf("BROADCASTING"));
        consumer.registerMessageQueueListener(ROCKETMQ_TOPIC, null);
        consumer.start();
        int receiveNum = 0;
        String receiveMsg = null;
        Set<MessageQueue> queues = consumer.fetchSubscribeMessageQueues(ROCKETMQ_TOPIC);
        for (MessageQueue queue : queues) {
            long offset = getMessageQueueOffset(consumer, queue);
            PullResult pullResult = consumer.pull(queue, null, offset, batchSize);
            if (pullResult.getPullStatus() == PullStatus.FOUND) {
                for (MessageExt message : pullResult.getMsgFoundList()) {
                    byte[] body = message.getBody();
                    receiveMsg = new String(body, "UTF-8");
                    // String[] receiveMsgKv = receiveMsg.split(",");
                    // msgs.remove(receiveMsgKv[1]);
                    logger.info("receive message : {}", receiveMsg);
                    receiveNum++;
                }
                long nextBeginOffset = pullResult.getNextBeginOffset();
                consumer.updateConsumeOffset(queue, offset);
            }
        }
        logger.info("receive message num={}", receiveNum);
        // wait for processQueueTable init
        Thread.sleep(1000);
        assertEquals(inTransaction.toJson(), receiveMsg);
    } finally {
        removePeer();
        consumer.shutdown();
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DefaultMQPullConsumer(org.apache.rocketmq.client.consumer.DefaultMQPullConsumer) PullResult(org.apache.rocketmq.client.consumer.PullResult) TableName(org.apache.hadoop.hbase.TableName) MessageExt(org.apache.rocketmq.common.message.MessageExt) Transaction(org.apache.rocketmq.hbase.sink.Transaction) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

Transaction (org.apache.rocketmq.hbase.sink.Transaction)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Cell (org.apache.hadoop.hbase.Cell)1 TableName (org.apache.hadoop.hbase.TableName)1 Put (org.apache.hadoop.hbase.client.Put)1 Table (org.apache.hadoop.hbase.client.Table)1 DefaultMQPullConsumer (org.apache.rocketmq.client.consumer.DefaultMQPullConsumer)1 PullResult (org.apache.rocketmq.client.consumer.PullResult)1 MessageExt (org.apache.rocketmq.common.message.MessageExt)1 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)1 Test (org.junit.Test)1