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; }
public Map<String, Long> getQueueCounters(UUID queueId) throws Exception { Map<String, Long> counters = new HashMap<String, Long>(); Keyspace ko = cass.getApplicationKeyspace(applicationId); SliceCounterQuery<UUID, String> q = createCounterSliceQuery(ko, ue, se); q.setColumnFamily(COUNTERS.getColumnFamily()); q.setRange(null, null, false, ALL_COUNT); QueryResult<CounterSlice<String>> r = q.setKey(queueId).execute(); for (HCounterColumn<String> column : r.get().getColumns()) { counters.put(column.getName(), column.getValue()); } return counters; }
/** * @param keyspace name of key space * @param columnFamily column family name * @param key key value * @param cloumnName column name * @return long count value * @throws CassandraDataAccessException */ public static long getCountValue( Keyspace keyspace, String columnFamily, String cloumnName, String key) throws CassandraDataAccessException { try { CounterQuery<String, String> query = HFactory.createCounterColumnQuery(keyspace, stringSerializer, stringSerializer); query.setColumnFamily(columnFamily).setKey(key).setName(cloumnName); HCounterColumn<String> counter = query.execute().get(); return counter.getValue(); } catch (Exception e) { throw new CassandraDataAccessException("Error while accessing:" + columnFamily, e); } }
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); }
public AggregateCounterSet getAggregateCounters( UUID queueId, String category, String counterName, CounterResolution resolution, long start, long finish, boolean pad) { start = resolution.round(start); finish = resolution.round(finish); long expected_time = start; Keyspace ko = cass.getApplicationKeyspace(applicationId); SliceCounterQuery<String, Long> q = createCounterSliceQuery(ko, se, le); q.setColumnFamily(APPLICATION_AGGREGATE_COUNTERS.getColumnFamily()); q.setRange(start, finish, false, ALL_COUNT); QueryResult<CounterSlice<Long>> r = q.setKey( counterUtils.getAggregateCounterRow( counterName, null, null, queueId, category, resolution)) .execute(); List<AggregateCounter> counters = new ArrayList<AggregateCounter>(); for (HCounterColumn<Long> column : r.get().getColumns()) { AggregateCounter count = new AggregateCounter(column.getName(), column.getValue()); if (pad && !(resolution == CounterResolution.ALL)) { while (count.getTimestamp() != expected_time) { counters.add(new AggregateCounter(expected_time, 0)); expected_time = resolution.next(expected_time); } expected_time = resolution.next(expected_time); } counters.add(count); } if (pad && !(resolution == CounterResolution.ALL)) { while (expected_time <= finish) { counters.add(new AggregateCounter(expected_time, 0)); expected_time = resolution.next(expected_time); } } return new AggregateCounterSet(counterName, queueId, category, counters); }
public long getCounterValue(K key, N name) { CounterQuery<K, N> counter = new ThriftCounterColumnQuery<K, N>(keyspace, keySerializer, columnNameSerializer) .setColumnFamily(columnFamily) .setKey(key) .setName(name); long counterValue = 0; this.policy.loadConsistencyLevelForRead(columnFamily); try { HCounterColumn<N> column = counter.execute().get(); if (column != null) { counterValue = column.getValue(); } } catch (Throwable throwable) { throw new RuntimeException(throwable); } finally { this.policy.reinitDefaultConsistencyLevel(); } return counterValue; }
public List<AggregateCounterSet> getAggregateCounters(UUID queueId, CounterQuery query) throws Exception { CounterResolution resolution = query.getResolution(); if (resolution == null) { resolution = CounterResolution.ALL; } long start = query.getStartTime() != null ? query.getStartTime() : 0; long finish = query.getFinishTime() != null ? query.getFinishTime() : 0; boolean pad = query.isPad(); if (start <= 0) { start = 0; } if ((finish <= 0) || (finish < start)) { finish = System.currentTimeMillis(); } start = resolution.round(start); finish = resolution.round(finish); long expected_time = start; if (pad && (resolution != CounterResolution.ALL)) { long max_counters = (finish - start) / resolution.interval(); if (max_counters > 1000) { finish = resolution.round(start + (resolution.interval() * 1000)); } } List<CounterFilterPredicate> filters = query.getCounterFilters(); if (filters == null) { return null; } Map<String, org.usergrid.persistence.cassandra.CounterUtils.AggregateCounterSelection> selections = new HashMap<String, AggregateCounterSelection>(); Keyspace ko = cass.getApplicationKeyspace(applicationId); for (CounterFilterPredicate filter : filters) { AggregateCounterSelection selection = new AggregateCounterSelection( filter.getName(), null, null, queueId, filter.getCategory()); selections.put(selection.getRow(resolution), selection); } MultigetSliceCounterQuery<String, Long> q = HFactory.createMultigetSliceCounterQuery(ko, se, le); q.setColumnFamily(APPLICATION_AGGREGATE_COUNTERS.getColumnFamily()); q.setRange(start, finish, false, ALL_COUNT); QueryResult<CounterRows<String, Long>> rows = q.setKeys(selections.keySet()).execute(); List<AggregateCounterSet> countSets = new ArrayList<AggregateCounterSet>(); for (CounterRow<String, Long> r : rows.get()) { expected_time = start; List<AggregateCounter> counters = new ArrayList<AggregateCounter>(); for (HCounterColumn<Long> column : r.getColumnSlice().getColumns()) { AggregateCounter count = new AggregateCounter(column.getName(), column.getValue()); if (pad && (resolution != CounterResolution.ALL)) { while (count.getTimestamp() != expected_time) { counters.add(new AggregateCounter(expected_time, 0)); expected_time = resolution.next(expected_time); } expected_time = resolution.next(expected_time); } counters.add(count); } if (pad && (resolution != CounterResolution.ALL)) { while (expected_time <= finish) { counters.add(new AggregateCounter(expected_time, 0)); expected_time = resolution.next(expected_time); } } AggregateCounterSelection selection = selections.get(r.getKey()); countSets.add( new AggregateCounterSet(selection.getName(), queueId, selection.getCategory(), counters)); } Collections.sort( countSets, new Comparator<AggregateCounterSet>() { @Override public int compare(AggregateCounterSet o1, AggregateCounterSet o2) { String s1 = o1.getName(); String s2 = o2.getName(); return s1.compareTo(s2); } }); return countSets; }