cassandra-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrew Tolbert <>
Subject Re: How to define blob column in Java?
Date Sun, 11 Sep 2016 18:53:48 GMT
Hi Alexandr,

I am assuming you are referring to the @Table annotation in the mapping
module in the Datastax Java Driver for Apache Cassandra (please correct me
if I am wrong).

You can achieve this with any of these three types using a custom codec
but it will work as is using ByteBuffer.  Here's a quick example:

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.utils.Bytes;
import com.datastax.driver.mapping.Mapper;
import com.datastax.driver.mapping.MappingManager;
import com.datastax.driver.mapping.annotations.Column;
import com.datastax.driver.mapping.annotations.PartitionKey;
import com.datastax.driver.mapping.annotations.Table;

import java.nio.ByteBuffer;

public class MapperBlobExample {

    @Table(keyspace="ex", name="blob_ex")
    static class BlobEx {

        int k;

        ByteBuffer b;

        int getK() {
            return k;

        void setK(int k) {
            this.k = k;

        ByteBuffer getB() {
            return b;

        void setB(ByteBuffer b) {
            this.b = b;

    public static void main(String args[]) {
        Cluster cluster =
        try {
            Session session = cluster.connect();
            session.execute("CREATE KEYSPACE IF NOT EXISTS ex WITH
replication = {'class': 'SimpleStrategy', 'replication_factor': 1};");
            session.execute("CREATE TABLE IF NOT EXISTS ex.blob_ex (k int
PRIMARY KEY, b blob);");

            MappingManager manager = new MappingManager(session);
            Mapper<BlobEx> mapper = manager.mapper(BlobEx.class);

            // insert row
            BlobEx ex = new BlobEx();

            // retrieve row
            BlobEx ex0 = mapper.get(0);
        } finally {

There are a few pitfalls around using ByteBuffer with the driver that you
should be aware of (this example
them).  The java-driver-user mailing list
also help.


On Sun, Sep 11, 2016 at 1:50 AM Alexandr Porunov <>

> Hello,
> I am using @Table annotation to define tables in cassandra. How properly I
> need to define blob type in Java? With ByteBuffer, byte[], String?
> Sincerely,
> Alexandr

View raw message