flume读取日志数据写入kafka

来源:互联网 发布:掌上公交软件下载 编辑:程序博客网 时间:2024/05/16 19:42

一、flume配置

flume要求1.6以上版本

flume-conf.properties文件配置内容,sinks的输出作为kafka的product

[html] view plaincopy
  1. a1.sources = r1  
  2. a1.sinks = k1  
  3. a1.channels = c1  
  4.   
  5. # Describe/configure the source  
  6. a1.sources.r1.type = exec  
  7. a1.sources.r1.command = tail -F /home/airib/work/log.log  
  8.   
  9. # Describe the sink  
  10. #a1.sinks.k1.type = logger  
  11. a1.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink  
  12. a1.sinks.k1.topic = test  
  13. a1.sinks.k1.brokerList = localhost:9092  
  14. a1.sinks.k1.requiredAcks = 1  
  15. a1.sinks.k1.batchSize = 20  
  16.   
  17. # Use a channel which buffers events in memory  
  18. a1.channels.c1.type = memory  
  19. a1.channels.c1.capacity = 1000  
  20. a1.channels.c1.transactionCapacity = 100  
  21.   
  22. # Bind the source and sink to the channel  
  23. a1.sources.r1.channels = c1  
  24. a1.sinks.k1.channel = c1  


flume启动

 bin/flume-ng agent --conf conf --conf-file conf/flume-conf.properties --name a1 -Dflume.root.logger=INFO,console


二  kafka的消费者java源代码

[html] view plaincopy
  1. package com.hgp.kafka.kafka;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6. import java.util.Properties;  
  7.   
  8. import kafka.consumer.ConsumerConfig;  
  9. import kafka.consumer.ConsumerIterator;  
  10. import kafka.consumer.KafkaStream;  
  11. import kafka.javaapi.consumer.ConsumerConnector;  
  12. import kafka.serializer.StringDecoder;  
  13. import kafka.utils.VerifiableProperties;  
  14.   
  15. public class KafkaConsumer {  
  16.   
  17.     private final ConsumerConnector consumer;  
  18.   
  19.     private KafkaConsumer() {  
  20.         Properties props = new Properties();  
  21.         //zookeeper 配置  
  22.         props.put("zookeeper.connect", "localhost:2181");  
  23.   
  24.         //group 代表一个消费组  
  25.         props.put("group.id", "jd-group");  
  26.   
  27.         //zk连接超时  
  28.         props.put("zookeeper.session.timeout.ms", "4000");  
  29.         props.put("zookeeper.sync.time.ms", "200");  
  30.         props.put("auto.commit.interval.ms", "1000");  
  31.         props.put("auto.offset.reset", "smallest");  
  32.         //序列化类  
  33.         props.put("serializer.class", "kafka.serializer.StringEncoder");  
  34.   
  35.         ConsumerConfig config = new ConsumerConfig(props);  
  36.   
  37.         consumer = kafka.consumer.Consumer.createJavaConsumerConnector(config);  
  38.     }  
  39.   
  40.     void consume() {  
  41.         Map<String, Integer> topicCountMap = new HashMap<String, Integer>();  
  42.         topicCountMap.put("test", new Integer(1));  
  43.   
  44.         StringDecoder keyDecoder = new StringDecoder(new VerifiableProperties());  
  45.         StringDecoder valueDecoder = new StringDecoder(new VerifiableProperties());  
  46.   
  47.         Map<String, List<KafkaStream<String, String>>> consumerMap =   
  48.                 consumer.createMessageStreams(topicCountMap,keyDecoder,valueDecoder);  
  49.         KafkaStream<String, String> stream = consumerMap.get("test").get(0);  
  50.         ConsumerIterator<String, String> it = stream.iterator();  
  51.         while (it.hasNext())  
  52.             System.out.println(it.next().message());  
  53.     }  
  54.   
  55.     public static void main(String[] args) {  
  56.         new KafkaConsumer().consume();  
  57.     }  
  58. }  

kafka启动命令

启动Zookeeper server: 

bin/zookeeper-server-start.sh config/zookeeper.properties &


启动Kafka server:

bin/kafka-server-start.sh config/server.properties & 


运行producer: 

bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test


运行consumer: 

bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning 

0 0