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

Running HelloWorld

You have successfully installed Apache Ignite, now it's time for fun. Let's connect to the Apache Ignite node and create a cache. The following are the steps to create a new Ignite cache:

  1. Open your favorite IDE and create a new Gradle project, hello-world
  2. Edit the build.gradle file with the following entries:
      implementation 'com.h2database:h2:1.4.196'
compile group: 'org.apache.ignite', name: 'ignite-core', version:
'2.5.0'
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.6.0'
compile group: 'org.apache.ignite', name: 'ignite-spring',
version: '2.5.0'
compile group: 'org.apache.ignite', name: 'ignite-indexing',
version: '2.5.0'
compile group: 'log4j', name: 'log4j', version: '1.2.17'
  1. Create a new Java class, HelloWorld
  2. Add the following lines to create a cache, myFirstIgniteCache, put values into the cache, and then retrieve values from the cache:
      public class HelloWorld {
public static void main(String[] args) {
try (Ignite ignite = Ignition.start()) {
IgniteCache<Integer, String> cache =
ignite.getOrCreateCache("myFirstIgniteCache");

for (int i = 0; i < 10; i++)
cache.put(i, Integer.toString(i));

for (int i = 0; i < 10; i++)
System.out.println("Fetched [key=" + i + ", val=" +
cache.get(i) + ']');
}
}
}

The Ignition.start() starts an Ignite instance in memory. A cache stores key-value pairs like java.util.Map. IgniteCache<Key, Value> represents a distributed cache where Key and Value are serializable objects. Here, we are asking Ignite to create (or get, if already created) a cache myFirstIgniteCache to store an integer key and String value, then store 10 integers in the cache, and finally ask the cache to get the values:

  1. Add the import statements and run the program.
  2. It will start a server node and add it to the existing cluster. You can see the topology snapshot indicating version=2 and servers=2:

Don't panic if the code doesn't look familiar; we will explore it step by step in Chapter 2, Understanding the Topologies and Caching Strategies.

主站蜘蛛池模板: 昌图县| 会昌县| 天水市| 大名县| 定边县| 莆田市| 兴文县| 通榆县| 河池市| 白朗县| 石家庄市| 靖西县| 威信县| 密云县| 玉环县| 怀仁县| 沅陵县| 望城县| 筠连县| 宣汉县| 香格里拉县| 芒康县| 泸西县| 芒康县| 绵阳市| 马尔康县| 贡觉县| 威远县| 慈溪市| 巩留县| 广宁县| 浦江县| 德令哈市| 象州县| 建昌县| 郧西县| 英吉沙县| 海口市| 抚宁县| 郸城县| 晋中市|