学習kafka:log 4 j書き込みkafka


最近1日かけてkafka公式ドキュメントを1回見て、自分でテストして、特記録とこれ、原理概念はドキュメントを見終わって理解することができて、主に問題です.1バージョンのPOM構成は、jarパッケージが競合する可能性があることに注意してください.
 <dependency>
            <groupId>org.apache.kafkagroupId>
            <artifactId>kafka_2.10artifactId>
            <version>0.8.2.2version>
            
            
            
            
            
            
        dependency>
        
        <dependency>
            <groupId>org.apache.kafkagroupId>
            <artifactId>kafka-log4j-appenderartifactId>
            <version>0.10.2.1version>
        dependency>
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-log4j12artifactId>
            <version>1.7.5version>
        dependency>

2 log 4 j構成:
log4j.rootLogger=INFO,console,KAFKA
## appender KAFKA
log4j.appender.KAFKA=kafka.producer.KafkaLog4jAppender
log4j.appender.KAFKA.topic=test
log4j.appender.KAFKA.brokerList=127.0.0.1:9092
log4j.appender.KAFKA.pa=127.0.0.1:9092

log4j.appender.KAFKA.compressionType=none
log4j.appender.KAFKA.syncSend=true
log4j.appender.KAFKA.layout=org.apache.log4j.PatternLayout
log4j.appender.KAFKA.ThresholdFilter.level=INFO
log4j.appender.KAFKA.ThresholdFilter.onMatch=ACCEPT
log4j.appender.KAFKA.ThresholdFilter.onMismatch=DENY

log4j.appender.KAFKA.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L %% - %m%n

## appender console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d (%t) [%p - %l] %m%n

3テストコード:
package com.need.kafka.services.demo;

import com.need.kafka.services.utils.KafkaPropertiesManager;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
import org.slf4j.impl.StaticLoggerBinder;
import org.slf4j.*;

import java.util.ArrayList;
import java.util.List;

/**
 *  demo  
 * 
 */
public class ProducerDemo {

    private static final Logger log = LoggerFactory.getLogger(ProducerDemo.class);

    public static void main(String args[]) throws InterruptedException {
//        ProducerConfig producerConfig = new ProducerConfig(KafkaPropertiesManager.getProducerProps());
//        Producer producer = new Producer(producerConfig);
//        //  
//        for (int i = 0; i <= 10; i++) {
//            KeyedMessage message =
//                    new KeyedMessage(KafkaProperties.TEST_TOPIC, i + "", "Message" + i);
//            producer.send(message);
//        }
//
////        //  
//        List> messages = new ArrayList>(100);
//        for (int i = 0; i <= 100; i++) {
//            KeyedMessage message =
//                    new KeyedMessage(KafkaProperties.TEST_TOPIC, i + "", "Message" + i);
//            messages.add(message);
//            if (i % 100 == 0) {
//                producer.send(messages);
//                messages.clear();
//            }
//        }
//        producer.send(messages);
        for(int i = 0;i <= 10; i++) {
                log.info("trace:This is Message [" + i + "] from log4j producer .. ");
                log.info("This is Message [" + i + "] from log4j producer .. ");
        }
    }
}

注意:クラスのソースは、jarによって異なるクラスもあります.
4結果:
2017-05-21 10:41:09 INFO  ProducerDemo:44 % - trace:This is Message [5] from log4j producer ..

2017-05-21 10:41:09 INFO  ProducerDemo:45 % - This is Message [5] from log4j producer ..

2017-05-21 10:41:09 INFO  ProducerDemo:44 % - trace:This is Message [6] from log4j producer ..

2017-05-21 10:41:09 INFO  ProducerDemo:45 % - This is Message [6] from log4j producer ..

2017-05-21 10:41:09 INFO  ProducerDemo:44 % - trace:This is Message [7] from log4j producer ..

2017-05-21 10:41:09 INFO  ProducerDemo:45 % - This is Message [7] from log4j producer ..

2017-05-21 10:41:09 INFO  ProducerDemo:44 % - trace:This is Message [8] from log4j producer ..

2017-05-21 10:41:09 INFO  ProducerDemo:45 % - This is Message [8] from log4j producer ..

2017-05-21 10:41:09 INFO  ProducerDemo:44 % - trace:This is Message [9] from log4j producer ..

2017-05-21 10:41:09 INFO  ProducerDemo:45 % - This is Message [9] from log4j producer ..

2017-05-21 10:41:09 INFO  ProducerDemo:44 % - trace:This is Message [10] from log4j producer ..

2017-05-21 10:41:09 INFO  ProducerDemo:45 % - This is Message [10] from log4j producer ..

注記:最も面倒なのは、元のプロジェクトがlog 4 jを使用している可能性があり、jarの導入を再修正する必要がある可能性があります.