[英]Kafka stream: Monthly time windows
基於這個例子( https://github.com/confluentinc/kafka-streams-examples/blob/5.5.0-post/src/test/java/io/confluent/examples/streams/window/DailyTimeWindows.java )我想創建一個每月時間 windows。 問題是尺寸方法,我不知道尺寸,因為每個月都有不同的尺寸。
有關更多上下文,我想根據 userId 計算一個月內進行交易的每個唯一用戶。
windowsFor 方法的實際實現:
public Map<Long, TimeWindow> windowsFor(final long timestamp) {
final Instant instant = Instant.ofEpochMilli(timestamp);
final ZonedDateTime zonedDateTime = instant.atZone(this.zoneId);
final ZonedDateTime startTime = zonedDateTime.truncatedTo(ChronoUnit.DAYS).withDayOfMonth(1);
final ZonedDateTime endTime = startTime.plusMonths(1);
final Map<Long, TimeWindow> windows = new LinkedHashMap<>();
windows.put(toEpochMilli(startTime), new TimeWindow(toEpochMilli(startTime), toEpochMilli(endTime)));
return windows;
}
有人有想法嗎?
問題是尺寸方法,我不知道尺寸,因為每個月都有不同的尺寸。
您可以將幾個月轉換為幾天,然后添加它。 您還需要注意檢查閏年。
public Map<Long, TimeWindow> windowsFor(final long timestamp) {
final Instant instant = Instant.ofEpochMilli(timestamp);
final ZonedDateTime zonedDateTime = instant.atZone(zoneId);
final ZonedDateTime startTime = zonedDateTime.truncatedTo(ChronoUnit.DAYS).withDayOfMonth(1);
final ZonedDateTime endTime = startTime.plusDays(getDays(startTime.getYear(), startTime.getMonthValue()));
final Map<Long, TimeWindow> windows = new LinkedHashMap<>();
windows.put(toEpochMilli(startTime), new TimeWindow(toEpochMilli(startTime), toEpochMilli(endTime)));
return windows;
}
public static int getDays(int year, int months) {
return YearMonth.of(year, months).lengthOfMonth();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.