Code Monkey home page Code Monkey logo

kafka-'s Introduction

kafka-

Kafka- [Java]

Producers:

Step 1:

 //create properties
    private Properties createProperties(){
        Properties properties = new Properties();
        properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,constants.BOOTSTRAP_SERVER);
        properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        return properties;
    }

Step 2:

// Create the producers
    public KafkaProducer<String,String> createProducers(){
        return new KafkaProducer<String, String>(createProperties());
    }

Step 3:

//create record
    public ProducerRecord<String, String> createRecord(){
        return new ProducerRecord<String, String>("first_topic","hello sumit");
    }
}

Step 4:

Send msg
producer.send(producersElements.createRecord());
Note: Here used three properties which are required there are more properties see link below

https://kafka.apache.org/documentation/#producerconfig

Check the msg recived or consumed
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic first_topic --group first_group

Consumers

Step 1:

 // Create the consumers
    public KafkaConsumer<String,String> createConsumer(){
        return new KafkaConsumer<>(createProperties());
    }

Step 2:

 //create properties for consumer
    private Properties createProperties(){
        System.out.println("Create Properties");
        Properties properties = new Properties();
        properties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, constants.BOOTSTRAP_SERVER);
        properties.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        properties.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        properties.setProperty(ConsumerConfig.GROUP_ID_CONFIG,constants.GROUP_ID);
        properties.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,constants.OFFSET_LATEST);
        return properties;
    }

Step 3:

  //subscribe single topic only
    public void subscribeSingleConsumer(){
        createConsumer().subscribe(Collections.singleton(constants.TOPIC_NAME));
    }
    
     //subscribe single topic only
        public void subscribeMultipleConsumer(){
            createConsumer().subscribe(Arrays.asList(constants.TOPIC_NAME));
        }

Step 4:

  // Get records
 ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
Note: Here used three properties which are required there are more properties see link below

https://kafka.apache.org/documentation/#consumerconfig

Twitter-

kafka-'s People

Contributors

sxerox007 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.