Converting cassandra blob to string

I have an old column family that has a column named "value" that has been defined as a blob datatype. This column usually contains two numbers separated by an underscore, such as "421_2".

When im using the python datastax driver and executing the request, the results are returned with this field parsed as a string:

In [21]: session.execute(q)
Out[21]: 
[Row(column1=4776015, value='145_0'),
 Row(column1=4891778, value='114_0'),
 Row(column1=4891780, value='195_0'),
 Row(column1=4893662, value='105_0'),
 Row(column1=4893664, value='115_0'),
 Row(column1=4898493, value='168_0'),
 Row(column1=4945162, value='148_0'),
 Row(column1=4945163, value='131_0'),
 Row(column1=4945168, value='125_0'),
 Row(column1=4945169, value='211_0'),
 Row(column1=4998426, value='463_0')]

      

When I use the java driver, I return the com.datastax.driver.core.Row object. When I try to read a value field, for example row.getString("value")

, I get the expected one InvalidTypeException: Column value is of type blob

. It seems like the only way to read the field is through row.getBytes("value")

and then return the object java.nio.HeapByteBuffer

.

The problem is that I cannot convert this object to string in an easy way. Googling gave two answers from 2012 that suggest the following:

String string_value = new String(result.getBytes("value"), "UTF-8");

      

But such a String constructor no longer exists. So my questions are:

  • How can I convert HeapByteBuffer to string?
  • Why did the python driver convert blob easily and java not?

Side note: I could debug the python driver, but at present this seems like too much work for something that should be trivial. (and the fact that no one asked about this suggests me to skip something simple here.)

+3


source to share


4 answers


Another easy way is to change the CQL statement.

select column1, blobastext(value) from YourTable where key = xxx

      



The second column will be of type String.

+4


source


You can also directly access the Java driver serializers. This way you don't have to deal with low level parts as well for other types.

Driver 2.0.x:

String s = (String)DataType.text().deserialize(byteBuffer);

      

Driver 2.1.x:



ProtocolVersion protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion();
String s = (String)DataType.text().deserialize(byteBuffer, protocolVersion);

      

Driver 2.2.x:

ProtocolVersion protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion();
String s = TypeCodec.VarcharCodec.instance.deserialize(byteBuffer, protocolVersion);

      

+7


source


For version 3.1.4 of the datastax java driver, the following converts the blob to a string:

ProtocolVersion proto = cluster.getConfiguration().getProtocolOptions().getProtocolVersion();

String deserialize = TypeCodec.varchar().deserialize(row.getBytes(i), proto);

      

+1


source


1.) Converting from byte buffer to Java is discussed in this answer .

2.) Assuming you are using Python 2, it returns as a string in Python because str is a binary type.

0


source







All Articles