kafka---->kafka stream的使用(一)
kafka stream的简单使用,这里是官方文档上面的例子。
kafka的简单使用
一、启动Kafka server
huhx@gohuhx:~/server/kafka_2.11-1.1.0$ bin/zookeeper-server-start.sh config/zookeeper.properties
huhx@gohuhx:~/server/kafka_2.11-1.1.0$ bin/kafka-server-start.sh config/server.properties
二、创建两个主题streams-plaintext-input与streams-wordcount-output
huhx@gohuhx:~/server/kafka_2.11-1.1.0$ bin/kafka-topics.sh --create \
--zookeeper localhost: \
--replication-factor \
--partitions \
--topic streams-plaintext-input huhx@gohuhx:~/server/kafka_2.11-1.1.0$ bin/kafka-topics.sh --create \
--zookeeper localhost: \
--replication-factor \
--partitions \
--topic streams-wordcount-output \
--config cleanup.policy=compact
可以使用bin/kafka-topics.sh --zookeeper localhost:2181 --describe查看创建的主题描述。
三、开启kafka里面自带的WordCount程序
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去接受处理之后的消息。
huhx@gohuhx:~/server/kafka_2.11-1.1.0$ bin/kafka-console-producer.sh --broker-list localhost: --topic streams-plaintext-input huhx@gohuhx:~/server/kafka_2.11-1.1.0$ bin/kafka-console-consumer.sh --bootstrap-server localhost: \
--topic streams-wordcount-output \
--from-beginning \
--formatter kafka.tools.DefaultMessageFormatter \
--property print.key=true \
--property print.value=true \
--property key.deserializer=org.apache.kafka.common.serialization.StringDeserializer \
--property value.deserializer=org.apache.kafka.common.serialization.LongDeserializer
向kafka-console-producer.sh的窗口输入一些数据
all streams lead to kafka
可以在kafka-console-consumer.sh的窗口里面看到如下的输出
all
streams
lead
to
kafka
继续在producer中输入数据,可以在consumer的窗口看到相应的输出。程序的结束可以按键Ctrl-C。
四、一个关于pipe的例子
package com.linux.huhx.stream; import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.Topology; import java.util.Properties;
import java.util.concurrent.CountDownLatch; /**
* user: huxhu
* date: 2018/8/12 8:59 PM
**/
public class PipeStream {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put(StreamsConfig.APPLICATION_ID_CONFIG, "streams-pipe");
props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.1.101:9092");
props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass()); final StreamsBuilder builder = new StreamsBuilder(); builder.stream("streams-plaintext-input").to("streams-pipe-output"); final Topology topology = builder.build(); final KafkaStreams streams = new KafkaStreams(topology, props);
final CountDownLatch latch = new CountDownLatch(1); // attach shutdown handler to catch control-c
Runtime.getRuntime().addShutdownHook(new Thread("streams-shutdown-hook") {
@Override
public void run() {
streams.close();
latch.countDown();
}
}); try {
streams.start();
latch.await();
} catch (Throwable e) {
System.exit(1);
}
System.exit(0);
}
}
运行以下命令执行程序
mvn clean package
mvn exec:java -Dexec.mainClass=com.linux.huhx.stream.PipeStream
看到以下的输出,说明正确启动了程序。注意程序没有结束,可以按ctrl+C终止程序。
[WARNING]
[WARNING] Some problems were encountered while building the effective settings
[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
[WARNING] Unrecognised tag: 'snapshotPolicy' (position: START_TAG seen ...\n <snapshotPolicy>... @203:27) @ /Users/huxhu/.m2/settings.xml, line 203, column 27
[WARNING]
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.linux.huhx:KafkaLearn:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 42, column 21
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ---------------------< com.linux.huhx:KafkaLearn >----------------------
[INFO] Building KafkaLearn 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ KafkaLearn ---
我们需要订阅上面声明的主题streams-pipe-output。
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的例子
package com.linux.huhx.stream; import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.streams.kstream.KStream; import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.CountDownLatch; public class LineSplit { public static void main(String[] args) {
Properties props = new Properties();
props.put(StreamsConfig.APPLICATION_ID_CONFIG, "streams-linesplit");
props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.1.101:9092");
props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass()); final StreamsBuilder builder = new StreamsBuilder(); KStream<String, String> source = builder.stream("streams-plaintext-input");
source.flatMapValues(value -> Arrays.asList(value.split("\\W+"))).to("streams-linesplit-output"); final Topology topology = builder.build();
final KafkaStreams streams = new KafkaStreams(topology, props);
final CountDownLatch latch = new CountDownLatch(1); // attach shutdown handler to catch control-c
Runtime.getRuntime().addShutdownHook(new Thread("streams-shutdown-hook") {
@Override
public void run() {
streams.close();
latch.countDown();
}
}); try {
streams.start();
latch.await();
} catch (Throwable e) {
System.exit(1);
}
System.exit(0);
}
}
在idea中直接运行上述程序,然后订阅上面声明的主题
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 Introduction中 Kafka for Stream Processing官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Kafka for Stream Processing kafka的流处理 It i ...
- Kafka和Stream架构的使用
Kafka的单节点运行 启动服务 Kafka 使用 ZooKeeper 如果你还没有 ZooKeeper 服务器,你需要先启动一个 ZooKeeper 服务器. 您可以通过与 kafka 打包在一起的 ...
- [Kafka] - Kafka Java Consumer实现(二)
Kafka提供了两种Consumer API,分别是:High Level Consumer API 和 Lower Level Consumer API(Simple Consumer API) H ...
- [Kafka] - Kafka Java Consumer实现(一)
Kafka提供了两种Consumer API,分别是:High Level Consumer API 和 Lower Level Consumer API(Simple Consumer API) H ...
- [Spark][kafka]kafka 生产者,消费者 互动例子
[Spark][kafka]kafka 生产者,消费者 互动例子 # pwd/usr/local/kafka_2.11-0.10.0.1/bin 创建topic:# ./kafka-topics.sh ...
- Zookeeper与Kafka Kafka
Zookeeper与Kafka Kafka Kafka SocketServer是基于Java NIO开发的,采用了Reactor的模式(已被大量实践证明非常高效,在Netty和Mina中广泛使用). ...
- Kafka启动遇到ERROR Exiting Kafka due to fatal exception (kafka.Kafka$)
------------恢复内容开始------------ Kafka启动遇到ERROR Exiting Kafka due to fatal exception (kafka.Kafka$) 解决 ...
- 【Kafka】Stream API
Stream API Kafka官方文档给了基本格式 http://kafka.apachecn.org/10/javadoc/index.html?org/apache/kafka/streams/ ...
- [Big Data - Kafka] Kafka剖析(一):Kafka背景及架构介绍
Kafka是由LinkedIn开发的一个分布式的消息系统,使用Scala编写,它以可水平扩展和高吞吐率而被广泛使用.目前越来越多的开源分布式处理系统如Cloudera.Apache Storm.Spa ...
- [Kafka] - Kafka基本概念介绍
Kafka官方介绍:Kafka是一个分布式的流处理平台(0.10.x版本),在kafka0.8.x版本的时候,kafka主要是作为一个分布式的.可分区的.具有副本数的日志服务系统(Kafka™ is ...
随机推荐
- LinkedList源码分析和实例应用
1. LinkedList介绍 LinkedList是继承于AbstractSequentialList抽象类,它也可以被当作堆栈.队列或者双端队列使用. LinkedList实现了Deque接口,即 ...
- mysql 设置、更改、找回密码
1,新装mysql,添加密码 mysqladmin -u root password NEWPASSWORD 2,记得旧密码,改密码 mysqladmin -u root -p'OLDPASSWORD ...
- CSS_对齐
2016-10-25 <css入门经典>第15章 1.text-align属性: 块属性内部的文本对齐方式.该属性只对块盒子有意义,内联盒子的内容没有对齐方式.(注意:只是盒子内部的内容对 ...
- js中字符串的方法
js String对象中常用方法小结,需要的朋友可以参考下: 1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码. strObj.charCodeAt(index) 说明: ...
- 如何修改IE浏览器的User-Agent用户代理字符串信息
每款浏览器都有一个专属的 User-Agent 字符串信息, 通过 User-Agent 网站可以检测用户所使用的浏览器版本.某些网站为了让用户获得更好的浏览体验,通过检测用户的浏览器版本,以确认用户 ...
- python测试开发django-52.xadmin添加自定义的javascript(get_media)
前言 我想使用xadmin在列表页每一行元素添加一个按钮,当点击这个按钮的时候,能发个请求出去,后台执行相关功能.于是想到添加自定义的javascript脚本能实现. 在/stackoverflow上 ...
- CSS魔法堂:display:none与visibility:hidden的恩怨情仇
前言 还记得面试时被问起"请说说display:none和visibility:hidden的区别"吗?是不是回答完display:none不占用原来的位置,而visibilit ...
- libreoffice python 操作word及excel文档
1.开始.关闭libreoffice服务: 开始之前同步字体文件时间,是因为创建soffice服务时,服务会检查所需加载的文件的时间,如果其认为时间不符,则其可能会重新加载,耗时较长,因此需事先统一时 ...
- MinFilter(MaxFilter)快速算法C++实现
目录 1.算法简述 1.1.MinFilter(MaxFilter) 算法简述 1.2.MinFilter(MaxFilter) 快速算法简述 2.实现代码 2.1.MinFilterOneRow 单 ...
- (原)tensorflow使用eager在mnist上训练的简单例子
转载请注明出处: https://www.cnblogs.com/darkknightzh/p/9989586.html 代码网址: https://github.com/darkknightzh/t ...