Search code examples
c++cassandrablobcql

Blob data type in Cassandra


I don't understand much about using blob data type. I'm new in Cassandra and have just started working with Cassandra's C++ API. Can the contents of a C++ structure be stored in Cassandra's Blob. If yes, then how. For example, to store bigint as blob we've bigintAsBlob().


Solution

  • Given a struct:

    struct my_struct {
        int field1;
        double field2;
    };
    

    And a prepared statement (I hope you use prepared statements), you can do this:

    my_struct s;
    std::unique_ptr<char[]> buffer(new char[sizeof(my_struct)]);
    memcpy(buffer.get(), &s, sizeof(my_struct));
    cass_statement_bind_bytes(statement, index, buffer.get(), sizeof(my_struct))
    

    Where statement is a prepared statement and index is the index of the blob field in your schema. Note that your struct has to be a POD with no members relying on dynamic memory (e.g. no char*), otherwise this won't work. I didn't test this but I hope it helps.