Update README.md

This commit is contained in:
johnA1331
2019-10-30 22:12:05 +08:00
committed by GitHub
parent db85c8f275
commit 33998bdac8
20533 changed files with 1642695 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
## Hazelcast
This module contains articles about Hazelcast
### Relevant Articles:
- [Guide to Hazelcast with Java](https://www.baeldung.com/java-hazelcast)
- [Introduction to Hazelcast Jet](https://www.baeldung.com/hazelcast-jet)
+38
View File
@@ -0,0 +1,38 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" 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">
<modelVersion>4.0.0</modelVersion>
<artifactId>hazelcast</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hazelcast</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<!-- Hazelcast Jet -->
<dependency>
<groupId>com.hazelcast.jet</groupId>
<artifactId>hazelcast-jet</artifactId>
<version>${hazelcast.jet.version}</version>
</dependency>
</dependencies>
<build>
<finalName>hazelcast</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<!-- hazelcast jet-->
<hazelcast.jet.version>0.6</hazelcast.jet.version>
</properties>
</project>
@@ -0,0 +1,24 @@
package com.baeldung.hazelcast.cluster;
import java.util.Map.Entry;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.config.GroupConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
public class NativeClient {
public static void main(String[] args) throws InterruptedException {
ClientConfig config = new ClientConfig();
GroupConfig groupConfig = config.getGroupConfig();
groupConfig.setName("dev");
groupConfig.setPassword("dev-pass");
HazelcastInstance hazelcastInstanceClient = HazelcastClient.newHazelcastClient(config);
IMap<Long, String> map = hazelcastInstanceClient.getMap("data");
for (Entry<Long, String> entry : map.entrySet()) {
System.out.println(String.format("Key: %d, Value: %s", entry.getKey(), entry.getValue()));
}
}
}
@@ -0,0 +1,19 @@
package com.baeldung.hazelcast.cluster;
import java.util.Map;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IdGenerator;
public class ServerNode {
public static void main(String[] args) {
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
Map<Long, String> map = hazelcastInstance.getMap("data");
IdGenerator idGenerator = hazelcastInstance.getIdGenerator("newid");
for (int i = 0; i < 10; i++) {
map.put(idGenerator.newId(), "message" + 1);
}
}
}
@@ -0,0 +1,51 @@
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.JetInstance;
import com.hazelcast.jet.pipeline.Pipeline;
import com.hazelcast.jet.pipeline.Sinks;
import com.hazelcast.jet.pipeline.Sources;
public class WordCounter {
private static final String LIST_NAME = "textList";
private static final String MAP_NAME = "countMap";
private Pipeline createPipeLine() {
Pipeline p = Pipeline.create();
p.drawFrom(Sources.<String> list(LIST_NAME))
.flatMap(word -> traverseArray(word.toLowerCase()
.split("\\W+")))
.filter(word -> !word.isEmpty())
.groupingKey(wholeItem())
.aggregate(counting())
.drainTo(Sinks.map(MAP_NAME));
return p;
}
public Long countWord(List<String> sentences, String word) {
long count = 0;
JetInstance jet = Jet.newJetInstance();
try {
List<String> textList = jet.getList(LIST_NAME);
textList.addAll(sentences);
Pipeline p = createPipeLine();
jet.newJob(p)
.join();
Map<String, Long> counts = jet.getMap(MAP_NAME);
count = counts.get(word);
} finally {
Jet.shutdownAll();
}
return count;
}
}
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.7.xsd"
xmlns="http://www.hazelcast.com/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<network>
<port auto-increment="true" port-count="20">5701</port>
<join>
<multicast enabled="false">
</multicast>
<tcp-ip enabled="true">
<member>machine1</member>
<member>localhost</member>
</tcp-ip>
</join>
</network>
</hazelcast>
+19
View File
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
@@ -0,0 +1,21 @@
package com.baeldung.hazelcast.jet;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class WordCounterUnitTest {
@Test
public void whenGivenSentencesAndWord_ThenReturnCountOfWord() {
List<String> sentences = new ArrayList<>();
sentences.add("The first second was alright, but the second second was tough.");
WordCounter wordCounter = new WordCounter();
long countSecond = wordCounter.countWord(sentences, "second");
assertTrue(countSecond == 3);
}
}