@Test public void testUpdate() throws Exception { insertRow(); final Map<String, String> input = new HashMap<String, String>(); input.put("field0", "new-value1"); input.put("field1", "new-value2"); final Status status = client.update(TABLE, DEFAULT_ROW_KEY, StringByteIterator.getByteIteratorMap(input)); assertThat(status, is(Status.OK)); final Select selectStmt = QueryBuilder.select("field0", "field1") .from(TABLE) .where(QueryBuilder.eq(CassandraCQLClient.YCSB_KEY, DEFAULT_ROW_KEY)) .limit(1); final ResultSet rs = session.execute(selectStmt); final Row row = rs.one(); assertThat(row, notNullValue()); assertThat(rs.isExhausted(), is(true)); assertThat(row.getString("field0"), is("new-value1")); assertThat(row.getString("field1"), is("new-value2")); }
@Override public Status update(String table, String key, Map<String, ByteIterator> values) { try { Set<String> fields = values.keySet(); PreparedStatement stmt = updateStmts.get(fields); if (stmt == null) { Update updateStmt = QueryBuilder.update(table); for (String field : fields) { updateStmt.with(QueryBuilder.set(field, QueryBuilder.bindMarker())); } updateStmt.where(QueryBuilder.eq(YCSB_KEY, QueryBuilder.bindMarker())); stmt = session.prepare(updateStmt); stmt.setConsistencyLevel(writeConsistencyLevel); if (trace) { stmt.enableTracing(); } PreparedStatement prevStmt = updateStmts.putIfAbsent(new HashSet(fields), stmt); if (prevStmt != null) { stmt = prevStmt; } } if (logger.isDebugEnabled()) { logger.debug(stmt.getQueryString()); logger.debug("key = {}", key); for (Map.Entry<String, ByteIterator> entry : values.entrySet()) { logger.debug("{} = {}", entry.getKey(), entry.getValue()); } } ColumnDefinitions vars = stmt.getVariables(); BoundStatement boundStmt = stmt.bind(); for (int i = 0; i < vars.size() - 1; i++) { boundStmt.setString(i, values.get(vars.getName(i)).toString()); } boundStmt.setString(vars.size() - 1, key); session.execute(boundStmt); return Status.OK; } catch (Exception e) { logger.error(MessageFormatter.format("Error updating key: {}", key).getMessage(), e); } return Status.ERROR; }
CassandraCQLClient extends DB { @Override public Status update(String table, String key, Map<String, ByteIterator> values) { try { Set<String> fields = values.keySet(); PreparedStatement stmt = updateStmts.get(fields); if (stmt == null) { Update updateStmt = QueryBuilder.update(table); for (String field : fields) { updateStmt.with(QueryBuilder.set(field, QueryBuilder.bindMarker())); } updateStmt.where(QueryBuilder.eq(YCSB_KEY, QueryBuilder.bindMarker())); stmt = session.prepare(updateStmt); stmt.setConsistencyLevel(writeConsistencyLevel); if (trace) { stmt.enableTracing(); } PreparedStatement prevStmt = updateStmts.putIfAbsent(new HashSet(fields), stmt); if (prevStmt != null) { stmt = prevStmt; } } if (logger.isDebugEnabled()) { logger.debug(stmt.getQueryString()); logger.debug("key = {}", key); for (Map.Entry<String, ByteIterator> entry : values.entrySet()) { logger.debug("{} = {}", entry.getKey(), entry.getValue()); } } ColumnDefinitions vars = stmt.getVariables(); BoundStatement boundStmt = stmt.bind(); for (int i = 0; i < vars.size() - 1; i++) { boundStmt.setString(i, values.get(vars.getName(i)).toString()); } boundStmt.setString(vars.size() - 1, key); session.execute(boundStmt); return Status.OK; } catch (Exception e) { logger.error(MessageFormatter.format("Error updating key: {}", key).getMessage(), e); } return Status.ERROR; } }
CassandraCQLClient extends DB { @Override public Status update(String table, String key, Map<String, ByteIterator> values) { try { Set<String> fields = values.keySet(); PreparedStatement stmt = updateStmts.get(fields); if (stmt == null) { Update updateStmt = QueryBuilder.update(table); for (String field : fields) { updateStmt.with(QueryBuilder.set(field, QueryBuilder.bindMarker())); } updateStmt.where(QueryBuilder.eq(YCSB_KEY, QueryBuilder.bindMarker())); stmt = session.prepare(updateStmt); stmt.setConsistencyLevel(writeConsistencyLevel); if (trace) { stmt.enableTracing(); } PreparedStatement prevStmt = updateStmts.putIfAbsent(new HashSet(fields), stmt); if (prevStmt != null) { stmt = prevStmt; } } if (logger.isDebugEnabled()) { logger.debug(stmt.getQueryString()); logger.debug("key = {}", key); for (Map.Entry<String, ByteIterator> entry : values.entrySet()) { logger.debug("{} = {}", entry.getKey(), entry.getValue()); } } ColumnDefinitions vars = stmt.getVariables(); BoundStatement boundStmt = stmt.bind(); for (int i = 0; i < vars.size() - 1; i++) { boundStmt.setString(i, values.get(vars.getName(i)).toString()); } boundStmt.setString(vars.size() - 1, key); session.execute(boundStmt); return Status.OK; } catch (Exception e) { logger.error(MessageFormatter.format("Error updating key: {}", key).getMessage(), e); } return Status.ERROR; }  }
CassandraCQLClient extends DB { @Override public Status update(String table, String key, Map<String, ByteIterator> values) { try { Set<String> fields = values.keySet(); PreparedStatement stmt = updateStmts.get(fields); if (stmt == null) { Update updateStmt = QueryBuilder.update(table); for (String field : fields) { updateStmt.with(QueryBuilder.set(field, QueryBuilder.bindMarker())); } updateStmt.where(QueryBuilder.eq(YCSB_KEY, QueryBuilder.bindMarker())); stmt = session.prepare(updateStmt); stmt.setConsistencyLevel(writeConsistencyLevel); if (trace) { stmt.enableTracing(); } PreparedStatement prevStmt = updateStmts.putIfAbsent(new HashSet(fields), stmt); if (prevStmt != null) { stmt = prevStmt; } } if (logger.isDebugEnabled()) { logger.debug(stmt.getQueryString()); logger.debug("key = {}", key); for (Map.Entry<String, ByteIterator> entry : values.entrySet()) { logger.debug("{} = {}", entry.getKey(), entry.getValue()); } } ColumnDefinitions vars = stmt.getVariables(); BoundStatement boundStmt = stmt.bind(); for (int i = 0; i < vars.size() - 1; i++) { boundStmt.setString(i, values.get(vars.getName(i)).toString()); } boundStmt.setString(vars.size() - 1, key); session.execute(boundStmt); return Status.OK; } catch (Exception e) { logger.error(MessageFormatter.format("Error updating key: {}", key).getMessage(), e); } return Status.ERROR; }  @Override void init(); @Override void cleanup(); @Override Status read(String table, String key, Set<String> fields,
      Map<String, ByteIterator> result); @Override Status scan(String table, String startkey, int recordcount,
      Set<String> fields, Vector<HashMap<String, ByteIterator>> result); @Override Status update(String table, String key, Map<String, ByteIterator> values); @Override Status insert(String table, String key, Map<String, ByteIterator> values); @Override Status delete(String table, String key); }
