package org.ts.ddcs.kafkaStream;


import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.streams.state.KeyValueStore;
import org.apache.kafka.streams.state.StoreBuilder;
import org.apache.kafka.streams.state.Stores;
import org.ts.ddcs.kafkaStream.source.DataStreamSourceMapProcessor;
import org.ts.ddcs.kafkaStream.source.DataStreamSourceProcessor;
import org.ts.ddcs.log.LogHelper;

import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;


/**
 * @author dylan
 */
public class KafkaStreamSourceTopology implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {

        try {

            Properties props = new Properties();
            props.put(StreamsConfig.APPLICATION_ID_CONFIG, "kafka-stream-source-topology");
            props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "172.1.0.238:9092");
            props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
            props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());
            props.put(StreamsConfig.NUM_STREAM_THREADS_CONFIG, 1);
            props.put(StreamsConfig.STATE_DIR_CONFIG, "D:\\tmp\\kafka\\source");

            //数据流拓扑结构
            Topology topology = new Topology();
            //数据源
            topology.addSource("dataStreamSource", "dcs.power01.1","dcs.power01.2");

            //数据处理器
            topology.addProcessor("dataStreamSourceProcessor", DataStreamSourceProcessor::new, "dataStreamSource");

            //数据处理器
            topology.addProcessor("dataStreamSourceMapProcessor", DataStreamSourceMapProcessor::new, "dataStreamSourceProcessor");


            //设置数据缓存
//            StoreBuilder<KeyValueStore<String, String>> blockStoreSupplier = Stores.keyValueStoreBuilder(
//                    Stores.inMemoryKeyValueStore("blockStoreCache"),
//                    Serdes.String(),
//                    Serdes.String())
//                    .withLoggingDisabled();
//            topology.addStateStore(blockStoreSupplier, "dataStreamSourceProcessor");
            //设置状态缓存
            StoreBuilder<KeyValueStore<String, Integer>> statusKeyStoreSupplier = Stores.keyValueStoreBuilder(
                    Stores.inMemoryKeyValueStore("statusStoreCache"),
                    Serdes.String(),
                    Serdes.Integer())
                    .withLoggingDisabled();
            topology.addStateStore(statusKeyStoreSupplier, "dataStreamSourceMapProcessor");


        //    System.out.println(topology.describe());
            // 查看具体构建的拓扑结构
            final KafkaStreams streams = new KafkaStreams(topology, props);
            // 控制运行次数，一次后就结束
            final CountDownLatch latch = new CountDownLatch(1);
            Runtime.getRuntime().addShutdownHook(new Thread(streams::close));
            try {
                streams.start();
                latch.await();
            } catch (Throwable e) {
                return 0;
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
        return 0;
    }
}
