官术网_书友最值得收藏!

Mapping back to the real world

Having briefly explored Hazelcast's distributed capabilities via a test console, let's have a look at how we are more likely to interact with a cluster in the real world. Let's create a new SimpleMapExample class with a main method to spin up and manipulate a named distributed map called capitals. Hazelcast refers to these named collections as a namespace and must be uniquely named across the cluster.

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import java.util.Map;

public class SimpleMapExample {
  public static void main(String[] args) {
    HazelcastInstance hz = Hazelcast.newHazelcastInstance();

    Map<String, String> capitals = hz.getMap("capitals");
    capitals.put("GB", "London");
    capitals.put("FR", "Paris");
    capitals.put("US", "Washington DC");
    capitals.put("AU", "Canberra");

    System.err.println(
      "Known capital cities: " + capitals.size());

    System.err.println(
      "Capital city of GB: " + capitals.get("GB"));
  }
}

As before, we should see various logging entries on the startup as well as the fun fact outputs; confirming we persisted into and retrieved from the map programmatically. We can also use this example in conjunction with our TestApp console from the previous section and interact with the new map capitals. To do this, we will need to switch namespaces from within the console before interacting with the map. Remember to make sure they have formed a cluster when using the console if you closed it since the previous example.

hazelcast[default] > ns capitals
namespace: capitals

hazelcast[capitals] > m.get GB
London

As with other implementations of Java maps, if we are creating our own objects for use within a Hazelcast map, we will need to consider the use of the custom equals() and hashCode() methods; however, it is the serialized binary form of the object that is used instead of these custom methods when the object is used as a key to a Hazelcast map entry.

主站蜘蛛池模板: 泗水县| 安义县| 章丘市| 崇州市| 商河县| 乃东县| 江源县| 普兰县| 宁波市| 靖远县| 夏邑县| 九龙城区| 荥经县| 通辽市| 武鸣县| 尼勒克县| 浙江省| 铁力市| 灌云县| 德安县| 阿勒泰市| 汾阳市| 石狮市| 曲阜市| 佛冈县| 东乌珠穆沁旗| 普安县| 巴青县| 伊吾县| 木兰县| 闸北区| 敖汉旗| 达日县| 临泽县| 马山县| 开远市| 阳山县| 同江市| 伊宁县| 石城县| 合作市|