view raw
Uttam Raj Uttam Raj - 6 months ago 43
Java Question

How to start Flume agent from java code

I am using hadoop 1.2.1 stable version in centos 6.5 and using apache flume 1.x i am running the flume agent and collecting the tweets in hdfs my flume.conf is

TwitterAgent.sources = Twitter
TwitterAgent.channels = MemChannel
TwitterAgent.sinks = HDFS
TwitterAgent.sources.Twitter.type = com.cloudera.flume.source.TwitterSource
TwitterAgent.sources.Twitter.channels = MemChannel
TwitterAgent.sources.Twitter.consumerKey = ******
TwitterAgent.sources.Twitter.consumerSecret =*****
TwitterAgent.sources.Twitter.accessToken = *****
TwitterAgent.sources.Twitter.accessTokenSecret = ***
TwitterAgent.sources.Twitter.keywords = CrudeOilPrice,Crude Oil,platts oil, Oil & Gas Journal
TwitterAgent.sources.Twitter.keywords = big data,hadoop = MemChannel
TwitterAgent.sinks.HDFS.type = hdfs
TwitterAgent.sinks.HDFS.hdfs.path = hdfs://master:9000/user/flume/tweets/
TwitterAgent.sinks.HDFS.hdfs.fileType = DataStream
TwitterAgent.sinks.HDFS.hdfs.writeFormat = Text
TwitterAgent.sinks.HDFS.hdfs.batchSize = 1000
TwitterAgent.sinks.HDFS.hdfs.rollSize = 0
TwitterAgent.sinks.HDFS.hdfs.rollCount = 10000
TwitterAgent.channels.MemChannel.type = memory
TwitterAgent.channels.MemChannel.capacity = 10000
TwitterAgent.channels.MemChannel.transactionCapacity = 100

for run this i used command:

>bin/flume-ng agent --conf ./conf/ -f conf/flume.conf -Dflume.root.logger=DEBUG,console -n TwitterAgent

now i am trying to run this with java program can any one give some idea i tried this code

public class fl {

public static void main(String[] args) throws IOException, InterruptedException
Process p;

p = Runtime.getRuntime().exec("/home/dsri/flume/bin/flume-ng agent --conf ./conf/ -f conf/flume.conf -Dflume.root.logger=DEBUG,console -n TwitterAgent");


BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line = "";

while ((line = reader.readLine())!= null)

but is not working for me..
now i am doing this code in java....

package dsri;
//package org.jai.flume.agent;

import java.util.HashMap;
import java.util.Map;
import org.apache.flume.agent.embedded.EmbeddedAgent;

public class FlumeAgentServiceImpl {

private static EmbeddedAgent agent;
private void createAgent() {
final Map<String, String> properties = new HashMap<String, String>();

properties.put("channel.type", "memory");
properties.put("channel.capacity", "200");
properties.put("sinks", "sink1 sink2");
properties.put("sink1.type", "avro");
properties.put("sink2.type", "avro");
properties.put("sink1.hostname", "");
properties.put("sink1.port", "5564");
properties.put("sink2.hostname", "");
properties.put("sink2.port", "5565");
properties.put("processor.type", "load_balance");

agent = new EmbeddedAgent("myagent");


public EmbeddedAgent getFlumeAgent() {
if (agent == null) {
return agent;

public static void main(String[] args) {
FlumeAgentServiceImpl f= new FlumeAgentServiceImpl();


But I am getting an exception...

org.apache.flume.FlumeException: NettyAvroRpcClient { host:, port: 5564 }: RPC connection error
at org.apache.flume.api.NettyAvroRpcClient.connect(
at org.apache.flume.api.NettyAvroRpcClient.connect(
at org.apache.flume.api.NettyAvroRpcClient.configure(
at org.apache.flume.api.RpcClientFactory.getInstance(
at org.apache.flume.sink.AvroSink.initializeRpcClient(
at org.apache.flume.sink.AbstractRpcSink.createConnection(
at org.apache.flume.sink.AbstractRpcSink.start(
at org.apache.flume.sink.AbstractSinkProcessor.start(
at org.apache.flume.sink.LoadBalancingSinkProcessor.start(
at org.apache.flume.SinkRunner.start(
at org.apache.flume.agent.embedded.EmbeddedAgent.doStart(
at org.apache.flume.agent.embedded.EmbeddedAgent.start(
at dsri.FlumeAgentServiceImpl.createAgent(
at dsri.FlumeAgentServiceImpl.getFlumeAgent(
at dsri.FlumeAgentServiceImpl.main(
Caused by: Error connecting to
at org.apache.avro.ipc.NettyTransceiver.getChannel(
at org.apache.avro.ipc.NettyTransceiver.<init>(
at org.apache.avro.ipc.NettyTransceiver.<init>(
at org.apache.flume.api.NettyAvroRpcClient.connect(
... 14 more


You could also load the Flume Configuration file rather than writing it within the Java code.The same configuration could be used while starting your standalone Flume Agent.

public static void main(String[] args)
    String[] args = new String[] { "agent", "-nAgent",
            "-fflume.conf" };



Where "Agent" is the name of your Flume Agent.
"flume.conf" is the configuration file which should be placed in the resources folder of your Java project.