0
votes

I am unable to upsert a row using the datastax driver.

The data in the Cassandra table is stored like follows:

tag        | partition_info
------------+--------------------------------------------------
 sometag | {{year: 2018, month: 1}, {year: 2018, month: 2}}

tag is primary key and partition_info is a UDT

CREATE TYPE codingjedi.tag_partitions (
    year bigint,
    month bigint
);

I want that if a tag doesn't exist then it gets created. If tag exists then the new udt value gets appended to old one. I suppose I cannot use insert as it overrides previous value i.e. this will not work

QueryBuilder.insertInto(tableName).value("tag",model.tag)
  .value("partition_info",setAsJavaSet(Set(partitionsInfo)))

I am trying to use update but it isn't working. Datastax driver gives error java.lang.IllegalArgumentException for following query

QueryBuilder.update(tableName).`with`(QueryBuilder.append("partition_info",setAsJavaSet(Set(partitionsInfo)))) 
  .where(QueryBuilder.eq("tag", id.tag))

I tried using add and append for primary key but but got the error PRIMARY KEY part tag found in SET part

QueryBuilder.update(tableName).`with`(QueryBuilder.add("tag",id.tag))
      .and(QueryBuilder.append("partition_info",setAsJavaSet(Set(partitionsInfo))))           .where(QueryBuilder.eq("tag", id.tag))
1

1 Answers

2
votes

You're using the incorrect operation in your update statement - you're using append, but it's used to append data to columns of list types. You can use instead either add if you're adding a single value (your case, so you wont even need to wrap data into Set explicitly), or addAll if you're adding multiple values.

QueryBuilder.update(tableName)
  .`with`(QueryBuilder.add("partition_info", partitionsInfo))
  .where(QueryBuilder.eq("tag", id.tag))