Apache IgniteのREST APIを使ってCacheを操作する

# はじめに

Apache Igniteは、インメモリデータグリッドを実現するミドルウェアです。インメモリデータグリッドとは、大量のデータを複数のサーバ上のメモリで分散化する技術です。

今回はIgniteのREST APIにアクセスして、Cacheの操作等を試してみます。
APIの一覧は以下に記載されています。

https://apacheignite.readme.io/docs/rest-api

# いろいろ試してみる 

## キャッシュへPUT

```
# curl 'http://localhost:8080/ignite?cmd=put&key=newKey&val=newValue&cacheName=put-get-example'
{"successStatus":0,"affinityNodeId":"bf71e392-84bb-4e59-b22e-7f33dd153b69","error":null,"sessionToken":null,"response":true}
```

## キャッシュをGET

```
# curl 'http://localhost:8080/ignite?cmd=get&key=newKey&cacheName=put-get-example'
{"successStatus":0,"affinityNodeId":"bf71e392-84bb-4e59-b22e-7f33dd153b69","error":null,"sessionToken":null,"response":"newValue"}
```

## keyが存在するか確認する（Contains key）

```
# curl 'http://localhost:8080/ignite?cmd=conkey&key=newKey&cacheName=put-get-example'
{"successStatus":0,"affinityNodeId":"bf71e392-84bb-4e59-b22e-7f33dd153b69","error":null,"sessionToken":null,"response":true}

# curl 'http://localhost:8080/ignite?cmd=conkey&key=nonExsistKey&cacheName=put-get-example'
{"successStatus":0,"affinityNodeId":"bf71e392-84bb-4e59-b22e-7f33dd153b69","error":null,"sessionToken":null,"response":false}
```

## keyを削除する

```
keyが存在することを確認
# curl 'http://localhost:8080/ignite?cmd=get&key=newKey&cacheName=put-get-example'
{"successStatus":0,"affinityNodeId":"bf71e392-84bb-4e59-b22e-7f33dd153b69","error":null,"sessionToken":null,"response":"newValue"}

keyを削除
# curl 'http://localhost:8080/ignite?cmd=rmv&key=newKey&cacheName=put-get-example'
{"successStatus":0,"affinityNodeId":"bf71e392-84bb-4e59-b22e-7f33dd153b69","error":null,"sessionToken":null,"response":true}

keyが削除されていることを確認
# curl 'http://localhost:8080/ignite?cmd=get&key=newKey&cacheName=put-get-example'
{"successStatus":0,"affinityNodeId":"bf71e392-84bb-4e59-b22e-7f33dd153b69","error":null,"sessionToken":null,"response":null}
```


## Igniteのバージョンを表示する

```
# curl 'http://localhost:8080/ignite?cmd=version'
{"successStatus":0,"error":null,"sessionToken":null,"response":"2.6.0"}
```

## ログを取得する

```
# curl 'http://localhost:8080/ignite?cmd=log&from=20&to=21'
{"successStatus":0,"error":null,"sessionToken":null,"response":"[04:04:33,766][INFO][main][IgniteKernal] VM total memory: 0.97GB[04:04:33,766][INFO][main][IgniteKernal] Remote Management [restart: on, REST: on, JMX (remote: on, port: 49114, auth: off, ssl: off)]"}
```

## キャッシュとメトリクスを取得する

```
# curl 'http://localhost:8080/ignite?cmd=cache&cacheName=put-get-example'
{"successStatus":0,"affinityNodeId":null,"error":null,"sessionToken":null,"response":{"reads":0,"writes":0,"hits":0,"misses":0}}
```


## キャッシュのサイズを取得する

```
# curl 'http://localhost:8080/ignite?cmd=size&cacheName=put-get-example'
{"successStatus":0,"affinityNodeId":null,"error":null,"sessionToken":null,"response":5}
```

## Nodeの情報を取得する

IPアドレス指定でNodeの情報を取得しています。

