Transceiver transceiver = null;
BufferedReader reader = null;
try {
transceiver = new NettyTransceiver(new InetSocketAddress(hostname, port));
AvroSourceProtocol client = SpecificRequestor.getClient(
AvroSourceProtocol.class, transceiver);
List<AvroFlumeEvent> eventBuffer = new ArrayList<AvroFlumeEvent>();
if (fileName != null) {
reader = new BufferedReader(new FileReader(new File(fileName)));
} else {
reader = new BufferedReader(new InputStreamReader(System.in));
}
String line = null;
long lastCheck = System.currentTimeMillis();
long sentBytes = 0;
while ((line = reader.readLine()) != null) {
// logger.debug("read:{}", line);
if (eventBuffer.size() >= 1000) {
Status status = client.appendBatch(eventBuffer);
if (!status.equals(Status.OK)) {
logger.error("Unable to send batch size:{} status:{}",
eventBuffer.size(), status);
}
eventBuffer.clear();
}
AvroFlumeEvent avroEvent = new AvroFlumeEvent();
avroEvent.headers = new HashMap<CharSequence, CharSequence>();
avroEvent.body = ByteBuffer.wrap(line.getBytes());
eventBuffer.add(avroEvent);
sentBytes += avroEvent.body.capacity();
sent++;
long now = System.currentTimeMillis();
if (now >= lastCheck + 5000) {
logger.debug("Packed {} bytes, {} events", sentBytes, sent);
lastCheck = now;
}
}
if (eventBuffer.size() > 0) {
Status status = client.appendBatch(eventBuffer);
if (!status.equals(Status.OK)) {
logger.error("Unable to send batch size:{} status:{}",
eventBuffer.size(), status);
}