/*
用于实现将一个topic的数据读取后从转换成大写,再将结果写入另一个topic
*/
public class StreamAPI {
public static void main(String[] args) {
Properties props = new Properties();
//设置程序的唯一标识
props.put(StreamsConfig.APPLICATION_ID_CONFIG, "wordcount-application");
//设置kafka集群
props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "node01:9092,node02:9092,node03:9092");
//设置序列化与反序列化
props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());
//实例一个计算逻辑
StreamsBuilder streamsBuilder = new StreamsBuilder();
//设置计算逻辑 stream:读取 to:写入
streamsBuilder.stream("18BD34").mapValues(line->line.toString().toUpperCase()).to("18BD34-1");
//构建Topology对象(拓扑,流程)
final Topology topology = streamsBuilder.build();
//实例 kafka流
KafkaStreams streams = new KafkaStreams(topology, props);
//启动流计算
streams.start();
}
}
Kafka中steamAPi操作
最新推荐文章于 2024-06-29 07:47:50 发布