­

My Quotes


When U were born , you cried and the world rejoiced
Live U'r life in such a way that when you go
THE WORLD SHOULD CRY





Free users please don't remove our link. Get the code again.

Friday, March 3, 2017

Kafka Multiple Topic

Producer and multiple Topics

    Download a recent stable version of Apache Kafka
  1. Untar the package
  2. Enter into Kafka directory
  3. Start Zookeeper Server
  4. bin/zookeeper-server-start.sh config/zookeeper.properties
  5. In a different terminal start Kafka Server
  6. bin/kafka-server-start.sh config/server.properties
  7. Create a topic test (if not exists)
  8. bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic example2
  9. Create a topic test1 (if not exists)
  10. bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic example3
  11. Start consumer on topic test
  12. bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic example2 --from-beginning
  13. Start consumer on topic test1
  14. bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic example3 --from-beginning
  15. Run mvn clean compile exec:exec
  16. You should see the message in the consumer terminal.
    Here are the project sources
  1. Project POM.xml
    
    
      4.0.0
     
      ../pom.xml
      1.0.0-SNAPSHOT
      com.example.kafka
      kafka-examples
     
      kafka-producer-multiple-topics
      kafka-producer-multiple-topics
    
     
      
       
        org.codehaus.mojo
        exec-maven-plugin
        
         
          
           exec
          
         
        
        
         java
         
          -classpath
          
          com.example.kafka.ProducerMultipleTopic
         
        
       
      
     
    
    
    

  2. Producer Java File
    package com.example.kafka;
    
    import java.util.Properties;
    
    import org.apache.kafka.clients.producer.KafkaProducer;
    import org.apache.kafka.clients.producer.ProducerRecord;
    import org.apache.kafka.common.serialization.StringSerializer;
    
    public class ProducerMultipleTopic {
    
        public static void main(String[] args) {
    
            Properties props = new Properties();
            props.put("bootstrap.servers", "localhost:9092");
            props.put("key.serializer", StringSerializer.class.getName());
            props.put("value.serializer", StringSerializer.class.getName());
            
            KafkaProducer prod = new KafkaProducer(props);
    
            ProducerRecord data1 = new ProducerRecord("example2", "example2");
            ProducerRecord data2 = new ProducerRecord("example3", "example3");
    
            prod.send(data1);
            prod.send(data2);
    
            prod.close();
        }
    }
    
  3. Logger file
    # Root logger option
    log4j.rootLogger=INFO, stdout
    
    # Direct log messages to stdout
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
    
    


  4. This is the PARENT POM
    
    
     4.0.0
     com.example.kafka
     kafka-examples
     1.0.0-SNAPSHOT
     pom
    
     
      
       org.apache.kafka
       kafka_2.10
       0.9.0.1
      
      
       org.apache.kafka
       kafka-clients
       0.9.0.1
      
      
       log4j
       log4j
       1.2.17
      
     
    
     
      UTF-8
     
    
     
      
       
        
         org.apache.maven.plugins
         maven-compiler-plugin
         3.2
         
          1.7
          1.7
         
        
        
         org.codehaus.mojo
         exec-maven-plugin
         1.3.2
        
       
      
     
     
       kafka-producer-multiple-topics
      
    
    
    

No comments :