• kafka scala API


    scala kafka api test

    依赖:

      <dependency>
                <groupId>org.apache.kafka</groupId>
                <artifactId>kafka_2.11</artifactId>
                <version>1.1.0</version>
            </dependency>

     base KafkaAPI  test

    import java.util
    import java.util.{Collections, Properties}
    import kafka.message.MessageAndMetadata
    import org.apache.kafka.clients.consumer.{ConsumerRecord, ConsumerRecords, KafkaConsumer}
    import org.apache.kafka.clients.consumer.internals.NoOpConsumerRebalanceListener
    import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord}
    import org.junit.Test
    import scala.actors.threadpool.{ExecutorService, Executors}
    import scala.collection.mutable
    
    object KakfaDemo {
      def main(args: Array[String]): Unit =
      {
        val properties = new Properties
        properties.put("bootstrap.servers","192.168.3.70:9092")
        properties.put("acks","0")
        properties.put("retries","3")
        properties.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer")
        properties.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer")
        //create new producer
    //    val producer: Producer[String, String] = new Producer[String,String](kafkaConfig)
         val producer = new KafkaProducer[String,String](properties)
        for(i <- 1 to 100)
        {
          var msg=s"${i}  this is test"
    //      producer.send(new KeyedMessage[String,String]("test1",msg))
          producer.send(new ProducerRecord[String,String]("test1",msg))
        }
    
      }
    
    }
    
    
    object  KafkaTest{
    
      def main(args: Array[String]): Unit = {
        val props = new Properties();
        props.put("bootstrap.servers", "192.168.3.70:9092")
        props.put("group.id", "test123")
        props.put("enable.auto.commit", "true")
        props.put("auto.offset.reset", "earliest")
        props.put("auto.commit.interval.ms", "1000");
        props.put("session.timeout.ms", "30000");
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
    
       val consumer = new KafkaConsumer[String, String](props)
        consumer.subscribe(Collections.singletonList("test1"))
        while(true)
        {
          val records = consumer.poll(1000);
          val record = records.iterator()
          while (record.hasNext)
          {
            val msg = record.next()
            System.out.println("offset=" + msg.offset() + ",--key=" + msg.key() + ",--value=" + msg.value())
            print("offset=" + msg.offset() + ",--key=" + msg.key() + ",--value=" + msg.value())
          }
          }
        }
    }
  • 相关阅读:
    newcoder 筱玛的迷阵探险(搜索 + 01字典树)题解
    str&repr的使用&format模板的自定义
    内置函数的补充与getattrebuit & item系列
    python几种常用模块
    面向对象的反射&动态导入模块
    面向对象的封装&定制数据类型
    面向对象的多态
    面向对象的继承
    面向对象的属性与方法
    面向对象的属性及类的增删改查
  • 原文地址:https://www.cnblogs.com/lshan/p/13984327.html
Copyright © 2020-2023  润新知