예제 #1
0
  /**
   * 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);
    }
  }
예제 #2
0
  /**
   * 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);
    }
  }
예제 #3
0
  /**
   * Get List of Strings in a Given ROW in a Cassandra Column Family Here we assume that the columns
   * in a given row have string data and key and value in the given column in that row have same
   * values.
   *
   * @param columnFamilyName Name of the column Family
   * @param rowName Row name
   * @param keyspace keySpace
   * @return List of string in that given row.
   * @throws CassandraDataAccessException In case of database access error or data error
   */
  public static List<String> getRowList(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 {
      SliceQuery<String, String, String> sliceQuery =
          HFactory.createSliceQuery(keyspace, stringSerializer, stringSerializer, stringSerializer);
      sliceQuery.setKey(rowName);
      sliceQuery.setColumnFamily(columnFamilyName);
      sliceQuery.setRange("", "", false, 10000);

      QueryResult<ColumnSlice<String, String>> result = sliceQuery.execute();
      ColumnSlice<String, String> columnSlice = result.get();
      for (HColumn<String, String> column : columnSlice.getColumns()) {
        rowList.add(column.getName());
      }
    } catch (Exception e) {
      throw new CassandraDataAccessException(
          "Error while accessing data from :" + columnFamilyName, e);
    }
    return rowList;
  }