Illustration Image

Cassandra.Link

The best knowledge base on Apache Cassandra®

Helping platform leaders, architects, engineers, and operators build scalable real time data platforms.

10/23/2020

Reading time:1 min

DataStax-Examples/blobs-java

by DataStax-Examples

Binary Large Objects ( Blobs ) can be useful when the data doesn't fit into a standardCassandra data type. Some examples of common Blob usage are for images or largefiles. Be careful of the size of your blobs, though: it's best to break them up so that each blob cell is 1 MB or less.Contributor(s): Olivier Michallat, Tomasz Lelek - derived fromhereObjectivesDemonstrate how to write and read these blob data type columns in a Java applicationProject LayoutApp.java - The main application fileHow this Sample WorksBy default, the Java DataStax Driver maps blob types to java.nio.ByteBuffer.The ByteBuffer API is a bit tricky to use at times, so we will show common pitfalls as well. We strongly recommend that you read thejava.nio.Buffer andByteBuffer API docs and become familiar with the capacity, limit, andposition properties. This tutorial is also useful.Setup and RunningPrerequisitesJDK 14A Cassandra cluster is running and accessible through the contacts points and data center identified in application.confRunningBuildingAt the project root levelmvn clean packageThis builds the JAR file located at target/storing-and-retrieving-blobs-1.0.jarRun the programjava -jar target/storing-and-retrieving-blobs-1.0.jarThis will produce output similar to the following:Writing retrieved buffer to /var/folders/v5/f93lqcsx2fj4__41f7l5w5j80000gn/T/blob6758286736353817789.png

Illustration Image

Binary Large Objects ( Blobs ) can be useful when the data doesn't fit into a standard Cassandra data type. Some examples of common Blob usage are for images or large files. Be careful of the size of your blobs, though: it's best to break them up so that each blob cell is 1 MB or less.

Contributor(s): Olivier Michallat, Tomasz Lelek - derived from here

Objectives

  • Demonstrate how to write and read these blob data type columns in a Java application

Project Layout

How this Sample Works

By default, the Java DataStax Driver maps blob types to java.nio.ByteBuffer. The ByteBuffer API is a bit tricky to use at times, so we will show common pitfalls as well. We strongly recommend that you read the java.nio.Buffer and ByteBuffer API docs and become familiar with the capacity, limit, and position properties. This tutorial is also useful.

Setup and Running

Prerequisites

  • JDK 14
  • A Cassandra cluster is running and accessible through the contacts points and data center identified in application.conf

Running

Building

At the project root level

mvn clean package

This builds the JAR file located at target/storing-and-retrieving-blobs-1.0.jar

Run the program

java -jar target/storing-and-retrieving-blobs-1.0.jar

This will produce output similar to the following:

Writing retrieved buffer to /var/folders/v5/f93lqcsx2fj4__41f7l5w5j80000gn/T/blob6758286736353817789.png

Related Articles

spring
rest
api

GitHub - DataStax-Examples/spring-data-starter: ⚡️ A sample Spring Data Cassandra REST API

John Doe

2/14/2024

Checkout Planet Cassandra

Claim Your Free Planet Cassandra Contributor T-shirt!

Make your contribution and score a FREE Planet Cassandra Contributor T-Shirt! 
We value our incredible Cassandra community, and we want to express our gratitude by sending an exclusive Planet Cassandra Contributor T-Shirt you can wear with pride.

Join Our Newsletter!

Sign up below to receive email updates and see what's going on with our company

Explore Related Topics

AllKafkaSparkScyllaSStableKubernetesApiGithubGraphQl

Explore Further

examples