use of org.apache.flume.Transaction in project rocketmq-externals by apache.
the class RocketMQSinkTest method testBatchEvent.
@Test
public void testBatchEvent() throws MQClientException, InterruptedException, EventDeliveryException, RemotingException, MQBrokerException, UnsupportedEncodingException {
/*
start sink
*/
Context context = new Context();
context.put(NAME_SERVER_CONFIG, nameServer);
context.put(TAG_CONFIG, tag);
context.put(BATCH_SIZE_CONFIG, String.valueOf(batchSize));
RocketMQSink sink = new RocketMQSink();
Configurables.configure(sink, context);
MemoryChannel channel = new MemoryChannel();
Configurables.configure(channel, context);
sink.setChannel(channel);
sink.start();
/*
mock flume source
*/
Map<String, String> msgs = new HashMap<>();
Transaction tx = channel.getTransaction();
tx.begin();
int sendNum = 0;
for (int i = 0; i < batchSize; i++) {
String sendMsg = "\"Hello RocketMQ\"" + "," + DateFormatUtils.format(new Date(), "yyyy-MM-DD hh:mm:ss:SSSS");
Event event = EventBuilder.withBody(sendMsg.getBytes(), null);
channel.put(event);
log.info("publish message : {}", sendMsg);
String[] sendMsgKv = sendMsg.split(",");
msgs.put(sendMsgKv[1], sendMsgKv[0]);
sendNum++;
Thread.sleep(10);
}
log.info("send message num={}", sendNum);
tx.commit();
tx.close();
Sink.Status status = sink.process();
if (status == Sink.Status.BACKOFF) {
fail("Error");
}
sink.stop();
/*
consumer message
*/
consumer = new DefaultMQPullConsumer(consumerGroup);
consumer.setNamesrvAddr(nameServer);
consumer.setMessageModel(MessageModel.valueOf("BROADCASTING"));
consumer.registerMessageQueueListener(TOPIC_DEFAULT, null);
consumer.start();
int receiveNum = 0;
String receiveMsg = null;
Set<MessageQueue> queues = consumer.fetchSubscribeMessageQueues(TOPIC_DEFAULT);
for (MessageQueue queue : queues) {
long offset = getMessageQueueOffset(queue);
PullResult pullResult = consumer.pull(queue, tag, 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]);
log.info("receive message : {}", receiveMsg);
receiveNum++;
}
long nextBeginOffset = pullResult.getNextBeginOffset();
putMessageQueueOffset(queue, nextBeginOffset);
}
}
log.info("receive message num={}", receiveNum);
/*
wait for processQueueTable init
*/
Thread.sleep(1000);
consumer.shutdown();
assertEquals(msgs.size(), 0);
}
use of org.apache.flume.Transaction in project phoenix by apache.
the class CsvEventSerializerIT method testBatchEvents.
@Test
public void testBatchEvents() throws EventDeliveryException, SQLException {
final String fullTableName = "FLUME_CSV_TEST";
initSinkContextWithDefaults(fullTableName);
sink = new PhoenixSink();
Configurables.configure(sink, sinkContext);
assertEquals(LifecycleState.IDLE, sink.getLifecycleState());
final Channel channel = this.initChannel();
sink.setChannel(channel);
sink.start();
int numEvents = 150;
String col1 = "val1";
String a1 = "\"aaa,bbb,ccc\"";
String a2 = "\"1,2,3,4\"";
String eventBody = null;
List<Event> eventList = Lists.newArrayListWithCapacity(numEvents);
for (int i = 0; i < eventList.size(); i++) {
eventBody = (col1 + i) + "," + i * 10.5 + "," + a1 + "," + a2;
Event event = EventBuilder.withBody(Bytes.toBytes(eventBody));
eventList.add(event);
}
// put event in channel
Transaction transaction = channel.getTransaction();
transaction.begin();
for (Event event : eventList) {
channel.put(event);
}
transaction.commit();
transaction.close();
sink.process();
int rowsInDb = countRows(fullTableName);
assertEquals(eventList.size(), rowsInDb);
sink.stop();
assertEquals(LifecycleState.STOP, sink.getLifecycleState());
dropTable(fullTableName);
}
use of org.apache.flume.Transaction in project phoenix by apache.
the class CsvEventSerializerIT method testWithDefaultDelimiters.
@Test
public void testWithDefaultDelimiters() throws EventDeliveryException, SQLException {
final String fullTableName = "FLUME_CSV_TEST";
String ddl = "CREATE TABLE IF NOT EXISTS " + fullTableName + " (flume_time timestamp not null, col1 varchar , col2 double, col3 varchar[], col4 integer[]" + " CONSTRAINT pk PRIMARY KEY (flume_time))\n";
String columns = "col1,col2,col3,col4";
String rowkeyType = DefaultKeyGenerator.TIMESTAMP.name();
initSinkContext(fullTableName, ddl, columns, null, null, null, null, rowkeyType, null);
sink = new PhoenixSink();
Configurables.configure(sink, sinkContext);
assertEquals(LifecycleState.IDLE, sink.getLifecycleState());
final Channel channel = this.initChannel();
sink.setChannel(channel);
sink.start();
final String eventBody = "kalyan,10.5,\"abc,pqr,xyz\",\"1,2,3,4\"";
final Event event = EventBuilder.withBody(Bytes.toBytes(eventBody));
// put event in channel
Transaction transaction = channel.getTransaction();
transaction.begin();
channel.put(event);
transaction.commit();
transaction.close();
sink.process();
int rowsInDb = countRows(fullTableName);
assertEquals(1, rowsInDb);
sink.stop();
assertEquals(LifecycleState.STOP, sink.getLifecycleState());
dropTable(fullTableName);
}
use of org.apache.flume.Transaction in project logging-log4j2 by apache.
the class FlumeAppenderTest method testStructured.
@Test
public void testStructured() throws IOException {
final Agent[] agents = new Agent[] { Agent.createAgent("localhost", testPort) };
final FlumeAppender avroAppender = FlumeAppender.createAppender(agents, null, null, "false", "Avro", null, "1000", "1000", "1", "1000", "avro", "false", null, null, null, "ReqCtx_", null, "true", "1", null, null, null, null);
avroAppender.start();
final Logger eventLogger = (Logger) LogManager.getLogger("EventLogger");
Assert.assertNotNull(eventLogger);
eventLogger.addAppender(avroAppender);
eventLogger.setLevel(Level.ALL);
final StructuredDataMessage msg = new StructuredDataMessage("Transfer", "Success", "Audit");
msg.put("memo", "This is a memo");
msg.put("acct", "12345");
msg.put("amount", "100.00");
ThreadContext.put("id", UUID.randomUUID().toString());
ThreadContext.put("memo", null);
ThreadContext.put("test", "123");
EventLogger.logEvent(msg);
final Transaction transaction = channel.getTransaction();
transaction.begin();
final Event event = channel.take();
Assert.assertNotNull(event);
Assert.assertTrue("Channel contained event, but not expected message", getBody(event).endsWith("Success"));
transaction.commit();
transaction.close();
eventSource.stop();
eventLogger.removeAppender(avroAppender);
avroAppender.stop();
}
use of org.apache.flume.Transaction in project logging-log4j2 by apache.
the class FlumeAppenderTest method testMultiple.
@Test
public void testMultiple() throws IOException {
final Agent[] agents = new Agent[] { Agent.createAgent("localhost", testPort) };
final FlumeAppender avroAppender = FlumeAppender.createAppender(agents, null, null, "false", "Avro", null, "1000", "1000", "1", "1000", "avro", "false", null, null, null, null, null, "true", "1", null, null, null, null);
avroAppender.start();
avroLogger.addAppender(avroAppender);
avroLogger.setLevel(Level.ALL);
Assert.assertNotNull(avroLogger);
for (int i = 0; i < 10; ++i) {
avroLogger.info("Test message " + i);
}
for (int i = 0; i < 10; ++i) {
final Transaction transaction = channel.getTransaction();
transaction.begin();
final Event event = channel.take();
Assert.assertNotNull(event);
Assert.assertTrue("Channel contained event, but not expected message", getBody(event).endsWith("Test message " + i));
transaction.commit();
transaction.close();
}
eventSource.stop();
}
Aggregations