Unable to put List of Aerospike Keys inside bin

Hi,

I am trying to maintain a map of object id and List of Aerospike Keys where it is listed. But when I try to publish that to Aerospike I get following exception:

Caused by: java.io.NotSerializableException: com.aerospike.client.Key at java.io.ObjectOutputStream.writeObject0(Unknown Source) at java.io.ObjectOutputStream.writeObject(Unknown Source) at java.util.ArrayList.writeObject(Unknown Source)

Code being used:

		Bin include = new Bin(CacheConstants.BIN_INCLUDE, idToKeysMap);
		client.put(new WritePolicy(), cacheKey, include);

idToKeysMap looks like:

1=[aerospikeKey1, aerospikeKey2,…]

How can we store this info into Aerospike?

Regards, Dragon

Hi Dragon

The Aerospike Key class is not serializable and that is the cause of your error. Here is some code that will do exactly what you want

	public void work() throws Exception {
		List<Map<String, Object>> keys = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < 20; i++){
			Key key = new Key(this.namespace, this.set, "key-to-test-with-"+i);
			keys.add(makeMapFromKey(key));
		}
		
		Key cacheKey = new Key(this.namespace, this.set, "cache-key");
		
		Bin include = new Bin(BIN_INCLUDE, Value.getAsList(keys));
		client.put(new WritePolicy(), cacheKey, include);
	}
	
	private Map<String, Object> makeMapFromKey(Key key){
		Map<String, Object> keyMap = new HashMap<String, Object>(4);
			keyMap.put("namespace", key.namespace);
			keyMap.put("setname", key.setName);
			keyMap.put("userkey", key.userKey);
			keyMap.put("digest",key.digest);
		return keyMap;
	}

But I suspect there is more to your question, can you answer these questions:

  1. Did you read the key digest from Aerospike as part of a query?
  2. Do you already know the namespace, set and key you are trying to store?

Regards Peter

The complete code is at GitHub - helipilot50/aerospike-storing-keys: Storing Keys in Aerospike

Thank you so much. I thought about this approach as well but then I thought to bypassed this by saving my raw object itself and reconstructing keys at receiving end. The function is a util method which can generate the required keys.

I was wondering if there is some inbuilt support. For answers to questions: I did not read the digest and I know the ns and setname as I was building the map while creating the keys for storing the ids.

Thanks once again.

Regards, Dragon

Hi Dragon,

Sorry for the late response…

There is no inbuilt support for storing the raw key information, if you need it you can make it into a map as we discussed previously, but another approach would be to store a subset of the key info in a Bin, i.e. a counter value concatenated on another value.

Ping me if you want to chat about this, my skype is peter.d.milne

1 Like