apache ignite_使用Apache Ignite瘦客户端– Apach( 二 )

< String, String > clientCache = igniteClient.getOrCreateCache(CACHE\ _NAME);// put a few valueclientCache.put("Moscow", "095");clientCache.put("Vladimir", "033");// get the region code of the Vladimir String val = clientCache.get("Vladimir");logger.info("Print value: {}", val);} catch (IgniteException e) {logger.error("Ignite exception:", e.getMessage());} catch (Exception e) {logger.error("Ignite exception:", e.getMessage());}}}
步骤4,让我们仔细看一下我们上面编写的程序 。
private static final Logger logger = LoggerFactory.getLogger(HelloThinClient.class);private static final String HOST = "127.0.0.1";private static final String PORT = "10800";private static final String CACHE_NAME = "thin-cache";
首先,我们声明了一些常量:记录器,主机IP地址,端口和我们要创建的缓存名称 。如果您使用其他IP地址,则应在此处进行更改 。端口10800是瘦客户端的默认端口 。
СlientConfiguration cfg = new ClientConfiguration().setAddresses(HOST+":"+PORT);
这些是程序中下一个令人兴奋的行 。我们创建了一个点燃实例
С并传递了我们上面声明的地址 。在下一个try-catch块中,我们定义了一个新缓存,名称为
精简缓存并放置2个键值对 。我们还使用了.方法来初始化与节点的连接 。
try (IgniteClient igniteClient = Ignition.startClient(cfg)) {ClientCache < String, String > clientCache = igniteClient.getOrCreateCache(CACHE\ _NAME);// put a few valueclientCache.put("Moscow", "095");clientCache.put("Vladimir", "033");// get the region code of the Vladimir String val = clientCache.get("Vladimir");logger.info("Print value: {}", val);} catch (IgniteException e) {logger.error("Ignite exception:", e.getMessage());} catch (Exception e) {logger.error("Ignite exception:", e.getMessage());}}
后来,我们检索了键的值并将其打印在控制台中 。
步骤5。如果尚未启动 单节点集群,请启动它 。在您喜欢的终端中使用以下命令 。

apache ignite_使用Apache Ignite瘦客户端– Apach

文章插图
$ IGNITE_HOME/bin/ignite.sh
步骤6。要生成项目,请发出以下命令 。
$ mvn clean install
这将运行Maven,告诉它执行安装目标 。此目标将编译,测试和打包您的项目代码,然后将其复制到本地依赖项存储库中 。成功编译后,第一次构建过程将需要几分钟才能完成,名为
-.jar将在目标目录中创建 。
步骤7。通过键入以下命令来运行应用程序 。
$ java -jar .\target\HelloThinClient-runnable.jar
您应该在终端中看到很多日志 。在日志末尾,您应该会找到类似的内容 。
该应用程序通过TCP套接字连接到节点,并在缓存精简缓存上执行放置和获取操作 。如果您查看节点控制台,您应该注意到群集拓扑尚未更改 。
翻译自:
【apache ignite_使用Apache Ignite瘦客户端– Apach】