CassandraCQLClient extends DB { @Override public Status update(String table, String key, Map<String, ByteIterator> values) { try { Set<String> fields = values.keySet(); PreparedStatement stmt = updateStmts.get(fields); if (stmt == null) { Update updateStmt = QueryBuilder.update(table); for (String field : fields) { updateStmt.with(QueryBuilder.set(field, QueryBuilder.bindMarker())); } updateStmt.where(QueryBuilder.eq(YCSB_KEY, QueryBuilder.bindMarker())); stmt = session.prepare(updateStmt); stmt.setConsistencyLevel(writeConsistencyLevel); if (trace) { stmt.enableTracing(); } PreparedStatement prevStmt = updateStmts.putIfAbsent(new HashSet(fields), stmt); if (prevStmt != null) { stmt = prevStmt; } } if (logger.isDebugEnabled()) { logger.debug(stmt.getQueryString()); logger.debug("key = {}", key); for (Map.Entry<String, ByteIterator> entry : values.entrySet()) { logger.debug("{} = {}", entry.getKey(), entry.getValue()); } } ColumnDefinitions vars = stmt.getVariables(); BoundStatement boundStmt = stmt.bind(); for (int i = 0; i < vars.size() - 1; i++) { boundStmt.setString(i, values.get(vars.getName(i)).toString()); } boundStmt.setString(vars.size() - 1, key); session.execute(boundStmt); return Status.OK; } catch (Exception e) { logger.error(MessageFormatter.format("Error updating key: {}", key).getMessage(), e); } return Status.ERROR; }  @Override void init(); @Override void cleanup(); @Override Status read(String table, String key, Set<String> fields,
      Map<String, ByteIterator> result); @Override Status scan(String table, String startkey, int recordcount,
      Set<String> fields, Vector<HashMap<String, ByteIterator>> result); @Override Status update(String table, String key, Map<String, ByteIterator> values); @Override Status insert(String table, String key, Map<String, ByteIterator> values); @Override Status delete(String table, String key); static final String YCSB_KEY; static final String KEYSPACE_PROPERTY; static final String KEYSPACE_PROPERTY_DEFAULT; static final String USERNAME_PROPERTY; static final String PASSWORD_PROPERTY; static final String HOSTS_PROPERTY; static final String PORT_PROPERTY; static final String PORT_PROPERTY_DEFAULT; static final String READ_CONSISTENCY_LEVEL_PROPERTY; static final String READ_CONSISTENCY_LEVEL_PROPERTY_DEFAULT; static final String WRITE_CONSISTENCY_LEVEL_PROPERTY; static final String WRITE_CONSISTENCY_LEVEL_PROPERTY_DEFAULT; static final String MAX_CONNECTIONS_PROPERTY; static final String CORE_CONNECTIONS_PROPERTY; static final String CONNECT_TIMEOUT_MILLIS_PROPERTY; static final String READ_TIMEOUT_MILLIS_PROPERTY; static final String TRACING_PROPERTY; static final String TRACING_PROPERTY_DEFAULT; static final String USE_SSL_CONNECTION; }