/*******************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*******************************************************************************/
package org.apache.flume.source.kafka;
/**
* A Source for Kafka which reads messages from kafka. I use this in company production environment
* and its performance is good. Over 100k messages per second can be read from kafka in one source.<p>
* <tt>zookeeper.connect: </tt> the zookeeper ip kafka use.<p>
* <tt>topic: </tt> the topic to read from kafka.<p>
* <tt>group.id: </tt> the groupid of consumer group.<p>
*/
public class KafkaSource extends AbstractSource implements Configurable, PollableSource {
private static final Logger log = LoggerFactory.getLogger(KafkaSource.class);
private ConsumerConnector consumer;
private ConsumerIterator<byte[], byte[]> it;
private String topic;
public Status process() throws EventDeliveryException {
List<Event> eventList = new ArrayList<Event>();
MessageAndMetadata<byte[],byte[]> message;
Event event;
Map<String, String> headers;
String strMessage;
try {
if(it.hasNext()) {
message = it.next();
event = new SimpleEvent();
headers = new HashMap<String, String>();
headers.put("timestamp", String.valueOf(System.currentTimeMillis()));
public void configure(Context context) {
topic = context.getString("topic");
if(topic == null) {
throw new ConfigurationException("Kafka topic must be specified.");
}
try {
this.consumer = KafkaSourceUtil.getConsumer(context);
} catch (IOException e) {
log.error("IOException occur, {}", e.getMessage());
} catch (InterruptedException e) {
log.error("InterruptedException occur, {}", e.getMessage());
}
Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
topicCountMap.put(topic, new Integer(1));
Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);
if(consumerMap == null) {
throw new ConfigurationException("topicCountMap is null");
}
List<KafkaStream<byte[], byte[]>> topicList = consumerMap.get(topic);
if(topicList == null || topicList.isEmpty()) {
throw new ConfigurationException("topicList is null or empty");
}
KafkaStream<byte[], byte[]> stream = topicList.get(0);
it = stream.iterator();
}
@Override
public synchronized void stop() {
consumer.shutdown();
super.stop();
}
}
/*******************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*******************************************************************************/
package org.apache.flume.source.kafka;