Flume Java Custom Sink和Source

时间:2018-04-18 13:57:58

标签: java hadoop apache-kafka flume flume-ng

Flume版本: - 1.6

卡夫卡版本: - 1.0.0

Zookeeper版本: - 3.4.10

我们快速要求将Flume与Kafka和Hadoop连接起来,因此我们从Kafka Consumer手中接收了这些事件,并将这些事件记录到了Hadoop。一切都是使用conf文件配置的,直到这里一切都很好。

现在我们需要检查是否可以使用自定义Java代码完成。我在互联网上尝试了很多选项来设计一个kafka源和HDFS接收器。我在cloudera VM中试过这个。

Kafka和zookeeper都已启动并正在运行。

代码正在运行,但是当我生成消息时,HDFS中没有插入任何内容。

如果有人可以指出我所遗漏的内容,那将会非常有用。

我尝试的代码是..

KafkaChannel channel = new KafkaChannel();

Map<String, String> channelParamters = new HashMap<String, String>();

channelParamters.put("brokerList", "localhost:9092");
channelParamters.put("zookeeperConnect","localhost:2181");
channelParamters.put("topic","integration");
channelParamters.put("groupId","channel");
channelParamters.put("batchSize", "15");
channelParamters.put("zookeeper.connect","localhost:2181");
channelParamters.put("clientId", "channel");
channelParamters.put("readSmallestOffset","true");
channelParamters.put("interceptors","i1");
channelParamters.put("interceptors.i1.type","host");
channelParamters.put("consumer.timeout.ms","1000");

channelParamters.put("parseAsFlumeEvent", "false");

channel.setName("KafkaSource");

Context channelContext = new Context(channelParamters);


final Map<String, String> properties = new HashMap<String, String>();

/** Sink Properties start **/

HDFSEventSink eventSink = new HDFSEventSink();

eventSink.setName("HDFSEventSink-" + "kafkaEventSink");

String hdfsBasePath = "hdfs://quickstart.cloudera:8020/user/cloudera/flume/events";

properties.put("hdfs.type", "hdfs");
properties.put("hdfs.path", hdfsBasePath + "/%Y/%m/%d/%H");
properties.put("hdfs.rollInterval ", "0");
properties.put("hdfs.rollSize ", "2048");
properties.put("hdfs.rollCount ", "0");
properties.put("hdfs.fileType ", " DataStream");
properties.put("channel", channel.getName());
properties.put("hdfs.maxOpenFiles", String.valueOf(1));

properties.put("hdfs.filePrefix ", " kafka_host");
properties.put("hdfs.fileSuffix ", " .txt");
properties.put("hdfs.idleTimeout ", "60");

/** Sink Properties end **/

Context sinkContext = new Context(properties);

eventSink.configure(sinkContext);

eventSink.setChannel(channel);

Configurables.configure(channel, channelContext);

eventSink.start();

channel.start();

1 个答案:

答案 0 :(得分:0)

我不清楚您要使用自定义Java代码实现的目标。

更好的方法是使用Kafka Connect(Apache Kafka的一部分)和开源HDFS connector。它完全正如你在这里做的那样,只是配置文件来设置,处理模式,扩展,自动故障转移等等。