使用Kafka滚动窗口查询时返回空数据

问题描述 投票:1回答:1

我正在尝试查询状态存储以在5分钟的窗口中获取数据。为此,我正在使用tumbling window。添加了REST查询数据。我有stream A,它使用topic1中的数据并执行一些转换并将键值输出到topic2。现在在stream B中,我正在对topic2数据进行滚动窗口操作。当我运行代码并使用REST查询时,我在浏览器上看到空数据。我可以看到状态存储中的数据正在流动。

我观察到的是,不是使用topic2stream A获取数据,而是使用生产者类将数据注入到topic2并能够从浏览器查询数据。但是,当topic2stream A获取数据时,我正在获取空数据。

这是我的stream A代码:

public static void main(String[] args) {    

                final StreamsBuilder builder = new StreamsBuilder();
                KStream<String, String> source = builder.stream("topic1");
                KStream<String, String> output = source
                        .map((k,v)->
                        {                                                                       
                            Map<String, Object> Fields = new LinkedHashMap<>();

                            Fields.put("FNAME","ABC");
                            Fields.put("LNAME","XYZ");

                            Map<String, Object> nFields = new LinkedHashMap<>();
                            nFields.put("ADDRESS1","HY");
                            nFields.put("ADDRESS2","BA");               
                            nFields.put("addF",Fields);

                            Map<String, Object> eve = new LinkedHashMap<>();                            
                            eve.put("nFields", nFields);

                            Map<String, Object> fevent = new LinkedHashMap<>();
                            fevent.put("eve", eve);             
                            LinkedHashMap<String, Object> newMap = new LinkedHashMap<>(fevent);                                 

                            return new KeyValue<>("JAY1234",newMap.toString());  
                        });

                output.to("topic2");        

}

这是我的stream B代码(发生翻滚窗口操作的地方:]

public static void main(String[] args) {

    final StreamsBuilder builder = new StreamsBuilder();
    KStream<String, String> eventStream = builder.stream("topic2");

    eventStream.groupByKey()
        .windowedBy(TimeWindows.of(300000))         
        .reduce((v1, v2) -> v1 + ";" + v2, Materialized.as("TumblingWindowPoc"));

    final Topology topology = builder.build();      
    KafkaStreams streams = new KafkaStreams(topology, props);   
    streams.start();      
}

REST代码:

@GET()
    @Path("/{storeName}/{key}")
    @Produces(MediaType.APPLICATION_JSON)
    public List<KeyValue<String, String>> windowedByKey(@PathParam("storeName") final String storeName,
            @PathParam("key") final String key) {

        final ReadOnlyWindowStore<String, String> store = streams.store(storeName,
                QueryableStoreTypes.<String, String>windowStore());
        if (store == null) {
            throw new NotFoundException();      }

        long timeTo = System.currentTimeMillis(); 
        long timeFrom = timeTo - 30000;         
        final WindowStoreIterator<String> results = store.fetch(key, timeFrom, timeTo);

        final List<KeyValue<String,String>> windowResults = new ArrayList<>();
        while (results.hasNext()) {
            final KeyValue<Long, String> next = results.next();     
            windowResults.add(new KeyValue<String,String>(key + "@" + next.key, next.value));
        }
        return windowResults;
    }

这就是我的键值数据的样子:

JAY1234 {eve = {nFields = {ADDRESS1 = HY,ADDRESS2 = BA,Fields = {FNAME = ABC,LNAME = XYZ,}}}}

使用REST查询时,我应该能够获取数据。任何帮助是极大的赞赏。谢谢!

java apache-kafka apache-kafka-streams kafka-producer-api
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.