Skip to content

Instantly share code, notes, and snippets.

@olim7t
Last active April 5, 2016 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olim7t/e056ef6915d038c714acb4a842f245d5 to your computer and use it in GitHub Desktop.
Save olim7t/e056ef6915d038c714acb4a842f245d5 to your computer and use it in GitHub Desktop.
Test program to debug blob reading issue
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;
public class Read2MbBlobs {
public static void main(String[] args) {
Cluster cluster = null;
try {
cluster = Cluster.builder()
.addContactPoint("127.0.0.1")
.build();
Session session = cluster.connect();
insert(session);
select(session);
} finally {
if (cluster != null) cluster.close();
}
}
private static void select(Session session) {
for (int i = 0; i < 20; i++) {
long start = System.nanoTime();
Row row = session.execute("select * from test.foo where k = ?", i).one();
long duration = System.nanoTime() - start;
System.out.printf("selected %d (containing %d bytes blob) in %d ms%n",
i,
row.getBytes("b").capacity(),
TimeUnit.NANOSECONDS.toMillis(duration));
}
}
private static void insert(Session session) {
session.execute("create table if not exists test.foo(k int primary key, b blob)");
PreparedStatement pst = session.prepare("insert into test.foo(k,b) values(?,?)");
for (int i = 0; i < 20; i++) {
session.execute(pst.bind(i, BLOB_2MB));
System.out.println("inserted" + i);
}
}
private static final ByteBuffer BLOB_2MB = blobWithZeroes(2 * 1024 * 1024);
private static ByteBuffer blobWithZeroes(int size) {
ByteBuffer b = ByteBuffer.allocate(size);
b.position(b.capacity());
b.flip();
return b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment