CompanyAugust 24, 2014

Introducing DataStax Java Driver 2.1

Oliver Michallat
Oliver Michallat
Introducing DataStax Java Driver 2.1
CREATE TYPE address (
    street text,
    city text,
    zip int
CREATE TABLE user_profiles (
    email text PRIMARY KEY,
    address address
Row row = session.execute("SELECT * FROM user_profiles").one();
UDTValue address = row.getUDTValue("address");
String street = address.getString("street"); // by field name
int zip       = address.getInt(2);           // by index
// Get the type from an existing value:
UserType addressType = address.getType();
// Or from the cluster metadata:
UserType addressType = cluster.getMetadata().getKeyspace("ks").getUserType("address");
UDTValue address2 = addressType.newValue()
                               .setString("street", "1600 Pennsylvania Ave NW")
                               .setString("city", "Washington")
                               .setInt("zip", 20500);
session.execute("INSERT INTO user_profiles (email, address) VALUES (?, ?)",
                "", address2);
CREATE TABLE points_of_interest (
    id int PRIMARY KEY,
    name text,
    coordinates tuple<float,float>
Row row = session.execute("SELECT * FROM points_of_interest").one();
TupleValue coordinates = row.getTupleValue("coordinates");
float latitude  = coordinates.getFloat(0);
float longitude = coordinates.getFloat(1);
TupleType coordinatesType = TupleType.of(DataType.cfloat(), DataType.cfloat());
TupleValue newCoordinates = coordinatesType.newValue(48.858222F, 2.2945F);
@UDT(keyspace = "ks", name = "address")
public class Address {
    private String street;
    private String city;
    private int zip;
    // getters and setters omitted...
@Table(keyspace = "ks", name = "user_profiles")
public class UserProfile {
    private String email;
    private Address address;
    // getters and setters omitted...
MappingManager manager = new MappingManager(session);
Mapper mapper = manager.mapper(UserProfile.class);
UserProfile myProfile = mapper.get("");
ListenableFuture saveFuture = mapper.saveAsync(anotherProfile);
interface ProfileAccessor {
    @Query("SELECT * FROM user_profiles LIMIT :max")
    Result firstN(@Param("max") int limit);
ProfileAccessor accessor = manager.createAccessor(ProfileAccessor.class);
Result profiles = accessor.firstN(10);
// Result is like ResultSet, but specialized for a mapped class:
for (UserProfile profile : profiles) {

NoSQL and Vector DB
for Generative AI,
Instantly, At Scale

Vector search capabilities on Astra DB enable complex, context-sensitive searches across diverse data formats for use in Generative AI applications, powered by Apache Cassandra®.