```
# curl 'http://localhost:8080/ignite?cmd=node&attr=true&mtr=true&ip=192.168.20.71'
{"successStatus":0,"error":null,"sessionToken":null,"response":{"nodeId":"bf71e392-84bb-4e59-b22e-7f33dd153b69","consistentId":"0:0:0:0:0:0:0:1%lo,10.0.2.15,127.0.0.1,172.17.0.1,172.18.0.1,172.19.0.1,192.168.100.254,192.168.20.71:47500","tcpHostNames":["172.18.0.1","192.168.100.254","172.17.0.1","172.19.0.1","igniteserver1","10.0.2.15"],"tcpPort":11211,"metrics":{"lastUpdateTime":1541833414462,"totalRejectedJobs":0,"totalCancelledJobs":0,"totalExecutedJobs":2,"totalIdleTime":14336584,"upTime":14135992,"startTime":1541819072336,"nodeStartTime":1541819074702,"totalStartedThreadCount":2966,"fileSystemFreeSpace":-1,"fileSystemTotalSpace":-1,"fileSystemUsableSpace":-1,"heapMemoryInitialized":1073741824,"heapMemoryMaximum":1037959168,"sentMessagesCount":3,"sentBytesCount":354,"receivedMessagesCount":3,"receivedBytesCount":1406,"totalCpus":1,"currentCpuLoad":0.006666666666666667,"currentGcCpuLoad":0.0,"heapMemoryUsed":176683424,"heapMemoryCommitted":1037959168,"nonHeapMemoryInitialized":2555904,"nonHeapMemoryUsed":68267152,"nonHeapMemoryCommitted":70385664,"nonHeapMemoryMaximum":780140544,"totalBusyTime":-200592,"maximumActiveJobs":1,"currentActiveJobs":0,"averageActiveJobs":1.5535187E-4,"maximumWaitingJobs":0,"currentWaitingJobs":0,"averageWaitingJobs":0.0,"maximumRejectedJobs":0,"currentRejectedJobs":0,"averageRejectedJobs":0.0,"maximumCancelledJobs":0,"currentCancelledJobs":0,"averageCancelledJobs":0.0,"maximumJobWaitTime":0,"currentJobWaitTime":0,"averageJobWaitTime":0.0,"maximumJobExecuteTime":0,"currentJobExecuteTime":0,"averageJobExecuteTime":0.0,"totalExecutedTasks":0,"currentIdleTime":13382001,"busyTimePercentage":-0.014190197,"idleTimePercentage":1.0141902,"averageCpuLoad":0.00972140230956445,"currentThreadCount":90,"maximumThreadCount":103,"currentDaemonThreadCount":11,"lastDataVersion":1541819074296},"caches":[{"name":"put-get-example","mode":"PARTITIONED","sqlSchema":null}],"order":1,"attributes":{"awt.toolkit":"sun.awt.X11.XToolkit","h2.objectCacheMaxPerElementSize":"0","java.specification.version":"1.8","sun.cpu.isalist":"","sun.jnu.encoding":"UTF-8","org.apache.ignite.rest.tcp.host.names":["172.18.0.1","192.168.100.254","172.17.0.1","172.19.0.1","igniteserver1","10.0.2.15"],"sun.arch.data.model":"64","org.apache.ignite.build.ver":"2.6.0","java.vendor.url":"http://java.oracle.com/","h2.serializeJavaObject":"false","org.apache.ignite.update.notifier.enabled":true,"sun.boot.library.path":"/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/jre/lib/amd64","com.sun.management.jmxremote.ssl":"false","sun.java.command":"org.apache.ignite.startup.cmdline.CommandLineStartup config/default-config.xml","IGNITE_SUCCESS_FILE":"/opt/apache-ignite/work/ignite_success_48222445-2cb0-4927-8023-6a620806f047","java.specification.vendor":"Oracle Corporation","org.apache.ignite.rest.jetty.addrs":["172.18.0.1","192.168.100.254","172.17.0.1","172.19.0.1","192.168.20.71","10.0.2.15","0:0:0:0:0:0:0:1%lo","127.0.0.1"],"java.home":"/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/jre","file.separator":"/","line.separator":"\n","org.apache.ignite.data.streamer.pool.size":8,"java.vm.specification.vendor":"Oracle Corporation","java.specification.name":"Java Platform API Specification","org.apache.ignite.phy.ram":2985730048,"org.apache.ignite.peer.classloading.enabled":false,"org.apache.ignite.lang.rt":"Java Platform API Specification ver. 1.8","IGNITE_HOME":"/opt/apache-ignite","sun.boot.class.path":"/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/jre/lib/resources.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/jre/lib/rt.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/jre/lib/sunrsasign.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/jre/lib/jsse.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/jre/lib/jce.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/jre/lib/charsets.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/jre/lib/jfr.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/jre/classes","sun.management.compiler":"HotSpot 64-Bit Tiered Compilers","java.runtime.version":"1.8.0_191-b12","user.name":"root","IGNITE_JETTY_HOST":"0.0.0.0","org.apache.ignite.marshaller.utf8SerializationVer2":false,"org.apache.ignite.data.regions.offheap.size":597146009,"file.encoding":"UTF-8","org.apache.ignite.macs":"024238C4356A, 02424C97EE39, 024290204759, 02429177CE40, 08002776F3CF, 080027E34A44","org.apache.ignite.marshaller.compactFooter":true,"org.apache.ignite.user.name":"root","org.apache.ignite.services.compatibility.enabled":null,"org.apache.ignite.rest.tcp.port":11211,"java.io.tmpdir":"/tmp","org.apache.ignite.jit.name":"HotSpot 64-Bit Tiered Compilers","com.sun.management.jmxremote.port":"49114","java.version":"1.8.0_191","java.vm.specification.name":"Java Virtual Machine Specification","java.awt.printerjob":"sun.print.PSPrinterJob","org.apache.ignite.jmx.port":49114,"sun.os.patch.level":"unknown","IGNITE_PROG_NAME":"./ignite.sh","java.library.path":"/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib","java.vendor":"Oracle Corporation","java.rmi.server.randomIDs":"true","org.apache.ignite.jvm.args":"@@@-Xms1g@@@-Xmx1g@@@-XX:+AggressiveOpts@@@-XX:MaxMetaspaceSize=256m@@@-DIGNITE_QUIET=true@@@-DIGNITE_SUCCESS_FILE=/opt/apache-ignite/work/ignite_success_48222445-2cb0-4927-8023-6a620806f047@@@-Dcom.sun.management.jmxremote@@@-Dcom.sun.management.jmxremote.port=49114@@@-Dcom.sun.management.jmxremote.authenticate=false@@@-Dcom.sun.management.jmxremote.ssl=false@@@-DIGNITE_HOME=/opt/apache-ignite@@@-DIGNITE_PROG_NAME=./ignite.sh","sun.io.unicode.encoding":"UnicodeLittle","http.strictPostRedirect":"true","h2.optimizeTwoEquals":"false","file.encoding.pkg":"sun.io","com.sun.management.jmxremote.authenticate":"false","java.class.path":"/opt/apache-ignite/libs/annotations-13.0.jar:/opt/apache-ignite/libs/cache-api-1.0.0.jar:/opt/apache-ignite/libs/ignite-core-2.6.0.jar:/opt/apache-ignite/libs/ignite-shmem-1.0.0.jar:/opt/apache-ignite/libs/postgresql-42.2.4.jar:/opt/apache-ignite/libs/testm.jar:/opt/apache-ignite/libs/ignite-indexing/commons-codec-1.11.jar:/opt/apache-ignite/libs/ignite-indexing/h2-1.4.195.jar:/opt/apache-ignite/libs/ignite-indexing/ignite-indexing-2.6.0.jar:/opt/apache-ignite/libs/ignite-indexing/lucene-analyzers-common-5.5.2.jar:/opt/apache-ignite/libs/ignite-indexing/lucene-core-5.5.2.jar:/opt/apache-ignite/libs/ignite-indexing/lucene-queryparser-5.5.2.jar:/opt/apache-ignite/libs/ignite-log4j2/ignite-log4j2-2.6.0.jar:/opt/apache-ignite/libs/ignite-log4j2/log4j-api-2.8.1.jar:/opt/apache-ignite/libs/ignite-log4j2/log4j-core-2.8.1.jar:/opt/apache-ignite/libs/ignite-rest-http/commons-lang-2.6.jar:/opt/apache-ignite/libs/ignite-rest-http/ignite-rest-http-2.6.0.jar:/opt/apache-ignite/libs/ignite-rest-http/jackson-annotations-2.6.5.jar:/opt/apache-ignite/libs/ignite-rest-http/jackson-core-2.6.5.jar:/opt/apache-ignite/libs/ignite-rest-http/jackson-databind-2.6.5.jar:/opt/apache-ignite/libs/ignite-rest-http/jetty-continuation-9.2.11.v20150529.jar:/opt/apache-ignite/libs/ignite-rest-http/jetty-http-9.2.11.v20150529.jar:/opt/apache-ignite/libs/ignite-rest-http/jetty-io-9.2.11.v20150529.jar:/opt/apache-ignite/libs/ignite-rest-http/jetty-server-9.2.11.v20150529.jar:/opt/apache-ignite/libs/ignite-rest-http/jetty-util-9.2.11.v20150529.jar:/opt/apache-ignite/libs/ignite-rest-http/jetty-xml-9.2.11.v20150529.jar:/opt/apache-ignite/libs/ignite-rest-http/log4j-1.2.17.jar:/opt/apache-ignite/libs/ignite-rest-http/slf4j-api-1.7.7.jar:/opt/apache-ignite/libs/ignite-rest-http/slf4j-log4j12-1.7.7.jar:/opt/apache-ignite/libs/ignite-rest-http/tomcat-servlet-api-8.0.23.jar:/opt/apache-ignite/libs/ignite-spring/commons-logging-1.1.1.jar:/opt/apache-ignite/libs/ignite-spring/ignite-spring-2.6.0.jar:/opt/apache-ignite/libs/ignite-spring/spring-aop-4.3.16.RELEASE.jar:/opt/apache-ignite/libs/ignite-spring/spring-beans-4.3.16.RELEASE.jar:/opt/apache-ignite/libs/ignite-spring/spring-context-4.3.16.RELEASE.jar:/opt/apache-ignite/libs/ignite-spring/spring-core-4.3.16.RELEASE.jar:/opt/apache-ignite/libs/ignite-spring/spring-expression-4.3.16.RELEASE.jar:/opt/apache-ignite/libs/ignite-spring/spring-jdbc-4.3.16.RELEASE.jar:/opt/apache-ignite/libs/ignite-spring/spring-tx-4.3.16.RELEASE.jar:/opt/apache-ignite/libs/licenses/*","java.vm.vendor":"Oracle Corporation","user.timezone":"Europe/Vienna","org.apache.ignite.cache.client":false,"os.name":"Linux","java.vm.specification.version":"1.8","org.apache.ignite.cache.lateAffinity":true,"org.apache.ignite.consistency.check.skipped":false,"user.country":"US","sun.java.launcher":"SUN_STANDARD","com.sun.management.jmxremote":"","sun.cpu.endian":"little","user.home":"/root","user.language":"en","h2.objectCache":"false","org.apache.ignite.build.date":"20180710","java.awt.graphicsenv":"sun.awt.X11GraphicsEnvironment","org.apache.ignite.rest.jetty.host.names":["172.18.0.1","192.168.100.254","172.17.0.1","172.19.0.1","igniteserver1","10.0.2.15"],"IGNITE_QUIET":"true","org.apache.ignite.rest.jetty.port":8080,"org.apache.ignite.ips":"10.0.2.15, 172.17.0.1, 172.18.0.1, 172.19.0.1, 192.168.100.254, 192.168.20.71, fe80:0:0:0:a00:27ff:fee3:4a44%enp0s8, fe80:0:0:0:f9a5:ca24:2241:6cad%enp0s3","path.separator":":","os.version":"3.10.0-693.2.2.el7.x86_64","java.endorsed.dirs":"/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/jre/lib/endorsed","org.apache.ignite.rest.tcp.addrs":["172.18.0.1","192.168.100.254","172.17.0.1","172.19.0.1","192.168.20.71","10.0.2.15","0:0:0:0:0:0:0:1%lo","127.0.0.1"],"java.runtime.name":"OpenJDK Runtime Environment","org.apache.ignite.ignite.dep.mode":"SHARED","java.vm.name":"OpenJDK 64-Bit Server VM","org.apache.ignite.ignite.name":null,"org.apache.ignite.authentication.enabled":false,"org.apache.ignite.jvm.pid":4192,"java.vendor.url.bug":"http://bugreport.sun.com/bugreport/","org.apache.ignite.rest.port.range":100,"org.apache.ignite.offheap.size":702003609,"org.apache.ignite.marshaller.useDefaultSUID":false,"user.dir":"/opt/apache-ignite-fabric-2.6.0-bin/bin","org.apache.ignite.restart.enabled":"true","os.arch":"amd64","IGNITE_CONFIG_URL":"file:/opt/apache-ignite/config/default-config.xml","java.vm.info":"mixed mode","java.vm.version":"25.191-b12","java.ext.dirs":"/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/jre/lib/ext:/usr/java/packages/lib/ext","java.class.version":"52.0","org.apache.ignite.marshaller":"org.apache.ignite.internal.binary.BinaryMarshaller"},"tcpAddresses":["172.18.0.1","192.168.100.254","172.17.0.1","172.19.0.1","192.168.20.71","10.0.2.15","0:0:0:0:0:0:0:1%lo","127.0.0.1"]}}
```