@PooledConnection public void setColumn( String keyspace, String column_family, String key, JSONObject json, ConsistencyLevel consistency_level, boolean index, long timestamp) throws InvalidRequestException, UnavailableException, TimedOutException, TException, HttpException, IOException { List<Mutation> slice = new ArrayList<Mutation>(); for (Object field : json.keySet()) { String name = (String) field; String value = (String) json.get(name); Column c = new Column(); c.setName(ByteBufferUtil.bytes(name)); c.setValue(ByteBufferUtil.bytes(value)); c.setTimestamp(timestamp); Mutation m = new Mutation(); ColumnOrSuperColumn cc = new ColumnOrSuperColumn(); cc.setColumn(c); m.setColumn_or_supercolumn(cc); slice.add(m); } Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap = new HashMap<ByteBuffer, Map<String, List<Mutation>>>(); Map<String, List<Mutation>> cfMutations = new HashMap<String, List<Mutation>>(); cfMutations.put(column_family, slice); mutationMap.put(ByteBufferUtil.bytes(key), cfMutations); getConnection(keyspace).batch_mutate(mutationMap, consistency_level); if (config.isIndexingEnabled() && index) indexer.index(column_family, key, json); }
public static void insertPost(Post post, String type) { try { String postId = post.getId(); IdNameEntity idNameEntity = post.getFrom(); String userId = idNameEntity.getId(); String userName = idNameEntity.getName(); String commentsCount = post.getComments() == null ? "0" : post.getComments().size() + ""; String likeCount = post.getLikes() == null ? "0" : post.getLikes().size() + ""; String columnFamily = COLUMN_FAMILY_FACEBOOK_POST; String rowKey = type; String superColumn = postId + "-" + userId + "-" + userName; Clock clock = new Clock(System.nanoTime()); Column column = new Column(); String columnName = commentsCount + "-" + likeCount; column.setName(columnName.getBytes(UTF8)); String columnValue = post.getMessage(); if (!StringUtils.isBlank(columnValue)) { column.setValue(columnValue.getBytes(UTF8)); } else { column.setValue("".getBytes(UTF8)); } column.setTimestamp(clock.timestamp); cassandra.insertSuperColumn(columnFamily, rowKey, superColumn, column); } catch (UnsupportedEncodingException e) { logger.error("[Info: encoding invalid] - [Error: {}]", e.toString()); } }
/** * Insert a record in the database. Any field/value pairs in the specified values HashMap will be * written into the record with the specified record key. * * @param table The name of the table * @param key The record key of the record to insert. * @param values A HashMap of field/value pairs to insert in the record * @return Zero on success, a non-zero error code on error */ public int insert(String table, String key, HashMap<String, ByteIterator> values) { if (!_table.equals(table)) { try { client.set_keyspace(table); _table = table; } catch (Exception e) { e.printStackTrace(); e.printStackTrace(System.out); return Error; } } for (int i = 0; i < OperationRetries; i++) { if (_debug) { System.out.println("Inserting key: " + key); } try { ByteBuffer wrappedKey = ByteBuffer.wrap(key.getBytes("UTF-8")); ColumnOrSuperColumn column; for (Map.Entry<String, ByteIterator> entry : values.entrySet()) { column = new ColumnOrSuperColumn(); Column subColumn = new Column(ByteBuffer.wrap(entry.getKey().getBytes("UTF-8"))); subColumn.setValue(ByteBuffer.wrap(entry.getValue().toArray())); subColumn.setTimestamp(System.currentTimeMillis()); column.setColumn(subColumn); mutations.add(new Mutation().setColumn_or_supercolumn(column)); } mutationMap.put(column_family, mutations); record.put(wrappedKey, mutationMap); client.batch_mutate(record, ConsistencyLevel.ONE); mutations.clear(); mutationMap.clear(); record.clear(); return Ok; } catch (Exception e) { errorexception = e; } try { Thread.sleep(500); } catch (InterruptedException e) { } } errorexception.printStackTrace(); errorexception.printStackTrace(System.out); return Error; }
private static Mutation getMutation(String colName, String value) { Column c = new Column(); c.setName(ByteBufferUtil.bytes(colName)); c.setValue(ByteBufferUtil.bytes(value)); c.setTimestamp(System.currentTimeMillis()); Mutation m = new Mutation(); m.setColumn_or_supercolumn(new ColumnOrSuperColumn()); m.column_or_supercolumn.setColumn(c); return m; }
@Override public ColumnListMutation<C> putEmptyColumn(C columnName, Integer ttl) { Column column = new Column(); column.setName(columnSerializer.toByteBuffer(columnName)); column.setValue(ThriftUtils.EMPTY_BYTE_BUFFER); column.setTimestamp(timestamp); if (ttl != null) column.setTtl(ttl); else if (defaultTtl != null) column.setTtl(defaultTtl); // 2. Create a mutation and append to the mutation list. Mutation mutation = new Mutation(); mutation.setColumn_or_supercolumn(new ColumnOrSuperColumn().setColumn(column)); mutationList.add(mutation); return this; }
@Override public <V> ColumnListMutation<C> putColumn( C columnName, V value, Serializer<V> valueSerializer, Integer ttl) { // 1. Set up the column with all the data Column column = new Column(); column.setName(columnSerializer.toByteBuffer(columnName)); column.setValue(valueSerializer.toByteBuffer(value)); column.setTimestamp(timestamp); if (ttl != null) column.setTtl(ttl); else if (defaultTtl != null) column.setTtl(defaultTtl); // 2. Create a mutation and append to the mutation list. Mutation mutation = new Mutation(); mutation.setColumn_or_supercolumn(new ColumnOrSuperColumn().setColumn(column)); mutationList.add(mutation); return this; }
public static Column createCol(String name, String value) { Column result = new Column(); long time = System.currentTimeMillis(); try { result = new Column(toByteBuffer(name)); if ((name.equals(Status.colname_createdate) || name.equals(Tag.name_col_cdate) || name.equals(Status.colname_modifydate) || name.equals(Tag.name_col_mdate)) && "".equals(value)) { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss"); result.setValue(toByteBuffer(sdf.format(cal.getTime()))); } else result.setValue(toByteBuffer(value)); result.setTimestamp(time); } catch (UnsupportedEncodingException ex) { Logger.getLogger(GeneralHandling.class.getName()).log(Level.SEVERE, null, ex); } return result; }
public static void insertPostPopular(Post post, String type, Integer position) { try { String postId = post.getId(); IdNameEntity idNameEntity = post.getFrom(); String userId = idNameEntity.getId(); String userName = idNameEntity.getName(); String columnFamily = COLUMN_FAMILY_FACEBOOK_POST_POPULAR; String rowKey = (type + "-" + position); Clock clock = new Clock(System.nanoTime()); Column column = new Column(); String columnName = (postId + "-" + userId + "-" + userName); column.setName(columnName.getBytes(UTF8)); String columnValue = post.getMessage(); column.setValue(columnValue.getBytes(UTF8)); column.setTimestamp(clock.timestamp); cassandra.insertColumn(columnFamily, rowKey, column); } catch (UnsupportedEncodingException e) { logger.error("[Info: encoding invalid] - [Error: {}]", e.toString()); } }