Skip to content

Commit 0be9196

Browse files
committed
BAEL-1164 hzaelcast Jet.
1 parent 2f0b1b4 commit 0be9196

3 files changed

Lines changed: 80 additions & 12 deletions

File tree

‎hazelcast/pom.xml‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@
1313
</parent>
1414

1515
<dependencies>
16-
<dependency>
17-
<groupId>com.hazelcast</groupId>
18-
<artifactId>hazelcast</artifactId>
19-
<version>${hazelcast.version}</version>
20-
</dependency>
21-
<dependency>
22-
<groupId>com.hazelcast</groupId>
23-
<artifactId>hazelcast-client</artifactId>
24-
<version>${hazelcast.version}</version>
25-
</dependency>
16+
<!-- Hazelcast Jet -->
17+
<dependency>
18+
<groupId>com.hazelcast.jet</groupId>
19+
<artifactId>hazelcast-jet</artifactId>
20+
<version>${hazelcast.jet.version}</version>
21+
</dependency>
2622
</dependencies>
2723

2824
<build>
@@ -36,8 +32,8 @@
3632
</build>
3733

3834
<properties>
39-
<!-- hazelcast -->
40-
<hazelcast.version>3.8.4</hazelcast.version>
35+
<!-- hazelcast jet-->
36+
<hazelcast.jet.version>0.6</hazelcast.jet.version>
4137
</properties>
4238

4339
</project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.hazelcast.jet;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
import static com.hazelcast.jet.Traversers.traverseArray;
7+
import static com.hazelcast.jet.aggregate.AggregateOperations.counting;
8+
import static com.hazelcast.jet.function.DistributedFunctions.wholeItem;
9+
10+
import com.hazelcast.jet.Jet;
11+
import com.hazelcast.jet.JetInstance;
12+
import com.hazelcast.jet.pipeline.Pipeline;
13+
import com.hazelcast.jet.pipeline.Sinks;
14+
import com.hazelcast.jet.pipeline.Sources;
15+
16+
public class WordCounter {
17+
18+
private static final String LIST_NAME = "textList";
19+
20+
private static final String MAP_NAME = "countMap";
21+
22+
private Pipeline createPipeLine() {
23+
Pipeline p = Pipeline.create();
24+
p.drawFrom(Sources.<String> list(LIST_NAME))
25+
.flatMap(word -> traverseArray(word.toLowerCase()
26+
.split("\\W+")))
27+
.filter(word -> !word.isEmpty())
28+
.groupingKey(wholeItem())
29+
.aggregate(counting())
30+
.drainTo(Sinks.map(MAP_NAME));
31+
return p;
32+
}
33+
34+
public Long countWord(List<String> sentences, String word) {
35+
long count = 0;
36+
JetInstance jet = Jet.newJetInstance();
37+
try {
38+
List<String> textList = jet.getList(LIST_NAME);
39+
textList.addAll(sentences);
40+
Pipeline p = createPipeLine();
41+
jet.newJob(p)
42+
.join();
43+
Map<String, Long> counts = jet.getMap(MAP_NAME);
44+
count = counts.get(word);
45+
} finally {
46+
Jet.shutdownAll();
47+
}
48+
return count;
49+
}
50+
51+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.hazelcast.jet;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import org.junit.Test;
9+
10+
public class WordCounterUnitTest {
11+
12+
@Test
13+
public void whenGivenSentencesAndWord_ThenReturnCountOfWord() {
14+
List<String> sentences = new ArrayList<>();
15+
sentences.add("The first second was alright, but the second second was tough.");
16+
WordCounter wordCounter = new WordCounter();
17+
long countSecond = wordCounter.countWord(sentences, "second");
18+
assertTrue(countSecond == 3);
19+
}
20+
21+
}

0 commit comments

Comments
 (0)