/** * Add a new Column <long,byte[]> to a given row in a given column family * * @param columnFamily column family name * @param row row name * @param key long key value * @param value value as a byte array * @param keyspace CassandraKeySpace * @throws CassandraDataAccessException */ public static void addLongByteArrayColumnToRow( String columnFamily, String row, long key, byte[] value, Keyspace keyspace) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't add Data , no keySpace provided "); } if (columnFamily == null || row == null || value == null) { throw new CassandraDataAccessException( "Can't add data with columnFamily = " + columnFamily + " and row=" + row + " key = " + key + " value = " + value); } try { Mutator<String> messageContentMutator = HFactory.createMutator(keyspace, stringSerializer); messageContentMutator.addInsertion( row, columnFamily, HFactory.createColumn(key, value, longSerializer, bytesArraySerializer)); messageContentMutator.execute(); } catch (Exception e) { throw new CassandraDataAccessException( "Error while adding new Column <int,byte[]> to cassandra store", e); } }
/** * Add a Mapping to a Given Row in cassandra column family. Mappings are used as search indexes * * @param columnFamily columnFamilyName * @param row row name * @param cKey key name for the adding column * @param cValue value for the adding column * @param keyspace Cassandra KeySpace * @throws CassandraDataAccessException In case of database access error or data error */ public static void addMappingToRaw( String columnFamily, String row, String cKey, String cValue, Keyspace keyspace) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't add Data , no KeySpace provided "); } if (columnFamily == null || row == null || cKey == null) { throw new CassandraDataAccessException( "Can't add data with columnFamily = " + columnFamily + " and rowName=" + row + " key = " + cKey); } try { Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer); mutator.addInsertion( row, columnFamily, HFactory.createColumn(cKey, cValue.trim(), stringSerializer, stringSerializer)); mutator.execute(); } catch (Exception e) { throw new CassandraDataAccessException("Error while adding a Mapping to row ", e); } }
public void send(EventObject event) throws JSONException { ByteBuffer rowKey = se.toByteBuffer(event.getString("partition_on")); event.put(timestamp_field, System.currentTimeMillis()); mutator.addInsertion( rowKey, keyspace, createColumn(event.getEventName(), event.toString(), se, se)); mutator.execute(); }
/** * Add new Column<long,long> to a given row in a given cassandra column family * * @param columnFamily column family name * @param row row name * @param key long key value of the column * @param value long value of the column * @param keyspace Cassandra KeySpace * @throws CassandraDataAccessException */ public static void addLongContentToRow( String columnFamily, String row, long key, long value, Keyspace keyspace) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't add Data , no keySpace provided "); } if (columnFamily == null || row == null) { throw new CassandraDataAccessException( "Can't add data with columnFamily = " + columnFamily + " and rowName=" + row + " key = " + key); } try { Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer); mutator.insert( row, columnFamily, HFactory.createColumn(key, value, longSerializer, longSerializer)); mutator.execute(); } catch (Exception e) { throw new CassandraDataAccessException("Error while adding long content to a row", e); } }
/** * Add Message to a Given Queue in Cassandra * * @param columnFamily ColumnFamily name * @param queue queue name * @param messageId Message id * @param message message in bytes * @param keyspace Cassandra KeySpace * @throws CassandraDataAccessException In case of database access error */ public static void addMessageToQueue( String columnFamily, String queue, long messageId, byte[] message, Keyspace keyspace) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't add Data , no mutator provided "); } if (columnFamily == null || queue == null || message == null) { throw new CassandraDataAccessException( "Can't add data with columnFamily = " + columnFamily + " and queue=" + queue + " message id = " + messageId + " message = " + message); } try { Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer); mutator.addInsertion( queue.trim(), columnFamily, HFactory.createColumn(messageId, message, longSerializer, bytesArraySerializer)); mutator.execute(); } catch (Exception e) { throw new CassandraDataAccessException("Error while adding message to Queue", e); } }
public static void deleteIntegerColumnFromRow( String columnFamily, String row, Integer key, Keyspace keyspace) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't delete Data , no keyspace provided "); } if (columnFamily == null || row == null) { throw new CassandraDataAccessException( "Can't delete data in columnFamily = " + columnFamily + " and rowName=" + row + " key = " + key); } try { Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer); mutator.addDeletion(row, columnFamily, key, integerSerializer); mutator.execute(); } catch (Exception e) { throw new CassandraDataAccessException("Error while deleting data", e); } }
/** * Write the updated client pointer * * @param queueId * @param consumerId * @param lastReturnedId This is a null safe parameter. If it's null, this won't be written since * it means we didn't read any messages */ protected void writeClientPointer(UUID queueId, UUID consumerId, UUID lastReturnedId) { // nothing to do if (lastReturnedId == null) { return; } // we want to set the timestamp to the value from the time uuid. If this is // not the max time uuid to ever be written // for this consumer, we want this to be discarded to avoid internode race // conditions with clock drift. long colTimestamp = UUIDUtils.getTimestampInMicros(lastReturnedId); Mutator<UUID> mutator = createMutator(ko, ue); if (logger.isDebugEnabled()) { logger.debug( "Writing last client id pointer of '{}' for queue '{}' and consumer '{}' with timestamp '{}", new Object[] {lastReturnedId, queueId, consumerId, colTimestamp}); } mutator.addInsertion( consumerId, CONSUMERS.getColumnFamily(), createColumn(queueId, lastReturnedId, colTimestamp, ue, ue)); mutator.execute(); }
public static void deleteIntegerColumnFromRow( String columnFamily, String row, int key, Mutator<String> mutator, boolean execute) throws CassandraDataAccessException { if (mutator == null) { throw new CassandraDataAccessException("Can't delete Data , no mutator provided "); } if (columnFamily == null || row == null) { throw new CassandraDataAccessException( "Can't delete data in columnFamily = " + columnFamily + " and rowName=" + row + " key = " + key); } try { mutator.addDeletion(row, columnFamily, key, integerSerializer); if (execute) { mutator.execute(); } } catch (Exception e) { throw new CassandraDataAccessException( "Error while deleting " + key + " from " + columnFamily); } }
@Override public Queue updateQueue(String queuePath, Queue queue) { queue.setPath(queuePath); UUID timestampUuid = newTimeUUID(); long timestamp = getTimestampInMicros(timestampUuid); Mutator<ByteBuffer> batch = createMutator(cass.getApplicationKeyspace(applicationId), be); addQueueToMutator(batch, queue, timestamp); try { batchUpdateQueuePropertiesIndexes( batch, queuePath, queue.getUuid(), queue.getProperties(), timestampUuid); } catch (Exception e) { logger.error("Unable to update queue", e); } batch.addInsertion( bytebuffer(queue.getUuid()), QUEUE_PROPERTIES.getColumnFamily(), createColumn(QUEUE_CREATED, timestamp / 1000, Long.MAX_VALUE - timestamp, se, le)); batch.addInsertion( bytebuffer(queue.getUuid()), QUEUE_PROPERTIES.getColumnFamily(), createColumn(QUEUE_MODIFIED, timestamp / 1000, timestamp, se, le)); batchExecute(batch, RETRY_COUNT); return queue; }
@Override public void removeSubscription(SubscriptionContext context) { Mutator<Object> mutator = getObjectMutator(); mutator.addDeletion(context.getId(), SUBSCRIPTION_COLUMN_FAMILY_NAME); mutator.execute(); removeResourceSubRelation(context); }
public void insertCounter(K key, N name, Long value) { Mutator<K> mutator = buildMutator(); Long currentValue = this.getCounterValue(key, name); long delta = value - currentValue; mutator.incrementCounter(key, columnFamily, name, delta); this.executeMutator(mutator); }
public static void addIntegerByteArrayContentToRaw( String columnFamily, String row, int key, byte[] value, Mutator<String> mutator, boolean execute) throws CassandraDataAccessException { if (mutator == null) { throw new CassandraDataAccessException("Can't add Data , no Mutator provided "); } if (columnFamily == null || row == null || value == null) { throw new CassandraDataAccessException( "Can't add data with columnFamily = " + columnFamily + " and row=" + row + " key = " + key + " value = " + value); } mutator.addInsertion( row, columnFamily, HFactory.createColumn(key, value, integerSerializer, bytesArraySerializer)); if (execute) { mutator.execute(); } }
@Override public void run() { synchronized (m_jobLock) { m_started = true; m_jobLock.notifyAll(); } /*if (true) return;*/ try { if (m_pendingMutations != null) { for (Triple<RowKeyType, ColumnKeyType, ValueType> data : m_buffer) { HColumnImpl<ColumnKeyType, ValueType> col = new HColumnImpl<ColumnKeyType, ValueType>( data.getSecond(), data.getThird(), data.getTime(), m_columnKeySerializer, m_valueSerializer); // if a TTL is set apply it to the column. This will // cause it to be removed after this number of seconds if (data.getTtl() != 0) { col.setTtl(data.getTtl()); } m_pendingMutations.addInsertion(data.getFirst(), m_cfName, col); } m_pendingMutations.execute(); } m_pendingMutations = null; } catch (Exception e) { logger.error("Error sending data to Cassandra (" + m_cfName + ")", e); m_maxBufferSize = m_maxBufferSize * 3 / 4; logger.error( "Reducing write buffer size to " + m_maxBufferSize + ". You need to increase your cassandra capacity or change the kairosdb.datastore.cassandra.write_buffer_max_size property."); } // If the batch failed we will retry it without changing the buffer size. while (m_pendingMutations != null) { try { Thread.sleep(100); } catch (InterruptedException ignored) { } try { m_pendingMutations.execute(); m_pendingMutations = null; } catch (Exception e) { logger.error("Error resending data", e); } } }
public void put(String columnFamily, String key, byte[] value) { Mutator<String> mutator = HFactory.createMutator(this._keyspace, StringSerializer.get()); mutator.insert( key, columnFamily, HFactory.createColumn( DEFAULT_COLUMN_NAME, value, StringSerializer.get(), BytesArraySerializer.get())); }
private void insertSubscription(SubscriptionContext context) { Mutator<Object> mutator = getObjectMutator(); mutator.addInsertion( context.getId(), SUBSCRIPTION_COLUMN_FAMILY_NAME, HFactory.createColumn("value", context, STRING_SERIALIZER, OBJECT_SERIALIZER)); mutator.execute(); }
public void addEventSoftState(EventSoftState state) { Mutator<String> mutator = getStringMutator(); mutator.addInsertion( state.getResourceURL(), EVENT_SOFT_STATE_COLUMN_FAMILY_NAME, HFactory.createColumn(state.getEntityTag(), state, STRING_SERIALIZER, OBJECT_SERIALIZER)); mutator.execute(); }
public QueueIndexUpdate batchUpdateQueueIndex( QueueIndexUpdate indexUpdate, UUID subcriptionQueueId) throws Exception { logger.info("batchUpdateQueueIndex"); Mutator<ByteBuffer> batch = indexUpdate.getBatch(); // queue_id,prop_name Object index_key = key(subcriptionQueueId, indexUpdate.getEntryName()); // subscription_queue_id,subscriber_queue_id,prop_name for (QueueIndexEntry entry : indexUpdate.getPrevEntries()) { if (entry.getValue() != null) { index_key = key(subcriptionQueueId, entry.getPath()); batch.addDeletion( bytebuffer(index_key), PROPERTY_INDEX.getColumnFamily(), entry.getIndexComposite(), dce, indexUpdate.getTimestamp()); } else { logger.error("Unexpected condition - deserialized property value is null"); } } if (indexUpdate.getNewEntries().size() > 0) { for (QueueIndexEntry indexEntry : indexUpdate.getNewEntries()) { index_key = key(subcriptionQueueId, indexEntry.getPath()); batch.addInsertion( bytebuffer(index_key), PROPERTY_INDEX.getColumnFamily(), createColumn( indexEntry.getIndexComposite(), ByteBuffer.allocate(0), indexUpdate.getTimestamp(), dce, be)); } } for (String index : indexUpdate.getIndexesSet()) { batch.addInsertion( bytebuffer(key(subcriptionQueueId, DICTIONARY_SUBSCRIBER_INDEXES)), QUEUE_DICTIONARIES.getColumnFamily(), createColumn(index, ByteBuffer.allocate(0), indexUpdate.getTimestamp(), se, be)); } return indexUpdate; }
@Override public void delete(final Mailbox mailbox) throws IOException { // purge all previously deleted objects // TODO: we should not instantiate here MessageDAO messageDAO = new CassandraMessageDAO(keyspace); messageDAO.purge(mailbox, new Date()); // delete all objects from object store try { List<UUID> messageIds = null; UUID start = null; // loop until we delete all items do { // get all message ids messageIds = LabelIndexPersistence.get( mailbox.getId(), ReservedLabels.ALL_MAILS.getId(), start, BatchConstants.BATCH_READS, true); // get all message headers Map<UUID, Message> messages = MessagePersistence.fetch(mailbox.getId(), messageIds, false); // delete message sources from object store for (UUID messageId : messages.keySet()) { blobStorage.delete(messages.get(messageId).getLocation()); } // set start element for the next loop start = messageIds.isEmpty() ? null : messageIds.get(messageIds.size() - 1); } while (messageIds.size() >= BatchConstants.BATCH_READS); } catch (Exception e) { throw new IOException(e); } // begin batch operation Mutator<String> m = createMutator(keyspace, strSe); // delete all MessageMetadata MessagePersistence.deleteAllMessages(m, mailbox.getId()); // delete all indexes from IndexLabel LabelIndexPersistence.deleteIndexes(m, mailbox.getId()); // delete all counters LabelCounterPersistence.deleteAll(m, mailbox.getId()); // delete Account data AccountPersistence.delete(m, mailbox.getId()); // commit batch operation m.execute(); }
@Override @CacheEvict(value = "user-cache", key = "#user.login") public void deleteUser(User user) { if (log.isDebugEnabled()) { log.debug("Deleting user : " + user); } Mutator<String> mutator = HFactory.createMutator(keyspaceOperator, StringSerializer.get()); mutator.addDeletion(user.getLogin(), USER_CF); mutator.execute(); }
/** * Decrement counter by 1 * * @param rawID raw name * @param columnFamily column family name * @param columnName name of column * @param keyspace keyspace * @throws CassandraDataAccessException */ public static void decrementCounter( String columnName, String columnFamily, String rawID, Keyspace keyspace, long decrementBy) throws CassandraDataAccessException { try { Mutator<String> mutator = HFactory.createMutator(keyspace, StringSerializer.get()); mutator.decrementCounter(rawID, columnFamily, columnName, decrementBy); mutator.execute(); } catch (Exception e) { throw new CassandraDataAccessException("Error while accessing:" + columnFamily, e); } }
private void insertResource(Resource resource) { Mutator<String> mutator = getStringMutator(); mutator.addInsertion( resource.getUri(), RESOURCE_COLUMN_FAMILY_NAME, HFactory.createColumn( ((SIPResource) resource).getEventName(), resource, STRING_SERIALIZER, OBJECT_SERIALIZER)); mutator.execute(); }
void insertData(String dpname, Date d, Timestamp u, double value) { try { long timeInMicroSeconds = u.getTime(); // UUID timeUUIDColumnName = TimeUUIDUtils.getTimeUUID(timeInMicroSeconds); System.out.println(d + "\t" + timeInMicroSeconds + "\t" + value); DateSerializer ds = new DateSerializer(); Mutator<Date> mu = HFactory.createMutator(keyspace, ds); mu.insert(d, dpname, HFactory.createColumn(timeInMicroSeconds, value)); } catch (Exception e) { e.printStackTrace(); } }
public void removeFriends(String from_uname, List<String> to_unames) { Mutator<String> mutator = cassandra.getMutator(); for (String uname : to_unames) { mutator .addDeletion(from_uname, FRIENDS, uname, SE) .addDeletion(uname, FOLLOWERS, from_uname, SE); mutator.decrementCounter(from_uname, MISC_COUNTS, "friends", 1); mutator.decrementCounter(uname, MISC_COUNTS, "followers", 1); } mutator.execute(); }
/** * Write the index definition * * @param stateManager The objects state manager * @param mutator The mutator to write to * @param clock the clock value to use */ public void writeIndex( OpenJPAStateManager stateManager, Mutator<byte[]> mutator, long clock, IndexQueue queue) { DynamicComposite searchComposite = null; DynamicComposite tombstoneComposite = null; DynamicComposite idAudit = null; // loop through all added objects and create the writes for them. // create our composite of the format of id+order* for (int i = 0; i < subClasses.length; i++) { searchComposite = newComposite(); searchComposite.addComponent(subClasses[i], stringSerializer); // create our composite of the format order*+id tombstoneComposite = newComposite(); tombstoneComposite.addComponent( 1, subClasses[i], stringSerializer, stringSerializer.getComparatorType().getTypeName(), ComponentEquality.EQUAL); idAudit = newComposite(); idAudit.addComponent( 1, subClasses[i], stringSerializer, stringSerializer.getComparatorType().getTypeName(), ComponentEquality.EQUAL); constructComposites(searchComposite, tombstoneComposite, idAudit, stateManager); mutator.addInsertion( indexName, CF_NAME, new HColumnImpl<DynamicComposite, byte[]>( searchComposite, HOLDER, clock, compositeSerializer, bytesSerializer)); mutator.addInsertion( reverseIndexName, CF_NAME, new HColumnImpl<DynamicComposite, byte[]>( tombstoneComposite, HOLDER, clock, compositeSerializer, bytesSerializer)); queue.addAudit(new IndexAudit(indexName, reverseIndexName, idAudit, clock, CF_NAME, true)); } }
@Override public void insertTatamibotConfiguration(TatamibotConfiguration tatamibotConfiguration) { UUID tatamibotConfigurationId = TimeUUIDUtils.getUniqueTimeUUIDinMillis(); Mutator<String> mutator = HFactory.createMutator(keyspaceOperator, StringSerializer.get()); mutator.insert( tatamibotConfiguration.getDomain(), ColumnFamilyKeys.DOMAIN_TATAMIBOT_CF, HFactory.createColumn( tatamibotConfigurationId, "", UUIDSerializer.get(), StringSerializer.get())); tatamibotConfiguration.setTatamibotConfigurationId(tatamibotConfigurationId.toString()); em.persist(tatamibotConfiguration); }
public void addNotifyBody( String resourceUri, String eventName, String notifyBodyType, NotifyBody notifyBody) { if (SIPConstans.EVENT_NAME_PRESENCE.equals(eventName)) { Mutator<String> mutator = getStringMutator(); mutator.addInsertion( resourceUri, PRESENCE_NB_COLUMN_FAMILY_NAME, HFactory.createColumn(notifyBodyType, notifyBody, STRING_SERIALIZER, OBJECT_SERIALIZER)); mutator.execute(); } else { throw new IllegalArgumentException("Can't save notify body for event[" + eventName + "]"); } }
public void addFriends(String from_uname, List<String> to_unames) { long timestamp = System.currentTimeMillis(); Mutator<String> mutator = cassandra.getMutator(); for (String uname : to_unames) { mutator .addInsertion( from_uname, FRIENDS, HFactory.createStringColumn(uname, String.valueOf(timestamp))) .addInsertion( uname, FOLLOWERS, HFactory.createStringColumn(from_uname, String.valueOf(timestamp))) .addCounter(from_uname, MISC_COUNTS, HFactory.createCounterColumn("friends", 1)) .addCounter(uname, MISC_COUNTS, HFactory.createCounterColumn("followers", 1)); } mutator.execute(); }
@Override @CacheEvict(value = "domain-tags-cache", key = "#domain") public void addTag(String domain, String tag) { HColumn<UUID, String> column = HFactory.createColumn( TimeUUIDUtils.getUniqueTimeUUIDinMillis(), tag, COLUMN_TTL, UUIDSerializer.get(), StringSerializer.get()); Mutator<String> mutator = HFactory.createMutator(keyspaceOperator, StringSerializer.get()); mutator.insert(domain, TRENDS_CF, column); }
/** * remove allocated raw-column space for counter * * @param cfName column family name * @param counterRowName name of row * @param queueColumn name of column * @param keyspace key space name * @throws CassandraDataAccessException */ public static void removeCounterColumn( String cfName, String counterRowName, String queueColumn, Keyspace keyspace) throws CassandraDataAccessException { try { Mutator<String> mutator = HFactory.createMutator(keyspace, StringSerializer.get()); mutator.deleteCounter(counterRowName, cfName, queueColumn, stringSerializer); mutator.execute(); CounterQuery<String, String> counter = new ThriftCounterColumnQuery<String, String>( keyspace, stringSerializer, stringSerializer); counter.setColumnFamily(cfName).setKey(counterRowName).setName(queueColumn); } catch (Exception e) { throw new CassandraDataAccessException("Error while accessing:" + cfName, e); } }
public void removeColumnRange(K key, N start, N end, boolean reverse, int count) { Mutator<K> mutator = HFactory.createMutator(keyspace, keySerializer); List<HColumn<N, V>> columns = createSliceQuery(keyspace, keySerializer, columnNameSerializer, valueSerializer) .setColumnFamily(columnFamily) .setKey(key) .setRange(start, end, reverse, count) .execute() .get() .getColumns(); for (HColumn<N, V> column : columns) { mutator.addDeletion(key, columnFamily, column.getName(), columnNameSerializer); } this.executeMutator(mutator); }