Пример #1
0
  @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);
    }
  }
Пример #2
0
  @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);
    }
  }
Пример #3
0
	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();
	}
Пример #4
0
  /**
   * Perform a range scan for a set of records in the database. Each field/value pair from the
   * result will be stored in a HashMap.
   *
   * @param table The name of the table
   * @param startkey The record key of the first record to read.
   * @param recordcount The number of records to read
   * @param fields The list of fields to read, or null for all of them
   * @param result A Vector of HashMaps, where each HashMap is a set field/value pairs for one
   *     record
   * @return Zero on success, a non-zero error code on error
   */
  public int scan(
      String table,
      String startkey,
      int recordcount,
      Set<String> fields,
      Vector<HashMap<String, ByteIterator>> result) {
    if (!_table.equals(table)) {
      try {
        client.set_keyspace(table);
        _table = table;
      } catch (Exception e) {
        e.printStackTrace();
        e.printStackTrace(System.out);
        return Error;
      }
    }

    for (int i = 0; i < OperationRetries; i++) {

      try {
        SlicePredicate predicate;
        if (fields == null) {
          predicate =
              new SlicePredicate()
                  .setSlice_range(new SliceRange(emptyByteBuffer, emptyByteBuffer, false, 1000000));

        } else {
          ArrayList<ByteBuffer> fieldlist = new ArrayList<ByteBuffer>(fields.size());
          for (String s : fields) {
            fieldlist.add(ByteBuffer.wrap(s.getBytes("UTF-8")));
          }

          predicate = new SlicePredicate().setColumn_names(fieldlist);
        }

        KeyRange kr =
            new KeyRange()
                .setStart_key(startkey.getBytes("UTF-8"))
                .setEnd_key(new byte[] {})
                .setCount(recordcount);

        List<KeySlice> results =
            client.get_range_slices(parent, predicate, kr, ConsistencyLevel.ONE);

        if (_debug) {
          System.out.println("Scanning startkey: " + startkey);
        }

        HashMap<String, ByteIterator> tuple;
        for (KeySlice oneresult : results) {
          tuple = new HashMap<String, ByteIterator>();

          Column column;
          String name;
          ByteIterator value;
          for (ColumnOrSuperColumn onecol : oneresult.columns) {
            column = onecol.column;
            name =
                new String(
                    column.name.array(),
                    column.name.position() + column.name.arrayOffset(),
                    column.name.remaining());
            value =
                new ByteArrayByteIterator(
                    column.value.array(),
                    column.value.position() + column.value.arrayOffset(),
                    column.value.remaining());

            tuple.put(name, value);

            if (_debug) {
              System.out.print("(" + name + "=" + value + ")");
            }
          }

          result.add(tuple);
          if (_debug) {
            System.out.println();
          }
        }

        return Ok;
      } catch (Exception e) {
        errorexception = e;
      }
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
      }
    }
    errorexception.printStackTrace();
    errorexception.printStackTrace(System.out);
    return Error;
  }
Пример #5
0
  @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);
    }
  }