kafka stream的简单使用,这里是官方文档上面的例子。

kafka的简单使用

一、启动Kafka server

  1. huhx@gohuhx:~/server/kafka_2.11-1.1.0$ bin/zookeeper-server-start.sh config/zookeeper.properties
  2. huhx@gohuhx:~/server/kafka_2.11-1.1.0$ bin/kafka-server-start.sh config/server.properties

二、创建两个主题streams-plaintext-input与streams-wordcount-output

  1. huhx@gohuhx:~/server/kafka_2.11-1.1.0$ bin/kafka-topics.sh --create \
  2. --zookeeper localhost: \
  3. --replication-factor \
  4. --partitions \
  5. --topic streams-plaintext-input
  6.  
  7. huhx@gohuhx:~/server/kafka_2.11-1.1.0$ bin/kafka-topics.sh --create \
  8. --zookeeper localhost: \
  9. --replication-factor \
  10. --partitions \
  11. --topic streams-wordcount-output \
  12. --config cleanup.policy=compact

可以使用bin/kafka-topics.sh --zookeeper localhost:2181 --describe查看创建的主题描述。

三、开启kafka里面自带的WordCount程序

  1. huhx@gohuhx:~/server/kafka_2.11-1.1.0$ bin/kafka-run-class.sh org.apache.kafka.streams.examples.wordcount.WordCountDemo

启动console producer去写入一些record,启动console consumer去接受处理之后的消息。

  1. huhx@gohuhx:~/server/kafka_2.11-1.1.0$ bin/kafka-console-producer.sh --broker-list localhost: --topic streams-plaintext-input
  2.  
  3. huhx@gohuhx:~/server/kafka_2.11-1.1.0$ bin/kafka-console-consumer.sh --bootstrap-server localhost: \
  4. --topic streams-wordcount-output \
  5. --from-beginning \
  6. --formatter kafka.tools.DefaultMessageFormatter \
  7. --property print.key=true \
  8. --property print.value=true \
  9. --property key.deserializer=org.apache.kafka.common.serialization.StringDeserializer \
  10. --property value.deserializer=org.apache.kafka.common.serialization.LongDeserializer

向kafka-console-producer.sh的窗口输入一些数据

  1. all streams lead to kafka

可以在kafka-console-consumer.sh的窗口里面看到如下的输出

  1. all
  2. streams
  3. lead
  4. to
  5. kafka

继续在producer中输入数据,可以在consumer的窗口看到相应的输出。程序的结束可以按键Ctrl-C。

四、一个关于pipe的例子

  1. package com.linux.huhx.stream;
  2.  
  3. import org.apache.kafka.common.serialization.Serdes;
  4. import org.apache.kafka.streams.KafkaStreams;
  5. import org.apache.kafka.streams.StreamsBuilder;
  6. import org.apache.kafka.streams.StreamsConfig;
  7. import org.apache.kafka.streams.Topology;
  8.  
  9. import java.util.Properties;
  10. import java.util.concurrent.CountDownLatch;
  11.  
  12. /**
  13. * user: huxhu
  14. * date: 2018/8/12 8:59 PM
  15. **/
  16. public class PipeStream {
  17. public static void main(String[] args) throws Exception {
  18. Properties props = new Properties();
  19. props.put(StreamsConfig.APPLICATION_ID_CONFIG, "streams-pipe");
  20. props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.1.101:9092");
  21. props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
  22. props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());
  23.  
  24. final StreamsBuilder builder = new StreamsBuilder();
  25.  
  26. builder.stream("streams-plaintext-input").to("streams-pipe-output");
  27.  
  28. final Topology topology = builder.build();
  29.  
  30. final KafkaStreams streams = new KafkaStreams(topology, props);
  31. final CountDownLatch latch = new CountDownLatch(1);
  32.  
  33. // attach shutdown handler to catch control-c
  34. Runtime.getRuntime().addShutdownHook(new Thread("streams-shutdown-hook") {
  35. @Override
  36. public void run() {
  37. streams.close();
  38. latch.countDown();
  39. }
  40. });
  41.  
  42. try {
  43. streams.start();
  44. latch.await();
  45. } catch (Throwable e) {
  46. System.exit(1);
  47. }
  48. System.exit(0);
  49. }
  50. }

运行以下命令执行程序

  1. mvn clean package
  2. mvn exec:java -Dexec.mainClass=com.linux.huhx.stream.PipeStream

