Search code examples
base64bigtable

How to store long number as 64 bit encoded value in Bigtable?


When using readModifyWrite method to increment a number it throws exception the value must be stored as 64 bit encoded signed integer in Java. How to store it as 64 bit big endian? I tried reversebytes method and Base64 encoding but didn't help.


Solution

  • Found thru' Google support :

    long number = 456712L; 
        ByteString encodedLong = ByteString.copyFrom(
            ByteBuffer.allocate(Long.BYTES).putLong(number).array()
        );
        
        Mutation mutation = Mutation.create(tableId, rowKey)
            .setCell(columnFamily, columnQualifier, encodedLong);
        dataClient.mutateRow(mutation);
    
        Row row = dataClient.readRow(tableId, rowKey);
        if (row != null && row.getCells(columnFamily, columnQualifier).size() > 0) {
            ByteString longBytes = row.getCells(columnFamily, columnQualifier).get(0).getValue();
            long longValue = ByteBuffer.wrap(longBytes.toByteArray()).getLong();
    
            System.out.println("Retrieved long value: " + longValue); 
        } else {
            System.out.println("Long value not found for row key: " + rowKey);
        }