+++ /dev/null
-package de.juplo.kafka.wordcount.top10;
-
-
-import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
-import lombok.AccessLevel;
-import lombok.AllArgsConstructor;
-import lombok.Data;
-import lombok.NoArgsConstructor;
-
-
-@NoArgsConstructor
-@AllArgsConstructor(
- staticName = "of",
- access = AccessLevel.PACKAGE)
-@Data
-@JsonIgnoreProperties(ignoreUnknown = true)
-public class Counter
-{
- String user;
- String word;
- long counter;
-}
package de.juplo.kafka.wordcount.top10;
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
+import lombok.NoArgsConstructor;
+@NoArgsConstructor
+@AllArgsConstructor(
+ staticName = "of",
+ access = AccessLevel.PACKAGE)
@Data
-@AllArgsConstructor(staticName = "of")
+@JsonIgnoreProperties(ignoreUnknown = true)
public class Entry
{
private String word;
- private Long count;
+ private Long counter;
}
{
private Entry[] entries = new Entry[0];
- public void add(Entry newEntry)
+ public Ranking add(Entry newEntry)
{
if (entries.length == 0)
{
entries = new Entry[1];
entries[0] = newEntry;
- return;
+ return this;
}
List<Entry> list = new LinkedList<>(Arrays.asList(entries));
Entry entry;
entry = list.get(i);
- if (entry.getCount() <= newEntry.getCount())
+ if (entry.getCounter() <= newEntry.getCounter())
{
list.add(i, newEntry);
for (int j = i+1; j < list.size(); j++)
list = list.subList(0,10);
}
entries = list.toArray(num -> new Entry[num]);
- return;
+ return this;
}
}
+
+ return this;
}
public static Ranking of(Entry... entries)
props.put(
JsonDeserializer.TYPE_MAPPINGS,
"word:" + Key.class.getName() + "," +
- "counter:" + Counter.class.getName());
+ "counter:" + Entry.class.getName());
props.put(JsonDeserializer.REMOVE_TYPE_INFO_HEADERS, Boolean.FALSE);
props.put(
- JsonDeserializer.TYPE_MAPPINGS,
+ JsonSerializer.TYPE_MAPPINGS,
"ranking:" + Ranking.class.getName());
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
StreamsBuilder builder = new StreamsBuilder();
builder
- .<Key, Counter>stream(inputTopic)
- .map((key, counter) ->
- {
- Entry entry = Entry.of(key.getWord(), counter.getCounter());
- return new KeyValue<>(key.getUser(), entry);
- })
+ .<Key, Entry>stream(inputTopic)
+ .map((key, entry) -> new KeyValue<>(key.getUser(), entry))
.groupByKey()
.aggregate(
() -> new Ranking(),
- (user, entry, ranking) ->
- {
- ranking.add(entry);
- return ranking;
- }
- )
+ (user, entry, ranking) ->ranking.add(entry))
.toStream()
.to(outputTopic);
--- /dev/null
+package de.juplo.kafka.wordcount.top10;
+
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import lombok.*;
+
+
+@Value(staticConstructor = "of")
+public class Counter
+{
+ String user;
+ String word;
+ long counter;
+}