public List<LogEntry> getPending() throws Throwable {
    List<LogEntry> result = new ArrayList<LogEntry>();
    SlicePredicate predicate = new SlicePredicate();
    SliceRange range =
        new SliceRange(
            ByteBufferUtil.bytes(""), ByteBufferUtil.bytes(""), false, MAX_NUMBER_COLUMNS);
    predicate.setSlice_range(range);

    KeyRange keyRange = new KeyRange(BATCH_SIZE);
    keyRange.setStart_key(ByteBufferUtil.bytes(""));
    keyRange.setEnd_key(ByteBufferUtil.EMPTY_BYTE_BUFFER);
    ColumnParent parent = new ColumnParent(COLUMN_FAMILY);

    IndexClause indexClause = new IndexClause();
    indexClause.setCount(BATCH_SIZE);
    indexClause.setStart_key(new byte[0]);
    indexClause.addToExpressions(
        new IndexExpression(
            ByteBufferUtil.bytes(LogEntryColumns.STATUS.toString()),
            IndexOperator.EQ,
            ByteBufferUtil.bytes(LogEntryStatus.COMMITTED.toString())));

    indexClause.addToExpressions(
        new IndexExpression(
            ByteBufferUtil.bytes(LogEntryColumns.HOST.toString()),
            IndexOperator.EQ,
            ByteBufferUtil.bytes(this.getHostName())));

    List<KeySlice> rows =
        getConnection(KEYSPACE)
            .get_indexed_slices(parent, indexClause, predicate, ConsistencyLevel.ALL);

    result.addAll(toLogEntry(rows));

    indexClause = new IndexClause();
    indexClause.setCount(BATCH_SIZE);
    indexClause.setStart_key(new byte[0]);
    indexClause.addToExpressions(
        new IndexExpression(
            ByteBufferUtil.bytes(LogEntryColumns.STATUS.toString()),
            IndexOperator.EQ,
            ByteBufferUtil.bytes(LogEntryStatus.COMMITTED.toString())));

    indexClause.addToExpressions(
        new IndexExpression(
            ByteBufferUtil.bytes((LogEntryColumns.TIMESTAMP.toString())),
            IndexOperator.LT,
            ByteBufferUtil.bytes(
                System.currentTimeMillis() - (1000L * TIME_BEFORE_PROCESS_OTHER_HOST))));

    rows =
        getConnection(KEYSPACE)
            .get_indexed_slices(parent, indexClause, predicate, ConsistencyLevel.ALL);

    result.addAll(toLogEntry(rows));
    return result;
  }
