Kafka编程实践

Kafka 编程实践

搭建了一个Kafka集群之后,看怎么用Java程序编写去利用它。

参考Kafka文档( http://kafka.apache.org/documentation/),Kafka可以用在三个场景:
  1. Messaging System
  2. Storage System
  3. Stream Processing

我用Java实现了一个“ 生产者-消费者”的场景。打算用一个工程,通过参数的形式来分别启动“生产者”和“消费者”进程。

建一个Java+Maven工程,在pom.xml中加个包:

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka-clients</artifactId>
    <version>1.0.0</version>
</dependency>

新建三个类:Main、Producer、Consumer
  • Main,负责启动程序并解析参数,选择启动Producer还是Consumer;
  • Producer,负责生产一批量的消息到一个topic
  • Consumer,循环监听一个topic,有消息就打印


Producer类

消息:我设定消息是一个Json字符串,模拟得像样一点:
       private   String genJsonObject() {
             return   "{\"id\":\""   + java.util.UUID. randomUUID ().toString().substring(0, 13) +   "\""
                        +   ", \"value\":\""   +   random .nextFloat() +   "\""
                        +   ", \"timestamp\":\""   +   sdformat .format( new   java.util.Date()) +   "\""
                        +   "}" ;
      }

生产它们:

       public   void   produce(String   topic , Long   number ) {
            
             random   =   new   java.util.Random(System. currentTimeMillis ());
            
             for   ( int   i   = 0;   i   <   number ;   i ++) {
                  String   message   = genJsonObject();
                  String   key   = java.util.UUID. randomUUID ().toString().substring(0, 13);
                   producer .send( new   ProducerRecord<String, String>( topic ,   key ,   message ));
                  
                  System. out .println( "Sent: "   +   message );
                  
                   // 随机延时
                   try   {
                        Thread. sleep (1000 +   random .nextInt(5000) );
                  }   catch   (InterruptedException   e ) {
                         e .printStackTrace();
                  }
            }
            
            System. out .println( "finish producing." );
      }


Consumer类

获取:

       public   void   consume(String   topics ) {
            java.util.List<String>   topicList   =   new   java.util.ArrayList<String>();
            
             for   (String   t   :   topics .split( "," )) {
                   topicList .add( t );
            }
             consumer .subscribe( topicList );
             while   ( true ) {
                  ConsumerRecords<String, String>   records   =   consumer .poll(1);
                   for   (ConsumerRecord<String, String>   record   :   records )
                        System. out .printf( "offset = %d, key = %s, value = %s%n" ,   record .offset(),   record .key(),   record .value());
            }
      }

Main类

package   kafkademo;
public   class   Main {
       static   public   void   main(String   args []) {
             switch   ( args [0]) {
             case   "consumer" :
                   runAsConsumer ( args );
                   break ;
             case   "producer" :
                   runAsProducer ( args );
                   break ;
                  
             default :
                   usage ();
                   return ;
            }
      }
       private   static   void   usage() {
            System. out .println( "Usage:" );
            System. out .println( "    Program type:" );
            System. out .println( "        consumer    run as a consumer." );
            System. out .println( "        producer    run as a producer." );
            System. out .println();
            System. out .println( "    Options:" );
            System. out .println( "--server=host:port  kafka entry point." );
            System. out .println( "--number=number     number of message to produce." );
            System. out .println( " --topic=topic      topic of message to produce." );
            System. out .println( "--topics=topics     topics of message to fetch from as a consumers." );
      }
       private   static   void   runAsProducer(String   args []) {
            
            String   host   =   "localhost:9092" ;
            Long   number   = 1L;
            String   topic   =   "test-demo" ;
            java.util.Properties   props   =   new   java.util.Properties();
            
             for (String   arg   :   args ) {
                   if   ( arg .startsWith( "--server=" )) {
                         host   =   arg .split( "=" )[1];   // "49.4.67.131:9092");
                  }
                  
                   if   ( arg .startsWith( "--number=" )) {
                        String   numString   =   arg .split( "=" )[1];
                         if   ( "INFINITE" .equals( numString )) {
                               number   = Long. MAX_VALUE ;
                        }   else   {
                               number   = Long. parseLong ( numString );
                        }
                  }
                   if   ( arg .startsWith( "--topic=" )) {
                         topic   =   arg .split( "=" )[1];
                  }
            }
            
            Producer   producer   =   new   Producer();
            
             props .put( "bootstrap.servers" ,   host );
             producer .init( props );
             producer .produce( topic ,   number );
            
             producer .tearDown();
            
      }
       private   static   void   runAsConsumer(String   args []) {
            String   host   =   "localhost:9092" ;
            String   topics   =   "test-demo" ;
            java.util.Properties   props   =   new   java.util.Properties();
            
             for (String   arg   :   args ) {
                   if   ( arg .startsWith( "--server=" )) {
                         host   =   arg .split( "=" )[1];   // "49.4.67.131:9092");
                  }
                  
                   if   ( arg .startsWith( "--topics=" )) {
                         topics   =   arg .split( "=" )[1];
                  }
            }
            
            Consumer   consumer   =   new   Consumer();
            
             props .put( "bootstrap.servers" ,   host );
             consumer .init( props );
             consumer . consume ( topics );
      }
}


运行起来

生产:


消费:

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值