package com.cloudera.flume.handlers.thrift;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.apache.thrift.TException;
import com.cloudera.flume.conf.Context;
import com.cloudera.flume.conf.FlumeConfiguration;
import com.cloudera.flume.conf.SinkFactory.SinkBuilder;
import com.cloudera.flume.core.Event;
import com.cloudera.flume.core.EventImpl;
import com.cloudera.flume.core.EventSink;
import com.cloudera.flume.handlers.hdfs.WriteableEvent;
super(host, port);
}
@Override
public void append(Event e)
throws IOException {
WriteableEvent we = new WriteableEvent(e);
RawEvent re = new RawEvent(ByteBuffer.wrap(we.toBytes()));
try {
client.rawAppend(re);
updateAppendStats(e);
} catch (TException e1) {
e1.printStackTrace();
throw new IOException("Append failed " + e);
}
}
public static SinkBuilder
builder() {
return new SinkBuilder() {
@Override
public EventSink
build(Context context, String... args) {
if (args.length > 2) {
throw new IllegalArgumentException(
"usage: trawsink([hostname, [portno]]) ");
}
String host = FlumeConfiguration.get().getCollectorHost();
int port = FlumeConfiguration.get().getCollectorPort();
if (args.length >= 1) {
host = args[0];
}
if (args.length >= 2) {
port = Integer.parseInt(args[1]);
}
return new ThriftRawEventSink(host, port);
}
};
}
public static void main(String argv[]) {
FlumeConfiguration conf = FlumeConfiguration.get();
ThriftRawEventSink sink =
new ThriftRawEventSink("localhost", conf.getCollectorPort());
try {
sink.open();
for (int i = 0; i < 100; i++) {
Event e = new EventImpl(("This is a test " + i).getBytes());
sink.append(e);
Thread.sleep(200);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}