Search code examples
javacassandracqlcql3

QueryBuilder for user defined type in cassandra driver 3.0 (Java)


I have a schema as follows

CREATE TABLE location_by_name(
id uuid,
coordinates frozen<coords>,
name text,
primary key (name)
);

CREATE TYPE coords(
longitude double, 
latitude double
);

I am able to insert data using the prepared statement, but I couldn't make it out using the QueryBuilder API,it would be great if anyone could point me in the right direction.

Thanks!


Solution

  • This should do the trick:

    UserType coordsType = cluster.getMetadata()
         .getKeyspace("ks")
         .getUserType("coords");
    UDTValue coordinates = coordsType.newValue()
        .setDouble("longitude", 2.35)
        .setDouble("latitude", 48.853);
    Statement insert = QueryBuilder.insertInto("location_by_name")
        .value("id", id)
        .value("coordinates", coordinates)
        .value("name", name);