Exemple #1
0
 /**
  * @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);
   }
 }
Exemple #2
0
  /**
   * 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);
    }
  }
Exemple #3
0
  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;
  }