Python client - Nested map, map_operation.get_by_key fails

I am just trying out this CDT/Nested map operation in Python but not sure where I am going wrong. Using the latest docker image for Aerospike in default configuration. Any pointers will help.

I get this error: (-2, ‘Failed to convert ctx’, ‘src/main/conversions.c’, 2047, False)

The put operation is successful.

Here is my code

client = aerospike.Client({"hosts": ["0.0.0.0"]})
try:
    print(datetime.now())
    client.connect()
    key = ("test", "chat_room_2", "userid")
    bin_name = "complex_map_bin"
    client.put(key, 
                {"bin1":
                    {
                        1523474230000: ['fav',     {'sku':1, 'b':2}],
                        1523474231001: ['comment', {'sku':2, 'b':22}],
                        1523474236006: ['viewed',  {'foo':'bar', 'sku':3, 'zz':'top'}],
                        1523474235005: ['comment', {'sku':1, 'c':1234}],
                        1523474233003: ['viewed',  {'sku':3, 'z':26}],
                        1523474234004: ['viewed',  {'sku':1, 'ff':'hhhl'}]
                    }
                }
    )
    _, _, result = client.get(key)
    pp = pprint.PrettyPrinter(indent=4)
    pp.pprint(result)

    # Try using a context for a map operation
    ctx = cdt_ctx.cdt_ctx_map_index(0)
    ops = [
        map_operations.map_get_by_key(
            bin_name, "fav", aerospike.MAP_RETURN_VALUE, ctx
        )
    ]
    print(client.operate(key, ops))
    client.close()
except Exception as e:
    print(e)
    sys.exit()

Hi @Pradeep_Banarva, thanks for posting!

cdt_ctx objects need to be supplied to operations as lists. Changing ctx = cdt_ctx.cdt_ctx_map_index(0) to ctx = [cdt_ctx.cdt_ctx_map_index(0)] will solve the " ‘Failed to convert ctx’ " error. I have modified your script to fetch a few nested values, hopefully these examples prove useful.

import sys
from datetime import datetime
import pprint
import aerospike
from aerospike_helpers import cdt_ctx
from aerospike_helpers.operations import map_operations
from aerospike_helpers.operations import list_operations

client = aerospike.Client({"hosts": ["0.0.0.0"]})
try:
    print(datetime.now())
    client.connect()
    key = ("test", "chat_room_2", "userid")
    # bin_name should be the name of the bin that contains the nested CDT
    bin_name = "bin1"
    client.put(key, 
                {"bin1":
                    {
                        1523474230000: ['fav',     {'sku':1, 'b':2}],
                        1523474231001: ['comment', {'sku':2, 'b':22}],
                        1523474236006: ['viewed',  {'foo':'bar', 'sku':3, 'zz':'top'}],
                        1523474235005: ['comment', {'sku':1, 'c':1234}],
                        1523474233003: ['viewed',  {'sku':3, 'z':26}],
                        1523474234004: ['viewed',  {'sku':1, 'ff':'hhhl'}]
                    }
                }
    )
    _, _, result = client.get(key)
    pp = pprint.PrettyPrinter(indent=4)
    pp.pprint(result)

    # Use a list op to retrieve nested map '{'sku':1, 'b':2}' from list '['fav',     {'sku':1, 'b':2}]'.
    ctx = [cdt_ctx.cdt_ctx_map_index(0)]
    ops = [
        list_operations.list_get_by_index(
            bin_name, 1, aerospike.LIST_RETURN_VALUE, ctx
        )
    ]
    _, _, res = client.operate(key, ops)
    print(res)

    # Example 2: Use a map op to retrieve key 'ff' from nested map '{'sku':1, 'ff':'hhhl'}'.
    ctx = [cdt_ctx.cdt_ctx_map_key(1523474234004), cdt_ctx.cdt_ctx_list_index(1)]
    ops = [
        map_operations.map_get_by_key(
            bin_name, 'ff', aerospike.LIST_RETURN_VALUE, ctx
        )
    ]
    _, _, res = client.operate(key, ops)
    print(res)

    client.close()
except Exception as e:
    print(e)
    sys.exit()

For more information please see. https://aerospike-python-client.readthedocs.io/en/latest/aerospike_helpers.cdt_ctx.html https://aerospike-python-client.readthedocs.io/en/latest/aerospike_helpers.html#module-aerospike_helpers

1 Like

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.