Search code examples
javadatabaseaerospike

Store data into aerospike


I have some data which is to be stored into aerospike against a column

Suppose the incomming data is

["A", 1]

Now the first question is how to hold this data in Java.

I tried this.

ArrayList value = new ArrayList();
value.add(new String("A"));
value.add(new Integer(2));

When i try to write this data to aerospike using

AerospikeClient client = client.put(new WritePolicy(), 
         new Key("namespace", "set", "test"), 
         new Bin("binName", value) );

Then if i use AQL to query then i am seeing

| AC ED 00 05 73 72 00 13 6A 61 76 61 2E 75 74 69 6C 2E 41 72 72 61 79 4C 69 73 74 78 81 D2 1D 99 C7 61 9D 03 00 01 49 00 04 73 69 7A 65 78 70 00 00 00 02 77 04 00 00 00 02 73 72 00 11 6A 61 76 61 2E 6C 61 6E 67 2E 49 6E 74 65 67 65 72 12 E2 A0 A4 F7 81 87 |

Some HexaDecimal numbers

but when i try to store the Data into Aerospike using

AerospikeClient client = client.put(new WritePolicy(), 
         new Key("namespace", "set", "test"), 
         new Bin("binName", Value.getAsList(value)) );

Then firing query through AQL gives me

["A",1]

Which is and seems like the intended behaviour but when i use the Aerospike client to fetch the values and check their types

List<Object> ret = (List<Object>) client.get(new Policy(), key, "test").getValue("binName");

    if(ret.get(0) instanceof Long){
        System.out.println("Got instance of long");
    }

Then i can see the print statement though Initially i sent Integer data.

Why is this happening, and can anyone tell me the any alternate solution to save an incomming data into aerospike say the data is

["A",1]

PS:Please support your answer with small code snippit

FOUND SOME INFO ON GITHUB

In reference to this link there is some function

which i am copy/pasting below

/**
 * Write/Read ArrayList<Object> directly instead of relying on java serializer.
 */
private void testListComplex(AerospikeClient client, Parameters params) throws Exception {
    console.info("Read/Write ArrayList<Object>");
    Key key = new Key(params.namespace, params.set, "listkey2");
    client.delete(params.writePolicy, key);

    byte[] blob = new byte[] {3, 52, 125};      
    ArrayList<Object> list = new ArrayList<Object>();
    list.add("string1");
    list.add(2);
    list.add(blob);

    Bin bin = new Bin(params.getBinName("listbin2"), list);
    client.put(params.writePolicy, key, bin);

    Record record = client.get(params.policy, key, bin.name);
    List<?> receivedList = (List<?>) record.getValue(bin.name);

    validateSize(3, receivedList.size());
    validate("string1", receivedList.get(0));
    // Server convert numbers to long, so must expect long.
    validate(2L, receivedList.get(1)); 
    validate(blob, (byte[])receivedList.get(2));
    
    console.info("Read/Write ArrayList<Object> successful.");
}

There is a comment that server converts number to long

Now i have a question. So does it mean for this type of Case integer cannot be stored?


Solution

  • If you check the Aerospike datatypes, you'll see that they only support 64-bit integers, which are long types in Java.

    If you don't need to access this data on the server through Lua UDF scripts, then you can just save it as serialized blob data. The java driver already supports native serialization which is what you did in the first attempt. The AQL is just showing you the serialized bytes but you can read it back in the java client just fine.

    Or you can store it as a json serialized string so that it's more compatible with other language drivers that you might use in the future.