Skip to content

Instantly share code, notes, and snippets.

@devsprint
Last active November 3, 2020 02:51
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save devsprint/5363023 to your computer and use it in GitHub Desktop.
Save devsprint/5363023 to your computer and use it in GitHub Desktop.
Write a blob content to Cassandra using datastax\java-driver
private static String WRITE_STATEMENT = "INSERT INTO avatars (id, image_type, avatar) VALUES (?,?,?);";
private final BoundStatement writeStatement=writeStatement = session.prepare(WRITE_STATEMENT)
.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM).bind();
try {
BoundStatement stmt = driver.getWriteStatement();
stmt.enableTracing();
stmt.setLong("id", accountId);
stmt.setString("image_type", image.getType());
stmt.setBytes("avatar", ByteBuffer.wrap(image.getBytes()));
ResultSet result = driver.execute(stmt);
if (result.getQueryTrace() == null) {
LOG.error("Null result set!");
} else {
if (result.getQueryTrace().getCoordinator() != null) {
LOG.info("UPLOAD COORDINATOR: {}", result.getQueryTrace()
.getCoordinator().getCanonicalHostName());
} else {
LOG.error("Null COORDINATOR!");
}
}
} catch (NoHostAvailableException e) {
LOG.error("Could not prepare the statement.", e);
throw new StorageUnavailableException(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment