/**
 * 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 com.dtstack.jlogstash.outputs;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;

import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dtstack.jlogstash.annotation.Required;
import com.dtstack.jlogstash.outputs.BaseOutput;
import com.dtstack.jlogstash.render.Formatter;
import com.google.common.collect.Maps;

/**
 * 
 * Reason: TODO ADD REASON(可选)
 * Date: 2016年8月31日 下午1:35:11
 * Company: www.dtstack.com
 * @author sishu.yss
 *
 */
@SuppressWarnings("serial")
public class Kafka extends BaseOutput {
	
	private static final Logger logger = LoggerFactory.getLogger(Kafka.class);

	private static ObjectMapper objectMapper = new ObjectMapper();
	
	private Properties props;
	
	private ProducerConfig pconfig;
	
	private Producer<String, byte[]> producer;
	
	private static String encoding = "utf-8";
	
	private static String timezone;
	
	@Required(required=true)
	private static String topic;
	
	private static Map<String,Map<String,String>> topicSelect;
	
	private Set<Map.Entry<String,Map<String,String>>> entryTopicSelect;

	@Required(required=true)
	private static String brokerList;
	
	private static Map<String,String> producerSettings;
	
	@SuppressWarnings("rawtypes")
	public Kafka(Map config) {
		super(config);
	}
	
	/**
	 * default
	 * props.put("key.serializer.class", "kafka.serializer.StringEncoder");
	 * props.put("value.serializer.class", "kafka.serializer.StringEncoder");
	 * props.put("partitioner.class", "kafka.producer.DefaultPartitioner");
	 * props.put("producer.type", "sync");
	 * props.put("compression.codec", "none");
	 * props.put("request.required.acks", "1");
	 * props.put("batch.num.messages", "1024");
	 * props.put("client.id", "");			
	 */
	public void prepare() {
		try{
			if(topicSelect!=null){
				entryTopicSelect = topicSelect.entrySet();
			}
			
			if(props==null){
				props = new Properties();
			}
			if(producerSettings!=null){
				props.putAll(producerSettings);
			}
			props.put("metadata.broker.list",brokerList);
			if(pconfig==null){
				pconfig = new ProducerConfig(props);
			}
			if(producer==null){
				producer= new Producer<String, byte[]>(pconfig);
			}
		}catch(Exception e){
			logger.error(e.getMessage());
			System.exit(1);
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected void emit(Map event) {
		try {
			String tp = null;
			if(entryTopicSelect != null){
				for(Map.Entry<String,Map<String,String>> entry:entryTopicSelect){
					String key = entry.getKey();
					Map<String,String> value = entry.getValue();
					Set<Map.Entry<String,String>> sets = value.entrySet();
					for(Map.Entry<String,String> ey:sets){
						if(ey.getKey().equals(event.get(key))){
							tp = Formatter.format(event, ey.getValue(), timezone);
							break;
						}
					}
				}
			}
			if(tp==null){
				tp = Formatter.format(event, topic, timezone);
			}
			producer.send(new KeyedMessage<>(tp, event.toString(), objectMapper.writeValueAsString(event).getBytes(encoding)));
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
	}
	 public static void main(String[] args){
		 
		 Kafka.topic="oggoggogg";
		 Kafka.brokerList = "116.62.164.243:9092";
		 
		 Kafka.producerSettings= Maps.newConcurrentMap();
			 
		 Kafka.producerSettings.put("producer.type", "async");
		 Kafka.producerSettings.put("key.serializer.class", "kafka.serializer.StringEncoder");
		 Kafka.producerSettings.put("value.serializer.class", "kafka.serializer.StringEncoder");
		 
		 Kafka kafka = new Kafka(Maps.newConcurrentMap());
		 kafka.prepare();
		 
		 for(int i=0;i<10;i++){
			 Map dd = Maps.newConcurrentMap();
			 dd.put("hhh", 2123);
			 kafka.emit(dd);
		 }
		 
	 }
}

@@ -840,31 +840,8 @@
 s;%0A%0A
-import java.util.List;%0A
 impo
@@ -2684,16 +2684,27 @@
 %09%09%0A%09 */%0A
+%09@Override%0A
 %09public 
@@ -3265,16 +3265,27 @@
 ked%22 %7D)%0A
+%09@Override%0A
 %09protect

add override