BAEL-4595: Upgrade to hazelcast-jet 4.2 (#10035)

This commit is contained in:
kwoyke 2020-09-15 20:33:11 +02:00 committed by GitHub
parent b64433bb36
commit 4ae6ecbb39
5 changed files with 30 additions and 39 deletions

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project <project
xmlns="http://maven.apache.org/POM/4.0.0" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>hazelcast</artifactId> <artifactId>hazelcast</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
@ -15,7 +15,6 @@
</parent> </parent>
<dependencies> <dependencies>
<!-- Hazelcast Jet -->
<dependency> <dependency>
<groupId>com.hazelcast.jet</groupId> <groupId>com.hazelcast.jet</groupId>
<artifactId>hazelcast-jet</artifactId> <artifactId>hazelcast-jet</artifactId>
@ -34,8 +33,7 @@
</build> </build>
<properties> <properties>
<!-- hazelcast jet --> <hazelcast.jet.version>4.2</hazelcast.jet.version>
<hazelcast.jet.version>0.6</hazelcast.jet.version>
</properties> </properties>
</project> </project>

View File

@ -1,24 +1,20 @@
package com.baeldung.hazelcast.cluster; package com.baeldung.hazelcast.cluster;
import java.util.Map.Entry;
import com.hazelcast.client.HazelcastClient; import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig; import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.config.GroupConfig;
import com.hazelcast.core.HazelcastInstance; import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import java.util.Map;
public class NativeClient { public class NativeClient {
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) {
ClientConfig config = new ClientConfig(); ClientConfig config = new ClientConfig();
GroupConfig groupConfig = config.getGroupConfig(); config.setClusterName("dev");
groupConfig.setName("dev");
groupConfig.setPassword("dev-pass");
HazelcastInstance hazelcastInstanceClient = HazelcastClient.newHazelcastClient(config); HazelcastInstance hazelcastInstanceClient = HazelcastClient.newHazelcastClient(config);
IMap<Long, String> map = hazelcastInstanceClient.getMap("data"); Map<Long, String> map = hazelcastInstanceClient.getMap("data");
for (Entry<Long, String> entry : map.entrySet()) { for (Map.Entry<Long, String> entry : map.entrySet()) {
System.out.println(String.format("Key: %d, Value: %s", entry.getKey(), entry.getValue())); System.out.printf("Key: %d, Value: %s%n", entry.getKey(), entry.getValue());
} }
} }
} }

View File

@ -1,19 +1,19 @@
package com.baeldung.hazelcast.cluster; package com.baeldung.hazelcast.cluster;
import java.util.Map;
import com.hazelcast.core.Hazelcast; import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance; import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IdGenerator; import com.hazelcast.flakeidgen.FlakeIdGenerator;
import java.util.Map;
public class ServerNode { public class ServerNode {
public static void main(String[] args) { public static void main(String[] args) {
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(); HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
Map<Long, String> map = hazelcastInstance.getMap("data"); Map<Long, String> map = hazelcastInstance.getMap("data");
IdGenerator idGenerator = hazelcastInstance.getIdGenerator("newid"); FlakeIdGenerator idGenerator = hazelcastInstance.getFlakeIdGenerator("newid");
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
map.put(idGenerator.newId(), "message" + 1); map.put(idGenerator.newId(), "message" + i);
} }
} }
} }

View File

@ -1,33 +1,31 @@
package com.baeldung.hazelcast.jet; package com.baeldung.hazelcast.jet;
import java.util.List;
import java.util.Map;
import static com.hazelcast.jet.Traversers.traverseArray;
import static com.hazelcast.jet.aggregate.AggregateOperations.counting;
import static com.hazelcast.jet.function.DistributedFunctions.wholeItem;
import com.hazelcast.jet.Jet; import com.hazelcast.jet.Jet;
import com.hazelcast.jet.JetInstance; import com.hazelcast.jet.JetInstance;
import com.hazelcast.jet.pipeline.Pipeline; import com.hazelcast.jet.pipeline.Pipeline;
import com.hazelcast.jet.pipeline.Sinks; import com.hazelcast.jet.pipeline.Sinks;
import com.hazelcast.jet.pipeline.Sources; import com.hazelcast.jet.pipeline.Sources;
import java.util.List;
import java.util.Map;
import static com.hazelcast.function.Functions.wholeItem;
import static com.hazelcast.jet.Traversers.traverseArray;
import static com.hazelcast.jet.aggregate.AggregateOperations.counting;
public class WordCounter { public class WordCounter {
private static final String LIST_NAME = "textList"; private static final String LIST_NAME = "textList";
private static final String MAP_NAME = "countMap"; private static final String MAP_NAME = "countMap";
private Pipeline createPipeLine() { private Pipeline createPipeLine() {
Pipeline p = Pipeline.create(); Pipeline p = Pipeline.create();
p.drawFrom(Sources.<String> list(LIST_NAME)) p.readFrom(Sources.<String>list(LIST_NAME))
.flatMap(word -> traverseArray(word.toLowerCase() .flatMap(word -> traverseArray(word.toLowerCase().split("\\W+")))
.split("\\W+")))
.filter(word -> !word.isEmpty()) .filter(word -> !word.isEmpty())
.groupingKey(wholeItem()) .groupingKey(wholeItem())
.aggregate(counting()) .aggregate(counting())
.drainTo(Sinks.map(MAP_NAME)); .writeTo(Sinks.map(MAP_NAME));
return p; return p;
} }
@ -38,8 +36,7 @@ public class WordCounter {
List<String> textList = jet.getList(LIST_NAME); List<String> textList = jet.getList(LIST_NAME);
textList.addAll(sentences); textList.addAll(sentences);
Pipeline p = createPipeLine(); Pipeline p = createPipeLine();
jet.newJob(p) jet.newJob(p).join();
.join();
Map<String, Long> counts = jet.getMap(MAP_NAME); Map<String, Long> counts = jet.getMap(MAP_NAME);
count = counts.get(word); count = counts.get(word);
} finally { } finally {

View File

@ -1,11 +1,11 @@
package com.baeldung.hazelcast.jet; package com.baeldung.hazelcast.jet;
import static org.junit.Assert.assertTrue; import org.junit.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.junit.Test; import static org.junit.Assert.assertEquals;
public class WordCounterUnitTest { public class WordCounterUnitTest {
@ -15,7 +15,7 @@ public class WordCounterUnitTest {
sentences.add("The first second was alright, but the second second was tough."); sentences.add("The first second was alright, but the second second was tough.");
WordCounter wordCounter = new WordCounter(); WordCounter wordCounter = new WordCounter();
long countSecond = wordCounter.countWord(sentences, "second"); long countSecond = wordCounter.countWord(sentences, "second");
assertTrue(countSecond == 3); assertEquals(3, countSecond);
} }
} }