You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cassandra.apache.org by Alexandr Porunov <al...@gmail.com> on 2016/09/11 06:50:00 UTC
How to define blob column in Java?
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
Re: How to define blob column in Java?
Posted by Alexandr Porunov <al...@gmail.com>.
Hello Andy,
Thank you very much!
Sincerely,
Alexandr
On Sun, Sep 11, 2016 at 9:53 PM, Andrew Tolbert <andrew.tolbert@datastax.com
> wrote:
> 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
>>
>
Re: How to define blob column in Java?
Posted by Andrew Tolbert <an...@datastax.com>.
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 <al...@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
>