Exemple #2
0
  @Test
  public void testGetRangeSlices() throws HectorException {
    for (int i = 0; i < 10; i++) {
      ColumnPath cp = new ColumnPath("Standard1");
      cp.setColumn(bytes("testGetRangeSlices_" + i));

      keyspace.insert(
          "testGetRangeSlices0",
          cp,
          StringSerializer.get().toByteBuffer("testGetRangeSlices_Value_" + i));
      keyspace.insert(
          "testGetRangeSlices1",
          cp,
          StringSerializer.get().toByteBuffer("testGetRangeSlices_Value_" + i));
      keyspace.insert(
          "testGetRangeSlices2",
          cp,
          StringSerializer.get().toByteBuffer("testGetRangeSlices_Value_" + i));
    }

    // get value
    ColumnParent clp = new ColumnParent("Standard1");
    SliceRange sr =
        new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 150);
    SlicePredicate sp = new SlicePredicate();
    sp.setSlice_range(sr);

    KeyRange range = new KeyRange();
    range.setStart_key("".getBytes());
    range.setEnd_key("".getBytes());

    Map<String, List<Column>> keySlices = se.fromBytesMap(keyspace.getRangeSlices(clp, sp, range));

    assertNotNull(keySlices);

    assertNotNull("testGetRangeSlices1 is null", keySlices.get("testGetRangeSlices1"));
    assertEquals(
        "testGetRangeSlices_Value_0",
        string(keySlices.get("testGetRangeSlices1").get(0).getValue()));
    assertEquals(10, keySlices.get("testGetRangeSlices1").size());

    ColumnPath cp = new ColumnPath("Standard1");
    keyspace.remove("testGetRanageSlices0", cp);
    keyspace.remove("testGetRanageSlices1", cp);
    keyspace.remove("testGetRanageSlices2", cp);
  }
  @Override
  public Set<Object> loadAllKeys(Set<Object> keysToExclude) throws CacheLoaderException {
    Cassandra.Client cassandraClient = null;
    try {
      cassandraClient = dataSource.getConnection();
      Set<Object> s = new HashSet<Object>();
      SlicePredicate slicePredicate = new SlicePredicate();
      slicePredicate.setSlice_range(
          new SliceRange(
              ByteBuffer.wrap(entryColumnPath.getColumn()),
              ByteBufferUtil.EMPTY_BYTE_BUFFER,
              false,
              1));
      String startKey = "";
      boolean complete = false;
      // Get the keys in SLICE_SIZE blocks
      while (!complete) {
        KeyRange keyRange = new KeyRange(SLICE_SIZE);
        keyRange.setStart_token(startKey);
        keyRange.setEnd_token("");
        List<KeySlice> keySlices =
            cassandraClient.get_range_slices(
                entryColumnParent, slicePredicate, keyRange, readConsistencyLevel);
        if (keySlices.size() < SLICE_SIZE) {
          complete = true;
        } else {
          startKey = new String(keySlices.get(keySlices.size() - 1).getKey(), UTF8Charset);
        }

        for (KeySlice keySlice : keySlices) {
          if (keySlice.getColumnsSize() > 0) {
            Object key = unhashKey(keySlice.getKey());
            if (key != null && (keysToExclude == null || !keysToExclude.contains(key))) s.add(key);
          }
        }
      }
      return s;
    } catch (Exception e) {
      throw new CacheLoaderException(e);
    } finally {
      dataSource.releaseConnection(cassandraClient);
    }
  }
  @Override
  public void clear() throws CacheLoaderException {
    Cassandra.Client cassandraClient = null;
    try {
      cassandraClient = dataSource.getConnection();
      SlicePredicate slicePredicate = new SlicePredicate();
      slicePredicate.setSlice_range(
          new SliceRange(
              ByteBuffer.wrap(entryColumnPath.getColumn()),
              ByteBufferUtil.EMPTY_BYTE_BUFFER,
              false,
              1));
      String startKey = "";
      boolean complete = false;
      // Get the keys in SLICE_SIZE blocks
      while (!complete) {
        KeyRange keyRange = new KeyRange(SLICE_SIZE);
        keyRange.setStart_token(startKey);
        keyRange.setEnd_token("");
        List<KeySlice> keySlices =
            cassandraClient.get_range_slices(
                entryColumnParent, slicePredicate, keyRange, readConsistencyLevel);
        if (keySlices.size() < SLICE_SIZE) {
          complete = true;
        } else {
          startKey = new String(keySlices.get(keySlices.size() - 1).getKey(), UTF8Charset);
        }
        Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap =
            new HashMap<ByteBuffer, Map<String, List<Mutation>>>();

        for (KeySlice keySlice : keySlices) {
          remove0(ByteBuffer.wrap(keySlice.getKey()), mutationMap);
        }
        cassandraClient.batch_mutate(mutationMap, ConsistencyLevel.ALL);
      }
    } catch (Exception e) {
      throw new CacheLoaderException(e);
    } finally {
      dataSource.releaseConnection(cassandraClient);
    }
  }
  @PooledConnection
  public JSONArray getRows(
      String keyspace, String columnFamily, String queryStr, ConsistencyLevel consistencyLevel)
      throws InvalidRequestException, UnavailableException, TimedOutException, TException,
          CharacterCodingException {
    if (StringUtils.isNotBlank(queryStr)) {
      return getRowsWithQuery(keyspace, columnFamily, queryStr, consistencyLevel);
    }
    SlicePredicate predicate = new SlicePredicate();
    SliceRange range =
        new SliceRange(ByteBufferUtil.bytes(""), ByteBufferUtil.bytes(""), false, MAX_COLUMNS);
    predicate.setSlice_range(range);

    KeyRange keyRange = new KeyRange(MAX_ROWS);
    keyRange.setStart_key(ByteBufferUtil.bytes(""));
    keyRange.setEnd_key(ByteBufferUtil.EMPTY_BYTE_BUFFER);
    ColumnParent parent = new ColumnParent(columnFamily);
    List<KeySlice> rows =
        getConnection(keyspace).get_range_slices(parent, predicate, keyRange, consistencyLevel);
    return JsonMarshaller.marshallRows(rows, true);
  }
	public static void main(String[] args) throws UnsupportedEncodingException,
	InvalidRequestException, UnavailableException, TimedOutException,
	TException, NotFoundException {

		TTransport tr = new TSocket(“192.168.1.204″, 9160);
		TProtocol proto = new TBinaryProtocol(tr);
		Cassandra.Client client = new Cassandra.Client(proto);

		tr.open();

		String keyspace = “Historical_Info”;
		String columnFamily = “Historical_Info_Column”;
		//String keyUserID = “3”;

		// read entire row
		SlicePredicate predicate = new SlicePredicate();
		SliceRange sliceRange = new SliceRange();
		sliceRange.setStart(new byte[0]);
		sliceRange.setFinish(new byte[0]);
		predicate.setSlice_range(sliceRange);

		KeyRange keyrRange = new KeyRange();
		keyrRange.setStart_key(“1″);
		keyrRange.setEnd_key(“”);
		//keyrRange.setCount(100);

		ColumnParent parent = new ColumnParent(columnFamily);

		List < KeySlice > ls = client.get_range_slices(keyspace, parent, predicate, keyrRange, ConsistencyLevel.ONE);

		for (KeySlice result: ls) {
			List < ColumnOrSuperColumn > column = result.columns;
			for (ColumnOrSuperColumn result2: column) {
				Column column2 = result2.column;
				System.out.println(new String(column2.name, UTF8) + ” - > ” + new String(column2.value, UTF8));
			}
		}

		tr.close();
	}
  @Override
  public Set<InternalCacheEntry> load(int numEntries) throws CacheLoaderException {
    Cassandra.Client cassandraClient = null;
    try {
      cassandraClient = dataSource.getConnection();
      Set<InternalCacheEntry> s = new HashSet<InternalCacheEntry>();
      SlicePredicate slicePredicate = new SlicePredicate();
      slicePredicate.setSlice_range(
          new SliceRange(
              ByteBuffer.wrap(entryColumnPath.getColumn()),
              ByteBufferUtil.EMPTY_BYTE_BUFFER,
              false,
              1));
      String startKey = "";

      // Get the keys in SLICE_SIZE blocks
      int sliceSize = Math.min(SLICE_SIZE, numEntries);
      for (boolean complete = false; !complete; ) {
        KeyRange keyRange = new KeyRange(sliceSize);
        keyRange.setStart_token(startKey);
        keyRange.setEnd_token("");
        List<KeySlice> keySlices =
            cassandraClient.get_range_slices(
                entryColumnParent, slicePredicate, keyRange, readConsistencyLevel);

        // Cycle through all the keys
        for (KeySlice keySlice : keySlices) {
          Object key = unhashKey(keySlice.getKey());
          if (key == null) // Skip invalid keys
          continue;
          List<ColumnOrSuperColumn> columns = keySlice.getColumns();
          if (columns.size() > 0) {
            if (log.isDebugEnabled()) {
              log.debugf("Loading %s", key);
            }
            byte[] value = columns.get(0).getColumn().getValue();
            InternalCacheEntry ice = unmarshall(value, key);
            s.add(ice);
          } else if (log.isDebugEnabled()) {
            log.debugf("Skipping empty key %s", key);
          }
        }
        if (keySlices.size() < sliceSize) {
          // Cassandra has returned less keys than what we asked for.
          // Assume we have finished
          complete = true;
        } else {
          // Cassandra has returned exactly the amount of keys we
          // asked for. If we haven't reached the required quota yet,
          // assume we need to cycle again starting from
          // the last returned key (excluded)
          sliceSize = Math.min(SLICE_SIZE, numEntries - s.size());
          if (sliceSize == 0) {
            complete = true;
          } else {
            startKey = new String(keySlices.get(keySlices.size() - 1).getKey(), UTF8Charset);
          }
        }
      }
      return s;
    } catch (Exception e) {
      throw new CacheLoaderException(e);
    } finally {
      dataSource.releaseConnection(cassandraClient);
    }
  }