1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| package com.matrix.flume;
import org.apache.flume.Event; import org.apache.flume.EventDeliveryException; import org.apache.flume.api.RpcClient; import org.apache.flume.api.RpcClientFactory; import org.apache.flume.event.EventBuilder; import java.nio.charset.Charset;
public class MyApp { public static void main(String[] args) { MyRpcClientFacade client = new MyRpcClientFacade(); client.init("master", 41414);
String sampleData = "Hello Flume!"; for (int i = 0; i < 10; i++) { client.sendDataToFlume(sampleData); }
client.cleanUp(); } }
class MyRpcClientFacade { private RpcClient client; private String hostname; private int port;
public void init(String hostname, int port) { this.hostname = hostname; this.port = port; this.client = RpcClientFactory.getDefaultInstance(hostname, port); }
public void sendDataToFlume(String data) { Event event = EventBuilder.withBody(data, Charset.forName("UTF-8"));
try { client.append(event); } catch (EventDeliveryException e) { client.close(); client = null; client = RpcClientFactory.getDefaultInstance(hostname, port); } }
public void cleanUp() { client.close(); }
}
|