cassandra-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrew Tolbert <andrew.tolb...@datastax.com>
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
<http://datastax.github.io/java-driver/manual/object_mapper/custom_codecs/>,
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 {

        @PartitionKey
        int k;

        @Column
        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 =
Cluster.builder().addContactPoint("127.0.0.1").build();
        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();
            ex.setK(0);
            ex.setB(Bytes.fromHexString("0xffee"));
            mapper.save(ex);

            // retrieve row
            BlobEx ex0 = mapper.get(0);
            System.out.println(Bytes.toHexString(ex0.getB()));
        } finally {
            cluster.close();
        }
    }
}

There are a few pitfalls around using ByteBuffer with the driver that you
should be aware of (this example
<https://github.com/datastax/java-driver/blob/3.0/driver-examples/src/main/java/com/datastax/driver/examples/datatypes/Blobs.java>
covers
them).  The java-driver-user mailing list
<https://groups.google.com/a/lists.datastax.com/forum/#!forum/java-driver-user>
can
also help.

Thanks!
Andy

On Sun, Sep 11, 2016 at 1:50 AM Alexandr Porunov <alexandr.porunov@gmail.com>
wrote:

> 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
>

Mime
View raw message