napster
napster

Reputation: 193

Cassandra - Update Set type column

We have a table in cassandra with below structure:

cities_in_state(state TEXT, zip TEXT, cities SET<TEXT>, PRIMARY KEY ((zip, 
state)))

I need to upsert the value of cities for the state with the java driver, Have this code:

BoundStatement bound = session().prepare("UPDATE cities_in_state SET cities 
= cities + ? WHERE zip = ? and state = ?").bind();
bound.setSet(1, cities);
bound.setString(2, "ZIP1");
bound.setString(3, "state1");

which is giving error like "HashSet can not be cast to String" And i should always need to add more cities to the existing cities. So how would i append the set column in cassandra with bound parameters.

Upvotes: 1

Views: 156

Answers (1)

Alex Ott
Alex Ott

Reputation: 87349

Following piece of code works just fine:

PreparedStatement prepared = session.prepare("UPDATE test.st SET cities = cities + ? WHERE zip = ? and state = ?");
BoundStatement bound = prepared.bind(Collections.singleton("t2"), "2", "1");
session.execute(bound);

Your problem is that you're starting counting with 1, while the Java driver uses 0-based indexes. Following piece of code works just find if I decrease every index by 1:

BoundStatement bound2 = prepared.bind();
bound2.setSet(0, Collections.singleton("t3"));
bound2.setString(1, "2");
bound2.setString(2, "1");
session.execute(bound2);

Upvotes: 1

Related Questions