看到以下的输出,说明正确启动了程序。注意程序没有结束,可以按ctrl+C终止程序。

  1. [WARNING]
  2. [WARNING] Some problems were encountered while building the effective settings
  3. [WARNING] Unrecognised tag: 'snapshotPolicy' (position: START_TAG seen ...</layout>\n <snapshotPolicy>... @267:27) @ /usr/local/Cellar/maven/3.5.4/libexec/conf/settings.xml, line 267, column 27
  4. [WARNING] Unrecognised tag: 'snapshotPolicy' (position: START_TAG seen ...\n <snapshotPolicy>... @203:27) @ /Users/huxhu/.m2/settings.xml, line 203, column 27
  5. [WARNING]
  6. [INFO] Scanning for projects...
  7. [WARNING]
  8. [WARNING] Some problems were encountered while building the effective model for com.linux.huhx:KafkaLearn:jar:1.0-SNAPSHOT
  9. [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 42, column 21
  10. [WARNING]
  11. [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
  12. [WARNING]
  13. [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
  14. [WARNING]
  15. [INFO]
  16. [INFO] ---------------------< com.linux.huhx:KafkaLearn >----------------------
  17. [INFO] Building KafkaLearn 1.0-SNAPSHOT
  18. [INFO] --------------------------------[ jar ]---------------------------------
  19. [INFO]
  20. [INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ KafkaLearn ---

我们需要订阅上面声明的主题streams-pipe-output。

  1. huhx@gohuhx:~/server/kafka_2.11-1.1.0$ bin/kafka-console-consumer.sh --bootstrap-server localhost: --topic streams-pipe-output --from-beginning

在streams-plaintext-input窗口输入数据,可以在streams-pipe-output窗口看到相应的输出。

五、一个关于LineSplit的例子

  1. package com.linux.huhx.stream;
  2.  
  3. import org.apache.kafka.common.serialization.Serdes;
  4. import org.apache.kafka.streams.KafkaStreams;
  5. import org.apache.kafka.streams.StreamsBuilder;
  6. import org.apache.kafka.streams.StreamsConfig;
  7. import org.apache.kafka.streams.Topology;
  8. import org.apache.kafka.streams.kstream.KStream;
  9.  
  10. import java.util.Arrays;
  11. import java.util.Properties;
  12. import java.util.concurrent.CountDownLatch;
  13.  
  14. public class LineSplit {
  15.  
  16. public static void main(String[] args) {
  17. Properties props = new Properties();
  18. props.put(StreamsConfig.APPLICATION_ID_CONFIG, "streams-linesplit");
  19. props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.1.101:9092");
  20. props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
  21. props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());
  22.  
  23. final StreamsBuilder builder = new StreamsBuilder();
  24.  
  25. KStream<String, String> source = builder.stream("streams-plaintext-input");
  26. source.flatMapValues(value -> Arrays.asList(value.split("\\W+"))).to("streams-linesplit-output");
  27.  
  28. final Topology topology = builder.build();
  29. final KafkaStreams streams = new KafkaStreams(topology, props);
  30. final CountDownLatch latch = new CountDownLatch(1);
  31.  
  32. // attach shutdown handler to catch control-c
  33. Runtime.getRuntime().addShutdownHook(new Thread("streams-shutdown-hook") {
  34. @Override
  35. public void run() {
  36. streams.close();
  37. latch.countDown();
  38. }
  39. });
  40.  
  41. try {
  42. streams.start();
  43. latch.await();
  44. } catch (Throwable e) {
  45. System.exit(1);
  46. }
  47. System.exit(0);
  48. }
  49. }

在idea中直接运行上述程序,然后订阅上面声明的主题

  1. huhx@gohuhx:~/server/kafka_2.11-1.1.0$ bin/kafka-console-consumer.sh --bootstrap-server localhost: --topic streams-linesplit-output --from-beginning

在producer和consumer的窗口,可以看到对应的操作如下:

友情链接

kafka---->kafka stream的使用(一)的更多相关文章

  1. 1.1 Introduction中 Kafka for Stream Processing官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Kafka for Stream Processing kafka的流处理 It i ...

  2. Kafka和Stream架构的使用

    Kafka的单节点运行 启动服务 Kafka 使用 ZooKeeper 如果你还没有 ZooKeeper 服务器,你需要先启动一个 ZooKeeper 服务器. 您可以通过与 kafka 打包在一起的 ...

  3. [Kafka] - Kafka Java Consumer实现(二)

    Kafka提供了两种Consumer API,分别是:High Level Consumer API 和 Lower Level Consumer API(Simple Consumer API) H ...

  4. [Kafka] - Kafka Java Consumer实现(一)

    Kafka提供了两种Consumer API,分别是:High Level Consumer API 和 Lower Level Consumer API(Simple Consumer API) H ...

  5. [Spark][kafka]kafka 生产者,消费者 互动例子

    [Spark][kafka]kafka 生产者,消费者 互动例子 # pwd/usr/local/kafka_2.11-0.10.0.1/bin 创建topic:# ./kafka-topics.sh ...

  6. Zookeeper与Kafka Kafka

    Zookeeper与Kafka Kafka Kafka SocketServer是基于Java NIO开发的,采用了Reactor的模式(已被大量实践证明非常高效,在Netty和Mina中广泛使用). ...

  7. Kafka启动遇到ERROR Exiting Kafka due to fatal exception (kafka.Kafka$)

    ------------恢复内容开始------------ Kafka启动遇到ERROR Exiting Kafka due to fatal exception (kafka.Kafka$) 解决 ...

  8. 【Kafka】Stream API

    Stream API Kafka官方文档给了基本格式 http://kafka.apachecn.org/10/javadoc/index.html?org/apache/kafka/streams/ ...

  9. [Big Data - Kafka] Kafka剖析(一):Kafka背景及架构介绍

    Kafka是由LinkedIn开发的一个分布式的消息系统,使用Scala编写,它以可水平扩展和高吞吐率而被广泛使用.目前越来越多的开源分布式处理系统如Cloudera.Apache Storm.Spa ...

  10. [Kafka] - Kafka基本概念介绍

    Kafka官方介绍:Kafka是一个分布式的流处理平台(0.10.x版本),在kafka0.8.x版本的时候,kafka主要是作为一个分布式的.可分区的.具有副本数的日志服务系统(Kafka™ is ...

随机推荐

  1. this-11.1-笔记

    1. 基本数据类型:string   undefined   null  boolean  number 引用数据类型  Object  array  function 1.2  二者的区别 基本数据 ...

  2. elment ui 图片上传遇到的一些问题

    图片上传返回200,message显示请上传图片 注意上图中的name字段要和服务器接受的name相同,这里我们是imgfile,默认name不是这个,所以要在el-upload组件上设置name属性 ...

  3. jQuery 学习02——效果:隐藏/显示、淡入淡出、滑动、动画、停止动画、Callback、链

    jQuery 效果- 隐藏hide()和显示show() 语法: $(selector).hide(speed,callback);$(selector).show(speed,callback); ...

  4. iOS链接库的冲突

    最近在打包的时候,遇到一个坑.特此记录一下 起因是发现 Unity 5.4 版本,使用c#写的下载,下载速度无法突破 2M/s,同样的网络,后来横向对比使用原来 Cocos2d 开始的游戏,可以达到 ...

  5. 深度残差网络(DRN)ResNet网络原理

    一说起“深度学习”,自然就联想到它非常显著的特点“深.深.深”(重要的事说三遍),通过很深层次的网络实现准确率非常高的图像识别.语音识别等能力.因此,我们自然很容易就想到:深的网络一般会比浅的网络效果 ...

  6. jquery的deferred使用详解

    原文:hhtps://www.cnblogs.com/shijingjing07/p/6403450.html -------------------------------------------- ...

  7. maven scope 范围讲解

    解决办法: <dependency>     <groupId>javax.servlet</groupId>     <artifactId>java ...

  8. 有钱人都用iphone?

    身边的朋友用iphone, 大家都会调侃真有钱. 大数据显示, 人家是真的有钱. 看看来自腾讯移动分析数据 你玩过抖音就知道, 拍视频, 传视频的过的看着都挺滋润的. 反观用安卓的用户, 前20个应用 ...

  9. 同步IO、异步IO、阻塞IO、非阻塞IO之间的联系与区别

    POSIX 同步IO.异步IO.阻塞IO.非阻塞IO,这几个词常见于各种各样的与网络相关的文章之中,往往不同上下文中它们的意思是不一样的,以致于我在很长一段时间对此感到困惑,所以想写一篇文章整理一下. ...

  10. Swift Enum 枚举

    前言 枚举是一种自定义的数据类型,在 Swift 中枚举类型拥有相当高的自由度.在 Swift 语言中枚举是一级类型,它拥有在其他语言中只有类才拥有的一些特性,比如实例方法,实例构造器等. 枚举声明的 ...