BigTable - Can you store boolean values?

Viewed 754

Is it possible to store boolean values?

I tried storing a True value in BigTable, and got the error message:

TypeError: True could not be converted to bytes

Looking at the code in GitHub, the function _to_bytes was used, and it throws an error if it cannot be converted to bytes.

Is there a recommended way to store boolean data? Or should I just cast True/False and then remember to convert the values back to Boolean when I retrieve the data?

2 Answers

This is how HBase converts booleans to bytes in Bytes.java:

  public static byte [] toBytes(final boolean b) {
    return new byte[] { b ? (byte) -1 : (byte) 0 };
  }

The Java cloud-bigtable-client encourages that java users use that class for converting primitives to values. There probably should be a similar class in other libraries to help encourage common conversions.

You cannot store a Boolean type in BigTable using Python.

This is what the documentation says about the data types that can be stored in BigTable:

Cloud Bigtable treats all data as raw byte strings for most purposes

So it seems that the best option you have is, as you said, to cast "True/False" as Strings and convert the data back to Boolean when you get it.

Related