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 (?, ?)",
                "xyz@example.com", 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 {
    @PartitionKey
    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("xyz@example.com");
ListenableFuture saveFuture = mapper.saveAsync(anotherProfile);
mapper.delete("xyz@example.com");
@Accessor
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) {
    System.out.println(profile.getAddress().getZip());
}
Share

One-stop Data API for Production GenAI

Astra DB gives JavaScript developers a complete data API and out-of-the-box integrations that make it easier to build production RAG apps with high relevancy and low latency.