In our previous tutorial, we had setup a multi-node ignite cluster on a windows machine. Let’s extend it with our first program involving apache ignite. In this tutorial, we will create a cache in a single server ignite cluster; add some values and retrieve it back. I’ll demonstrate this tutorial with eclipse IDE. If you are using any other IDE, you can modify some eclipse specific instructions accordingly.
Followings are used as part of this activity:
1 2 3 4 5 |
JAVA version "1.8.0_144" Apache Ignite version "2.0.0" Eclipse Neon Apache Maven Windows 10 Laptop |
- Launch eclipse with any workspace of your choice. In my case, the workspace is “”F:\workspace\java\ignite”.
- In the project explorer, right click to create New -> Project … From the New Project dialog, select “Maven Project” wizard. Click Next > buton couple of times and go to the window where you need to specify Group Id, Artifact id and Package and click on Finish button. I have set the followings as the value –
123Group Id: com.iteritory.igniteArtifact Id: ignite-tutorialPackage: com.iteritory.ignite.ignitetutorial - Next, we’ll update the pom.xml with the required dependencies. For me, it’s the exact like below –
1234567891011121314151617181920212223242526272829303132333435<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><groupId>com.iteritory.ignite</groupId><artifactId>ignite-tutorial</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>ignite-tutorial</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>org.apache.ignite</groupId><artifactId>ignite-core</artifactId><version>2.0.0</version></dependency><dependency><groupId>org.apache.ignite</groupId><artifactId>ignite-spring</artifactId><version>2.0.0</version></dependency></dependencies></project>
- Next, time to write a very simple JAVA program that will join a pre-existing single node ignite server cluster as a client; it’ll create a cache, put some values and retrieve the same from cache.
- Right click on the package “com.iteritory.ignite.ignitetutorial” in package explorer and create a new JAVA class called “FirstProgram.java”. My program looks as below –
1234567891011121314151617181920212223242526272829303132package com.iteritory.ignite.ignitetutorial;import org.apache.ignite.Ignite;import org.apache.ignite.IgniteCache;import org.apache.ignite.Ignition;public class FirstProgram {public static void main(String[] args) {// Set the node start mode as client; so, this node will join the apache// cluster as clientIgnition.setClientMode(true);// Here, we provide the cache configuration fileIgnite objIgnite = Ignition.start("F:\\apache-ignite-fabric-2.0.0-bin\\config\\itc-poc-config.xml");// create cache if not already existingIgniteCache<Integer, String> objIgniteCache = objIgnite.getOrCreateCache("myFirstIgniteCache");// Populating the cache with few valuesobjIgniteCache.put(1, "salman");objIgniteCache.put(2, "Abhishek");objIgniteCache.put(3, "Siddharth");objIgniteCache.put(4, "Dev");// Get these items from cacheSystem.out.println(objIgniteCache.get(1));System.out.println(objIgniteCache.get(2));System.out.println(objIgniteCache.get(3));System.out.println(objIgniteCache.get(4));}}
- The package structure looks like below –
- Next we’ll create itc-poc-config.xml; the content is as below –
1234567891011121314151617181920212223242526272829303132333435363738394041<?xml version="1.0" encoding="UTF-8"?><!--Licensed to the Apache Software Foundation (ASF) under one or morecontributor license agreements. See the NOTICE file distributed withthis work for additional information regarding copyright ownership.The ASF licenses this file to You under the Apache License, Version 2.0(the "License"); you may not use this file except in compliance withthe License. You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.--><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean class="org.apache.ignite.configuration.IgniteConfiguration"><!-- Configure TCP discovery SPI to provide list of initial nodes. --><property name="discoverySpi"><bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi"><property name="ipFinder"><bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder"><property name="addresses"><list><value>127.0.0.1:47500..47509</value></list></property></bean></property></bean></property></bean></beans>
- Next, launch command prompt and change directory to %IGNITE_HOME%/bin
- Run command –> ignite.bat F:\apache-ignite-fabric-2.0.0-bin\config\itc-poc-config.xml
- After the server starts, we’ll see last few lines are something similar to following –
- Now, that server is running, we will run above JAVA program as an application from eclipse. In a while, you will see something like following –
- If you analyze the result above, the JAVA program we wrote, joined the cluster as a node; hence, the client became 0 to 1. You can see the same from the server log as well. If you open the command window where we ran the ignite.bat file, you will see a new line; let’s have a look into following screenshot from the server logs –
- The other part of the result in step 11, outputs the 4 names, we inserted into the cache from the program itself.
- So, to conclude, this tutorial gives a basic overview of apache ignite client and server node; how a client node joins the server, how to create a simple cache in apache ignite, how to put data in apache ignite cache and how to retrieve data from cache.
Nikita
February 8, 2018[SEVERE][main][IgniteKernal] Failed to start manager: GridManagerAdapter [enabled=true, name=o.a.i.i.managers.discovery.GridDiscoveryManager]
class org.apache.ignite.IgniteCheckedException: Failed to start SPI: TcpDiscoverySpi [addrRslvr=null, sockTimeout=5000, ackTimeout=5000, marsh=JdkMarshaller [], reconCnt=10, maxAckTimeout=600000, forceSrvMode=false, clientReconnectDisabled=false]
Sadruddin Md
February 9, 2018Can you please paste the entire error log
vb
February 22, 2018[15:31:17] Topology snapshot [ver=4, servers=2, clients=2, CPUs=8, heap=5.4GB]
[15:31:18,983][SEVERE][exchange-worker-#38][GridDhtPartitionsExchangeFuture] Failed to reinitialize local partitions (preloading will be stopped): GridDhtPartitionExchangeId [topVer=AffinityTopologyVersion [topVer=4, minorTopVer=1], discoEvt=DiscoveryCustomEvent [customMsg=DynamicCacheChangeBatch [id=f59d32eb161-20f8ff9b-62e6-4cc8-8a44-b88336c361cb, reqs=[DynamicCacheChangeRequest [cacheName=Orders_Cache, hasCfg=true, nodeId=c6efcb38-b257-4560-81a2-652b835d7a22, clientStartOnly=false, stop=false, destroy=false]], exchangeActions=ExchangeActions [startCaches=[Orders_Cache], stopCaches=null, startGrps=[Orders_Cache], stopGrps=[], resetParts=null, stateChangeRequest=null], startCaches=false], affTopVer=AffinityTopologyVersion [topVer=4, minorTopVer=1], super=DiscoveryEvent [evtNode=TcpDiscoveryNode [id=c6efcb38-b257-4560-81a2-652b835d7a22, addrs=[0:0:0:0:0:0:0:1%lo, 127.0.0.1, 192.168.1.207], sockAddrs=[/0:0:0:0:0:0:0:1%lo:0, /127.0.0.1:0, /192.168.1.211:0], discPort=0, order=4, intOrder=4, lastExchangeTime=1519313477877, loc=false, ver=2.3.0#20171028-sha1:8add7fd5, isClient=true], topVer=4, nodeId8=ed59e230, msg=null, type=DISCOVERY_CUSTOM_EVT, tstamp=1519313478974]], nodeId=c6efcb38, evt=DISCOVERY_CUSTOM_EVT]
java.lang.RuntimeException: Failed to create an instance of com.iteritory.ignite.adapter.OrdersCacheStoreAdapter
at javax.cache.configuration.FactoryBuilder$ClassFactory.create(FactoryBuilder.java:134)
Nek
July 18, 2018Very good article and Thanks!!. btw do you have this code uploaded anywhere as a package so that I can clone and try quickly.
Sadruddin Md
July 18, 2018Hey Nek, Thank you.
For now, I didn’t push the code in git; however, I plan to do it soon. I will update you here once I have done it.
Nek
July 23, 2018Thanks !!