2

hi I wonder if there is a more elegant way of inserting blob instead of converting the byte array into string like:

string strblob = "0x00" + BitConverter.ToString(stream2.GetBuffer()).Replace("-", "");

ISession session = cluster.Connect("abc");

session.Execute(string.Format("insert into events(blob) values ({0})", blobStr));
Alex
  • 21,273
  • 10
  • 61
  • 73
  • As far as I am aware, converting the binary data to hex string format and prefixing "0x" is the only way to load binary data into Cassandra using Datastax. – Alex Oct 26 '15 at 13:34
  • thanks Jaco. it seems a lot of memory waste... /: –  Oct 26 '15 at 13:58
  • also when retrieving data need to takeoff the 0x... //: –  Oct 26 '15 at 13:59
  • Similar question that might help you as well: http://stackoverflow.com/questions/32072373/insert-image-into-cassandra-blob-using-c-sharp – Aaron Oct 26 '15 at 16:13

1 Answers1

3

There is a more elegant and more effective solution which is to use bound statement.

byte[] blob = stream2.GetBuffer();
ISession session = cluster.Connect("abc");

PreparedStatement preparedStatement = session.Prepare("INSERT INTO events(blob) VALUES (?)");
BoundStatement boundStatement = preparedStatement.Bind(blob);

session.Execute(boundStatement);

The idea behind PreparedStatement is to parsed the string query once and reuse it as much as you need with different values. So keep track of your prepared statements.

Adrien Piquerez
  • 1,009
  • 10
  • 11