# Atomic increment for nested map

**URL:** <https://discuss.aerospike.com/t/atomic-increment-for-nested-map/8171>\
**Category:** Java Client\
**Created:** [January 20, 2021, 6:56pm UTC](https://discuss.aerospike.com/t/atomic-increment-for-nested-map/8171 "2021-01-20T18:56:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![aparekh](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@aparekh](https://discuss.aerospike.com/u/aparekh)\
**Post date:** [January 20, 2021, 6:56pm UTC](https://discuss.aerospike.com/t/atomic-increment-for-nested-map/8171/1 "2021-01-20T18:56:03Z")

</div>

Hey, currently on Aerospike Java client we support increment for maps using MapOperation.increment(mapPolicy, bin, mapKey, value) → Map\<Long, Long\>

is there someway we can increment a nested map Map\<Long, Map\<Long, Long\>\> MapOperation.increment(mapPolicy, bin, mapKey, mapSecondKey, value)

If something like this doesn’t exist yet, would udf be another way of handling these?

---

<div class="post-metadata">

**Author:** ![aparekh](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@aparekh](https://discuss.aerospike.com/u/aparekh)\
**Post date:** [January 20, 2021, 7:05pm UTC](https://discuss.aerospike.com/t/atomic-increment-for-nested-map/8171/2 "2021-01-20T19:05:39Z")

</div>

I tried the following command and that didn’t work either MapOperation.put(mapPolicy, “map”, Value.get(“count”), Value.get(10), CTX.mapKey(Value.get(“123”))))

and I get the following error : com.aerospike.client.AerospikeException: Error 26,1,30000,5000,1,BB9979897BC230A 172.17.35.56 3000: Operation not applicable

---

<div class="post-metadata">

**Author:** ![kporter](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/kporter/32/515_2.png) [@kporter](https://discuss.aerospike.com/u/kporter)\
**Post date:** [January 20, 2021, 7:39pm UTC](https://discuss.aerospike.com/t/atomic-increment-for-nested-map/8171/3 "2021-01-20T19:39:05Z")

</div>

Yes, but it will depend on the version of the Java client and Aerospike server that you are running.

The [MapOperation.increment](https://www.aerospike.com/apidocs/java/com/aerospike/client/cdt/MapOperation.html#increment-com.aerospike.client.cdt.MapPolicy-java.lang.String-com.aerospike.client.Value-com.aerospike.client.Value-com.aerospike.client.cdt.CTX...-) in the current Java version (5.0.2) supports passing a context to find the element to apply the operation to.

[Aerospike 4.6.0.2](https://www.aerospike.com/download/server/notes.html#4.6.0.2) introduced support for nested List/Map update operations via context parameter.

[Aerospike 4.9.0.3](https://www.aerospike.com/download/server/notes.html#4.9.0.3) introduced support for nested List/Map operations via context parameter that can create the elements in the context path (similar to `mkdir -p`).

---

<div class="post-metadata">

**Author:** ![aparekh](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@aparekh](https://discuss.aerospike.com/u/aparekh)\
**Post date:** [January 20, 2021, 7:50pm UTC](https://discuss.aerospike.com/t/atomic-increment-for-nested-map/8171/4 "2021-01-20T19:50:25Z")

</div>

I updated to 5.0.2 when I got the error that operation is not supported

---

<div class="post-metadata">

**Author:** ![xorphox](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/xorphox/32/633_2.png) [@xorphox](https://discuss.aerospike.com/u/xorphox)\
**Post date:** [January 20, 2021, 11:12pm UTC](https://discuss.aerospike.com/t/atomic-increment-for-nested-map/8171/5 "2021-01-20T23:12:41Z")

</div>

Is there a top level map key of “123”? Since you mention they are Long keys, did you mean Value.get(123), without the quotes?

---

<div class="post-metadata">

**Author:** ![aparekh](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@aparekh](https://discuss.aerospike.com/u/aparekh)\
**Post date:** [January 21, 2021, 6:55pm UTC](https://discuss.aerospike.com/t/atomic-increment-for-nested-map/8171/6 "2021-01-21T18:55:03Z")

</div>

You are correct. I double checked the method expected structure : {321={123=100}}

```auto
MapOperation.put(
    MapPolicy.Default,
    "bin",
    Value.get(123),
    Value.get(100),
    CTX.mapKey(Value.get(321))));

```

following the example from here : [https://www.aerospike.com/apidocs/java/com/aerospike/client/cdt/MapOperation.html#increment-com.aerospike.client.cdt.MapPolicy-java.lang.String-com.aerospike.client.Value-com.aerospike.client.Value-com.aerospike.client.cdt.CTX](https://www.aerospike.com/apidocs/java/com/aerospike/client/cdt/MapOperation.html#increment-com.aerospike.client.cdt.MapPolicy-java.lang.String-com.aerospike.client.Value-com.aerospike.client.Value-com.aerospike.client.cdt.CTX)…-

The aerospike server version is 4.9.0.11 and client version is 5.0.2.

---

<div class="post-metadata">

**Author:** ![aparekh](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@aparekh](https://discuss.aerospike.com/u/aparekh)\
**Post date:** [January 22, 2021, 11:42pm UTC](https://discuss.aerospike.com/t/atomic-increment-for-nested-map/8171/7 "2021-01-22T23:42:10Z")

</div>

@kporter are there are examples to compare what might be wrong here?

---

<div class="post-metadata">

**Author:** ![xorphox](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/xorphox/32/633_2.png) [@xorphox](https://discuss.aerospike.com/u/xorphox)\
**Post date:** [January 23, 2021, 1:39am UTC](https://discuss.aerospike.com/t/atomic-increment-for-nested-map/8171/8 "2021-01-23T01:39:48Z")

</div>

That should work if the top level key 321 already exist.

---

<div class="post-metadata">

**Author:** ![aparekh](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@aparekh](https://discuss.aerospike.com/u/aparekh)\
**Post date:** [January 25, 2021, 5:37pm UTC](https://discuss.aerospike.com/t/atomic-increment-for-nested-map/8171/9 "2021-01-25T17:37:20Z")

</div>

If I use increment instead of put, do both keys still need to exist? For regular maps if key doesn’t exist MapOperation.increment creates the key. Should it work the same way for nested maps?

---

<div class="post-metadata">

**Author:** ![xorphox](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/xorphox/32/633_2.png) [@xorphox](https://discuss.aerospike.com/u/xorphox)\
**Post date:** [January 25, 2021, 7:10pm UTC](https://discuss.aerospike.com/t/atomic-increment-for-nested-map/8171/10 "2021-01-25T19:10:16Z")

</div>

You have to specify a context flag for that to happen. That has been implemented since 4.9.0.3. I’m not sure how to do that in Java but the context flag to create context (if it didn’t exist) should be there.

---

<div class="post-metadata">

**Author:** ![kporter](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/kporter/32/515_2.png) [@kporter](https://discuss.aerospike.com/u/kporter)\
**Post date:** [January 25, 2021, 7:22pm UTC](https://discuss.aerospike.com/t/atomic-increment-for-nested-map/8171/11 "2021-01-25T19:22:38Z")

</div>

The Java APIs for context can be found here: [https://www.aerospike.com/apidocs/java/com/aerospike/client/cdt/CTX.html](https://www.aerospike.com/apidocs/java/com/aerospike/client/cdt/CTX.html)

You would be interested in the APIs suffixed “create”.

---

<div class="post-metadata">

**Author:** ![aparekh](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@aparekh](https://discuss.aerospike.com/u/aparekh)\
**Post date:** [January 26, 2021, 12:54am UTC](https://discuss.aerospike.com/t/atomic-increment-for-nested-map/8171/12 "2021-01-26T00:54:11Z")

</div>

Thank for all the help, it works now.
