Question

I am trying to insert byte array into Blob data type in my Cassandra table.. I am using Datastax Java driver. Below is my code -

for (Map.Entry<String, byte[]> entry : attributes.entrySet()) {
    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());

    String cql = "insert into test_data (user_id, name, value) values ('"+userId+"', '"+entry.getKey()+"', '"+entry.getValue()+"');";

    System.out.println(cql);

    CassandraDatastaxConnection.getInstance();

    CassandraDatastaxConnection.getSession().execute(cql);

}

And this is the exception I am getting back -

InvalidQueryException: cannot parse '[B@50908fa9' as hex bytes

I guess the problem is, the way I am making my above cql.. Something is missing for sure...

I have created the table like this -

create table test_data (user_id text, name text, value blob, primary key (user_id, name));

Can anybody help me? Thanks...

Était-ce utile?

La solution

The problem is that when you append the byte array to the String it calls toString on the byte[] which prints the unhelpful pointer you are seeing. You need to manually convert it to a String for your data type. In your case you are using a blob, so you need to convert to a hex string.

This question has code for converting the byte[] to String:

How to convert a byte array to a hex string in Java?

You can use one of those functions and prepend '0x' to it. Then you should have a valid String for your blob.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top