kafka Streams 应用程序:Pipe
目录
前言
因为学校布置了要"编写kafka stream 应用程序:pipe"的实训,然后网上找到的相关文章的介绍不是特别详细,而且要踩点坑,所以我顺路写篇文章水水。(ps:希望我们大数据5班的同学能看到并且用得上)
配置列表:
软件 |
---|
kafka_2.11-2.0.0 |
zookeeper-3.4.10 |
intellij IDEA |
1、maven配置
pom.xml配置如下(仅供参考):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>KafkaProject</artifactId>
<version>1.0-SNAPSHOT</version>
<repositories>
<repository>
<id>central</id>
<url>http://maven.aliyun.com/nexus/content/groups/public//</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
<repository>
<id>maven_central</id>
<name>Maven Central</name>
<url>https://repo.maven.apache.org/maven2/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、主题的创建
首先当然是启动kafka了,这里略过
创建streams-plaintext-input
bin/kafka-topics.sh --zookeeper localhost:2181 --create --topic streams-plaintext-input --partitions 2 --replication-factor 1
创建streams-pipe-output
bin/kafka-topics.sh --zookeeper localhost:2181 --create --topic streams-pipe-output --partitions 2 --replication-factor 1
3、主程序编写
此处跟网上能找到的例程大同小异,不过要注意props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "hadoop01:9092");
这一行里的hadoop01要改成自己kafka虚拟机的地址
代码如下:
package com.gripen.pipe;
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;
public class Pipe {
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, "hadoop01: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);
}
}
如果按照前面的步骤创建好了两个主题,那么就可以运行这个程序了。
没毛病的话输出如图所示:
4、测试输入输出
保持程序的运行,同时在虚拟机内创建两个终端并cd到kafka文件目录下,分别输入以下两条指令:
启动控制台生产者
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic streams-plaintext-input
启动控制台消费者
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 \
--topic streams-pipe-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.StringDeserializer
此时就可以在控制台生产者处输入测试消息,示例如下:
此时,如果程序没有错的话应该可以在控制台消费者处看到同步的消息输出,如图:
遇到的坑
在许多网上的例子里,启动控制台生产者使用的代码是bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic streams-plaintext-input
,但是在我使用的时候报错
后来经过查找,发现是版本问题:
在2.5.0版本之前只支持 --broker-list
在2.5.0版本之后支持 --bootstrap-server
由于老师给的版本小于2.5.0,所以将启动控制台生产者代码改为bin/kafka-console-producer.sh --broker-list localhost:9092 --topic streams-plaintext-input
即可。