简体   繁体   English

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

[英]Empty data is returned when querying using Kafka tumbling window

I'm trying to query the state store to get the data in a window of 5 mins.我正在尝试查询状态存储以在 5 分钟的窗口中获取数据。 For that I'm using tumbling window .为此,我正在使用tumbling window Have added REST to query the data.添加了 REST 来查询数据。 I've stream A which consumes data from topic1 and performs some transformations and output a key value to topic2 .我已经stream A ,其消耗来自数据topic1和执行一些变换和输出一个密钥值topic2 Now in stream B I'm doing tumbling window operation on topic2 data.现在在stream B我正在对topic2数据进行滚动窗口操作。 When I run the code and queried using REST, I'm seeing empty data on my browser.当我运行代码并使用 REST 进行查询时,我在浏览器上看到了空数据。 I can see the data in the state store flowing.我可以看到状态存储中的数据流动。

What I've observed is, instead of topic2 getting data from stream A , I used a producer class to inject the data to topic2 and able to query the data from browser.我观察到的是,我使用生产者类将数据注入到topic2并能够从浏览器查询数据,而不是topic2stream A获取数据。 But when the topic2 is getting data from stream A , I'm getting empty data.但是当topic2stream A获取数据时,我得到的是空数据。

Here is my stream A code :这是我的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");        

} }

Here is my stream B code (where tumbling window operation happening):这是我的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 code :休息代码:

@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;
    }

And this is how my key value data looks like :这就是我的键值数据的样子:

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

I should be able to get the data when querying using REST.我应该能够在使用 REST 查询时获取数据。 Any help is greatly appreciated.任何帮助是极大的赞赏。 Thanks!谢谢!

to fetch the window timeFrom should be before window start.获取窗口 timeFrom 应该在窗口启动之前。 So if you want the data for last 30 seconds, you can substract window duration for fetching, like timeTo - 30000 - 300000, and then filter out events required events from whole window data所以如果你想要最后30秒的数据,你可以减去窗口持续时间来获取,比如timeTo - 30000 - 300000,然后从整个窗口数据中过滤掉event required events

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM