简体   繁体   中英

Issue while fetching data from Aerospike DB Stored in Raw Format

Can anyone please suggest how I can retrieve data stored in Raw format in Aerospike using Aerospike client, I'm able to fetch all other sort of data from aerospike, but not the data stored in raw format. Equivalent functions in C language are present, but not able to found its equivalence in Java.
C-Language function name for setting data in Aerospike as_record_set_raw
C-Language function name for getting data in Aerospike as_record_get_bytes

While fetching data from Java, using Aerospike client, I'm getting {msg=[B@6d3163a6, length=10} , while the message is stored in raw format.

Expected Output:- {msg=66 65 78 44, length=10}
Already checked the encoding part of workspace

PS:- getBytes() not working, getting following exception:- java.lang.ClassCastException: class [B cannot be cast to class java.lang.Long ([B and java.lang.Long are in module java.base of loader 'bootstrap')

code

        userKey = new Key("test", "testset", key);
        userRecord = client.get(null, userKey);
        if (userRecord != null) {
            System.out.println("\nINFO: User record read successfully! Here are the details:\n");
        System.out.println("msg:   " + userRecord.getValue("msg")+ "\n");
        }

There is no getBytes() method that I can see on the Record object, in the recent javadocs at least.

I see the C function as_set_record_raw() is documented as Set specified bin's value to an NULL terminated string . So I would suggest using the com.aerospike.client.Record.getString() java method and use the String getBytes() method as needed to get the raw byte array.

Edit: I'm assuming here that you are trying to read back data through the Java client that was written using the C client.

Raw data is literally unencoded bytes stored in the database. For me, the easiest way to work with is to cast the data into a byte array and convert the byte array into a string to use it:

userKey = new Key("test", "testset", key);
userRecord = client.get(null, userKey);
if (userRecord != null) {
    byte[] myByteArray = (byte[])userRecord.getValue(msg);
    System.out.println("\nINFO: User record read successfully! Here are the details:\n");
    System.out.println("msg:   " + Arrays.toString(myByteArray) + "\n");
}

If you want to see some functioning examples of working with this type of data, in case I borked your code, check out this code example in with the Aerospike Java Client code, and the java-intro_to_transactions.ipynb notebook in Aerospike's Tutorial Notebooks .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM