/** * 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); } }
// TM @Override protected void batchDeleteRow(String cf, List<Node[]> li, String keyspace) { if (cf.equals(CF_C_SPO)) { super.batchDeleteRow(cf, li, keyspace); } else if (cf.equals(CF_PO_S)) { Mutator<byte[]> m = HFactory.createMutator(getExistingKeyspace(keyspace), _bs); for (Node[] nx : li) { // reorder for the key Node[] reordered = Util.reorder(nx, _maps.get(cf)); ByteBuffer rowKey = createKey(new Node[] {reordered[0], reordered[1]}); String colKey = reordered[2].toN3(); // delete the full row m.addDeletion(rowKey.array(), cf); } m.execute(); } else { Mutator<String> m = HFactory.createMutator(getExistingKeyspace(keyspace), _ss); for (Node[] nx : li) { // reorder for the key Node[] reordered = Util.reorder(nx, _maps.get(cf)); String rowKey = reordered[0].toN3(); m.addDeletion(rowKey, cf); _log.info("Delete full row for " + rowKey + " cf= " + cf); } m.execute(); } }
/** * 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); } }
/** * 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); } }
/** * 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); } }
public void init() { CassandraHostConfigurator hostConfig = new CassandraHostConfigurator(darkStarNode + ":" + String.valueOf(darkStarPort)); cluster = HFactory.createCluster(clusterName, hostConfig); keySpace = HFactory.createKeyspace(keyspace, cluster, policy); mutator = HFactory.createMutator(keySpace, bfs); }
/** * Create a Cassandra Cluster instance given the connection details * * @param userName userName to connect to the cassandra * @param password password to connect to cassandra * @param clusterName Cluster name * @param connectionString cassandra connection string * @return Cluster instance * @throws CassandraDataAccessException In case of and error in accessing database or data error */ public static Cluster createCluster( String userName, String password, String clusterName, String connectionString) throws CassandraDataAccessException { if (userName == null || password == null) { throw new CassandraDataAccessException( "Can't create cluster with empty userName or Password"); } if (clusterName == null) { throw new CassandraDataAccessException("Can't create cluster with empty cluster name"); } if (connectionString == null) { throw new CassandraDataAccessException("Can't create cluster with empty connection string"); } Map<String, String> credentials = new HashMap<String, String>(); credentials.put(USERNAME_KEY, userName); credentials.put(PASSWORD_KEY, password); CassandraHostConfigurator hostConfigurator = new CassandraHostConfigurator(connectionString); hostConfigurator.setMaxActive(2000); Cluster cluster = HFactory.getCluster(clusterName); if (cluster == null) { cluster = HFactory.createCluster(clusterName, hostConfigurator, credentials); } return cluster; }
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())); }
void setEnvironment() throws InvalidRequestException, TException { try { myCluster = HFactory.getOrCreateCluster("Test Sample", "localhost:9160"); ksdef = myCluster.describeKeyspace(keyspaceName); if (ksdef == null) addKeyspacetoCassandra(); keyspace = HFactory.createKeyspace("mostkeyspace", myCluster); } catch (Exception e) { System.out.print("Unalble to setup environment"); e.printStackTrace(); } }
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(); } }
@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 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); }
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); } }
public static List<String> getDestinationQueueNamesFromCounterColumns( String columnFamilyName, String rowName, Keyspace keyspace) throws CassandraDataAccessException { ArrayList<String> rowList = new ArrayList<String>(); if (keyspace == null) { throw new CassandraDataAccessException("Can't access Data , no keyspace provided "); } if (columnFamilyName == null || rowName == null) { throw new CassandraDataAccessException( "Can't access data with columnFamily =" + columnFamilyName + " and rowName=" + rowName); } try { SliceCounterQuery<String, String> sliceQuery = HFactory.createCounterSliceQuery(keyspace, stringSerializer, stringSerializer); sliceQuery.setKey(rowName); sliceQuery.setColumnFamily(columnFamilyName); sliceQuery.setRange("", "", false, Integer.MAX_VALUE); QueryResult<CounterSlice<String>> result = sliceQuery.execute(); CounterSlice<String> columnSlice = result.get(); for (HCounterColumn<String> column : columnSlice.getColumns()) { rowList.add(column.getName()); } } catch (Exception e) { throw new CassandraDataAccessException( "Error while accessing data from :" + columnFamilyName, e); } return rowList; }
@Override public String getLastRevision(String executionPlanIdentifier) { ColumnSlice<String, byte[]> cs; String rangeStart = new StringBuffer(String.valueOf(timeAt1970.getTime())).append("_").toString(); boolean firstLoop = true; while (true) { SliceQuery<String, String, byte[]> q = HFactory.createSliceQuery(keyspace, sser, sser, bser); q.setColumnFamily(INDEX_COLUMN_FAMILY_NAME) .setKey(executionPlanIdentifier) .setRange(rangeStart, String.valueOf(Long.MAX_VALUE), false, 1000); QueryResult<ColumnSlice<String, byte[]>> r = q.execute(); cs = r.get(); int size = cs.getColumns().size(); if (firstLoop && size == 0) { return null; } else if (size == 0) { return rangeStart; } else { firstLoop = false; } int lastIndex = size - 1; rangeStart = cs.getColumns().get(lastIndex).getName(); if (size < 1000) { break; } } log.info("found revision " + rangeStart); return rangeStart; }
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 Collection<TatamibotConfiguration> findTatamibotConfigurationsByDomain(String domain) { Set<TatamibotConfiguration> configurations = new HashSet<TatamibotConfiguration>(); ColumnSlice<UUID, String> results = HFactory.createSliceQuery( keyspaceOperator, StringSerializer.get(), UUIDSerializer.get(), StringSerializer.get()) .setColumnFamily(ColumnFamilyKeys.DOMAIN_TATAMIBOT_CF) .setKey(domain) .setRange(null, null, false, Integer.MAX_VALUE) .execute() .get(); for (HColumn<UUID, String> column : results.getColumns()) { String tatamibotConfigurationId = column.getName().toString(); TatamibotConfiguration configuration = em.find(TatamibotConfiguration.class, tatamibotConfigurationId); configurations.add(configuration); } return configurations; }
/** * Get set of messages in a column family * * @param queueName QueueName * @param columnFamilyName ColumnFamilyName * @param keyspace Cassandra KeySpace * @param count max message count limit * @return ColumnSlice which contain the messages * @throws CassandraDataAccessException */ public static ColumnSlice<Long, byte[]> getMessagesFromQueue( String queueName, String columnFamilyName, Keyspace keyspace, int count) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't access Data , no keyspace provided "); } if (columnFamilyName == null || queueName == null) { throw new CassandraDataAccessException( "Can't access data with columnFamily = " + columnFamilyName + " and queueName=" + queueName); } try { SliceQuery<String, Long, byte[]> sliceQuery = HFactory.createSliceQuery( keyspace, stringSerializer, longSerializer, bytesArraySerializer); sliceQuery.setKey(queueName); sliceQuery.setRange((long) 0, Long.MAX_VALUE, false, count); sliceQuery.setColumnFamily(columnFamilyName); QueryResult<ColumnSlice<Long, byte[]>> result = sliceQuery.execute(); ColumnSlice<Long, byte[]> columnSlice = result.get(); return columnSlice; } catch (Exception e) { throw new CassandraDataAccessException( "Error while getting data from " + columnFamilyName, e); } }
/** * Get a HColumn<Long, byte[]> with a given key in a given row in a given column Family * * @param rowName name of the row * @param columnFamily column Family name * @param key long type key of the column we are looking for * @param keyspace cassandra keySpace instance * @return query result as a cassandra column * @throws CassandraDataAccessException in case of an Error when accessing data */ public static HColumn<Long, byte[]> getLongByteArrayColumnInARow( String rowName, String columnFamily, long key, Keyspace keyspace) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't access Data , no keyspace provided "); } if (columnFamily == null || rowName == null) { throw new CassandraDataAccessException( "Can't access data with columnFamily = " + columnFamily + " and rowName=" + rowName); } try { ColumnQuery columnQuery = HFactory.createColumnQuery( keyspace, stringSerializer, longSerializer, bytesArraySerializer); columnQuery.setColumnFamily(columnFamily); columnQuery.setKey(rowName); columnQuery.setName(key); QueryResult<HColumn<Long, byte[]>> result = columnQuery.execute(); HColumn<Long, byte[]> column = result.get(); return column; } catch (Exception e) { throw new CassandraDataAccessException( "Error while executing quary for HColumn<Long, byte[]> with key =" + key + " in column Family = " + columnFamily, e); } }
/** * Get Number of <String,String> type columns in a given row in a cassandra column family * * @param rowName row Name we are querying for * @param columnFamilyName columnFamilName * @param keyspace * @param count * @return */ public static ColumnSlice<String, String> getStringTypeColumnsInARow( String rowName, String columnFamilyName, Keyspace keyspace, int count) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't access Data , no keyspace provided "); } if (columnFamilyName == null || rowName == null) { throw new CassandraDataAccessException( "Can't access data with columnFamily = " + columnFamilyName + " and rowName=" + rowName); } try { SliceQuery sliceQuery = HFactory.createSliceQuery(keyspace, stringSerializer, stringSerializer, stringSerializer); sliceQuery.setKey(rowName); sliceQuery.setColumnFamily(columnFamilyName); sliceQuery.setRange("", "", false, count); QueryResult<ColumnSlice<String, String>> result = sliceQuery.execute(); ColumnSlice<String, String> columnSlice = result.get(); return columnSlice; } catch (Exception e) { throw new CassandraDataAccessException( "Error while getting data from : " + columnFamilyName, e); } }
@Override public PersistenceObject load( PersistenceManagementEvent persistenceManagementEvent, String nodeId) { List<NodeSnapshot> list = new ArrayList<NodeSnapshot>(); ColumnSlice<String, byte[]> cs; SliceQuery<String, String, byte[]> q = HFactory.createSliceQuery(keyspace, sser, sser, bser); q.setColumnFamily(COLUMN_FAMILY_NAME) .setKey(persistenceManagementEvent.getRevision()) .setRange("", "", false, 1000) .setColumnNames(nodeId); QueryResult<ColumnSlice<String, byte[]>> r = q.execute(); cs = r.get(); PersistenceObject persistenceObject = null; for (HColumn<String, byte[]> hc : cs.getColumns()) { persistenceObject = (PersistenceObject) ByteSerializer.BToO(hc.getValue()); // list.add(new NodeSnapshot(hc.getName(), hc.getValue())); } // return list; return persistenceObject; }
/** * Counts columns in the specified range of a standard column family * * @param key * @param start * @param end * @param max * @return */ public int countColumns(K key, N start, N end, int max) { CountQuery<K, N> query = HFactory.createCountQuery(keyspace, keySerializer, topSerializer); query.setKey(key); query.setColumnFamily(columnFamily); query.setRange(start, end, max); return query.execute().get(); }
private List<String> getTags( Keyspace keyspaceOperator, String columnfamily, String cfId, String startRange, String endRange) { SliceQuery<String, String, String> query = HFactory.createSliceQuery( keyspaceOperator, StringSerializer.get(), StringSerializer.get(), StringSerializer.get()) .setColumnFamily(columnfamily) .setKey(cfId) .setRange(TAG_COLUMN_MIN_NAME, TAG_COLUMN_MAX_NAME, false, Integer.MAX_VALUE); QueryResult<ColumnSlice<String, String>> queryResult = query.execute(); ColumnSlice<String, String> columnSlice = queryResult.get(); SortedSet<String> tags = new TreeSet<String>(); for (HColumn<String, String> hColumn : columnSlice.getColumns()) { tags.add(hColumn.getValue()); } return new ArrayList<String>(tags); }
public void runQuery() throws IOException { MultigetSliceQuery<DataPointsRowKey, Integer, ByteBuffer> msliceQuery = HFactory.createMultigetSliceQuery( m_keyspace, ROW_KEY_SERIALIZER, IntegerSerializer.get(), ByteBufferSerializer.get()); msliceQuery.setColumnFamily(m_columnFamily); msliceQuery.setKeys(m_rowKeys); if (m_descending) msliceQuery.setRange(m_endTime, m_startTime, true, m_multiRowReadSize); else msliceQuery.setRange(m_startTime, m_endTime, false, m_multiRowReadSize); Rows<DataPointsRowKey, Integer, ByteBuffer> rows = msliceQuery.execute().get(); List<Row<DataPointsRowKey, Integer, ByteBuffer>> unfinishedRows = new ArrayList<Row<DataPointsRowKey, Integer, ByteBuffer>>(); for (Row<DataPointsRowKey, Integer, ByteBuffer> row : rows) { List<HColumn<Integer, ByteBuffer>> columns = row.getColumnSlice().getColumns(); if (!m_limit && columns.size() == m_multiRowReadSize) unfinishedRows.add(row); writeColumns(row.getKey(), columns); } // Iterate through the unfinished rows and get the rest of the data. // todo: use multiple threads to retrieve this data for (Row<DataPointsRowKey, Integer, ByteBuffer> unfinishedRow : unfinishedRows) { DataPointsRowKey key = unfinishedRow.getKey(); SliceQuery<DataPointsRowKey, Integer, ByteBuffer> sliceQuery = HFactory.createSliceQuery( m_keyspace, ROW_KEY_SERIALIZER, IntegerSerializer.get(), ByteBufferSerializer.get()); sliceQuery.setColumnFamily(m_columnFamily); sliceQuery.setKey(key); List<HColumn<Integer, ByteBuffer>> columns = unfinishedRow.getColumnSlice().getColumns(); do { Integer lastTime = columns.get(columns.size() - 1).getName(); if (m_descending) sliceQuery.setRange(lastTime - 1, m_startTime, true, m_singleRowReadSize); else sliceQuery.setRange(lastTime + 1, m_endTime, false, m_singleRowReadSize); columns = sliceQuery.execute().get().getColumns(); writeColumns(key, columns); } while (columns.size() == m_singleRowReadSize); } }
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 static <A, B> HColumn<A, B> column(final A name, final B value) { return HFactory.createColumn( name, // value, // (Serializer<A>) SerializerTypeInferer.getSerializer(name), // (Serializer<B>) SerializerTypeInferer.getSerializer(value)); }
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 truncate() { Mutator<K> mutator = HFactory.createMutator(keyspace, keySerializer); Iterator<K> iterator = new KeyIterator<K>(keyspace, columnFamily, keySerializer).iterator(); while (iterator.hasNext()) { this.removeRowBatch(iterator.next(), mutator); } this.executeMutator(mutator); }
public void removeCounterRow(K key) { SliceCounterQuery<K, N> query = HFactory.createCounterSliceQuery(keyspace, keySerializer, columnNameSerializer) .setColumnFamily(columnFamily) .setKey(key); AchillesCounterSliceIterator<K, N> iterator = new AchillesCounterSliceIterator<K, N>( policy, columnFamily, query, (N) null, (N) null, false, DEFAULT_LENGTH); Mutator<K> mutator = HFactory.createMutator(keyspace, keySerializer); while (iterator.hasNext()) { HCounterColumn<N> counterCol = iterator.next(); mutator.decrementCounter(key, columnFamily, counterCol.getName(), counterCol.getValue()); } this.executeMutator(mutator); }