Kafka:实时计算-实时生产-适时传递-实时存储--实时展现

需求

使用StreamAPI获取student这个topic当中的数据,然后将数据全部转为大写,写入到teacher这个topic当中去

第一步:创建两个Topic

bin/kafka-topics.sh --create --zookeeper node01:2181,node02:2181,node03:2181  --replication-factor 2 --partitions 3 --topic student
-------------------------------------------------------------
bin/kafka-topics.sh --create --zookeeper node01:2181,node02:2181,node03:2181  --replication-factor 2 --partitions 3 --topic teacher

第二步:创建StreamApi(使用IDEA)

public class SteamApi {
    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");
            //设置序列化与反序列化
            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();
            //设置计算逻辑
            streamsBuilder.stream("student").mapValues(line->line.toString().toUpperCase()).to("teacher");
            //构建Topology对象(拓扑,流程)
            final Topology topology = streamsBuilder.build();
            //实例 kafka流
            KafkaStreams streams = new KafkaStreams(topology, props);
            //启动流计算
            streams.start();


    }
}

第三步:创建消费者Consumer

bin/kafka-console-consumer.sh --zookeeper node01:2181,node02:2181,node03:2181 --topic student --from-beginning
--------------------------------------------------------------------------------------------
bin/kafka-console-consumer.sh --zookeeper node01:2181,node02:2181,node03:2181 --topic teacher --from-beginning

第四步:创建生产者Producer

方式一:
使用代码的形式:

public class Producer {
    public static void main(String[] args) {

        //1、配置kafka集群
        Properties props = new Properties();

        //kafka服务器地址
        props.put("bootstrap.servers", "node01:9092,node02:9092,node03:9092");
        //消息确认机制
        props.put("acks", "all");
        //重试机制
        props.put("retries", 0);
        //批量发送的大小
        props.put("batch.size", 16384);
        //消息延迟
        props.put("linger.ms", 1);
        //批量的缓冲区大小
        props.put("buffer.memory", 33554432);
        //kafka数据中key  value的序列化
        props.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer");

        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(props);
        for (int i = 0; i < 10; i++) {
            ProducerRecord record = new ProducerRecord("student", "bbbb_" + i);
            kafkaProducer.send(record);
        }
        kafkaProducer.close();
    }
}

结果:
Kafka:实时计算-实时生产-适时传递-实时存储--实时展现_第1张图片
方式二:
使用Linux窗口命令的形式:

bin/kafka-console-producer.sh  --broker-list node01:9092,node02:9092,node03:9092  --topic  student

Kafka:实时计算-实时生产-适时传递-实时存储--实时展现_第2张图片

你可能感兴趣的:(Kafka)