示例#1
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);
    }
  }
示例#2
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);
    }
  }
示例#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;
  }
 public Queue getQueue(String queuePath, UUID queueId) {
   SliceQuery<UUID, String, ByteBuffer> q =
       createSliceQuery(cass.getApplicationKeyspace(applicationId), ue, se, be);
   q.setColumnFamily(QUEUE_PROPERTIES.getColumnFamily());
   q.setKey(queueId);
   q.setRange(null, null, false, ALL_COUNT);
   QueryResult<ColumnSlice<String, ByteBuffer>> r = q.execute();
   ColumnSlice<String, ByteBuffer> slice = r.get();
   List<HColumn<String, ByteBuffer>> results = slice.getColumns();
   return deserializeQueue(results);
 }
 @Override
 public Message getMessage(UUID messageId) {
   SliceQuery<UUID, String, ByteBuffer> q =
       createSliceQuery(cass.getApplicationKeyspace(applicationId), ue, se, be);
   q.setColumnFamily(MESSAGE_PROPERTIES.getColumnFamily());
   q.setKey(messageId);
   q.setRange(null, null, false, ALL_COUNT);
   QueryResult<ColumnSlice<String, ByteBuffer>> r = q.execute();
   ColumnSlice<String, ByteBuffer> slice = r.get();
   List<HColumn<String, ByteBuffer>> results = slice.getColumns();
   return deserializeMessage(results);
 }
  @Override
  public Set<String> getQueueCounterNames(String queuePath) throws Exception {
    Set<String> names = new HashSet<String>();
    Keyspace ko = cass.getApplicationKeyspace(applicationId);
    SliceQuery<String, String, ByteBuffer> q = createSliceQuery(ko, se, se, be);
    q.setColumnFamily(QueuesCF.QUEUE_DICTIONARIES.toString());
    q.setKey(CassandraPersistenceUtils.key(getQueueId(queuePath), DICTIONARY_COUNTERS).toString());
    q.setRange(null, null, false, ALL_COUNT);

    List<HColumn<String, ByteBuffer>> columns = q.execute().get().getColumns();
    for (HColumn<String, ByteBuffer> column : columns) {
      names.add(column.getName());
    }
    return names;
  }
示例#7
0
    CompositeQueryIterator(String key, Composite start, Composite end) {
      this.key = key;
      this.start = start;
      this.end = end;

      SliceQuery<String, Composite, String> sliceQuery =
          HFactory.createSliceQuery(
              tutorialKeyspace,
              StringSerializer.get(),
              new CompositeSerializer(),
              StringSerializer.get());
      sliceQuery.setColumnFamily("CountryStateCity");
      sliceQuery.setKey(key);

      sliceIterator = new ColumnSliceIterator(sliceQuery, start, end, false);
    }
  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);
    }
  }
  /**
   * Get a list of UUIDs that can be read for the client. This comes directly from the queue inbox,
   * and DOES NOT take into account client messages
   *
   * @param queueId The queue id to read
   * @param bounds The bounds to use when reading
   * @return
   */
  protected List<UUID> getQueueRange(UUID queueId, QueueBounds bounds, SearchParam params) {

    if (bounds == null) {
      logger.error("Necessary queue bounds not found");
      throw new QueueException("Neccessary queue bounds not found");
    }

    UUID finish_uuid = params.reversed ? bounds.getOldest() : bounds.getNewest();

    List<UUID> results = new ArrayList<UUID>(params.limit);

    UUID start = params.startId;

    if (start == null) {
      start = params.reversed ? bounds.getNewest() : bounds.getOldest();
    }

    if (start == null) {
      logger.error("No first message in queue");
      return results;
    }

    if (finish_uuid == null) {
      logger.error("No last message in queue");
      return results;
    }

    long start_ts_shard = roundLong(getTimestampInMillis(start), QUEUE_SHARD_INTERVAL);

    long finish_ts_shard = roundLong(getTimestampInMillis(finish_uuid), QUEUE_SHARD_INTERVAL);

    long current_ts_shard = start_ts_shard;
    if (params.reversed) {
      current_ts_shard = finish_ts_shard;
    }

    while ((current_ts_shard >= start_ts_shard) && (current_ts_shard <= finish_ts_shard)) {

      UUID slice_start = MIN_TIME_UUID;
      UUID slice_end = MAX_TIME_UUID;

      if (current_ts_shard == start_ts_shard) {
        slice_start = start;
      }

      if (current_ts_shard == finish_ts_shard) {
        slice_end = finish_uuid;
      }

      SliceQuery<ByteBuffer, UUID, ByteBuffer> q = createSliceQuery(ko, be, ue, be);
      q.setColumnFamily(QUEUE_INBOX.getColumnFamily());
      q.setKey(getQueueShardRowKey(queueId, current_ts_shard));
      q.setRange(slice_start, slice_end, params.reversed, params.limit + 1);

      List<HColumn<UUID, ByteBuffer>> cassResults = q.execute().get().getColumns();

      for (int i = 0; i < cassResults.size(); i++) {
        HColumn<UUID, ByteBuffer> column = cassResults.get(i);

        // skip the first one, we've already read it
        if (i == 0 && params.skipFirst && params.startId.equals(column.getName())) {
          continue;
        }

        UUID id = column.getName();

        results.add(id);

        logger.debug("Added id '{}' to result set for queue id '{}'", id, queueId);

        if (results.size() >= params.limit) {
          return results;
        }
      }

      if (params.reversed) {
        current_ts_shard -= QUEUE_SHARD_INTERVAL;
      } else {
        current_ts_shard += QUEUE_SHARD_INTERVAL;
      }
    }

    return results;
  }