Example usage for org.apache.cassandra.db DecoratedKey compareTo

List of usage examples for org.apache.cassandra.db DecoratedKey compareTo

Introduction

In this page you can find the example usage for org.apache.cassandra.db DecoratedKey compareTo.

Prototype

public int compareTo(PartitionPosition pos) 

Source Link

Usage

From source file:com.knewton.mapreduce.util.RandomStudentEventGenerator.java

License:Apache License

/**
 * Helper method for generating decorated increasing student ids. Used by
 * {@link WriteSampleSSTable} for writing a sorted SSTable.
 *
 * @return A list of byte buffers with student IDs
 *///from www  . ja  v  a 2s .com
public static List<ByteBuffer> getStudentIds(int numberOfStudents) {
    long studentId = 1000000L;
    List<ByteBuffer> studentIds = Lists.newArrayListWithCapacity(numberOfStudents);

    for (int i = 0; i < numberOfStudents; i++) {
        ByteBuffer bb = ByteBuffer.wrap(new byte[8]);
        bb.putLong(studentId++);
        bb.rewind();
        studentIds.add(bb);
    }

    Collections.sort(studentIds, new Comparator<ByteBuffer>() {
        @Override
        public int compare(ByteBuffer o1, ByteBuffer o2) {
            DecoratedKey dk1 = StorageService.getPartitioner().decorateKey(o1);
            DecoratedKey dk2 = StorageService.getPartitioner().decorateKey(o2);
            return dk1.compareTo(dk2);
        }
    });

    return studentIds;
}

From source file:net.imagini.cassandra.DumpSSTables.SSTableExport.java

License:Apache License

/**
 * Enumerate row keys from an SSTableReader and write the result to a
 * PrintStream./*  w w  w  .  java2s. com*/
 * 
 * @param desc
 *            the descriptor of the file to export the rows from
 * @param outs
 *            PrintStream to write the output to
 * @throws IOException
 *             on failure to read/write input/output
 */
public static void enumeratekeys(Descriptor desc, PrintStream outs) throws IOException {
    KeyIterator iter = new KeyIterator(desc);
    DecoratedKey lastKey = null;
    while (iter.hasNext()) {
        DecoratedKey key = iter.next();

        // validate order of the keys in the sstable
        if (lastKey != null && lastKey.compareTo(key) > 0)
            throw new IOException("Key out of order! " + lastKey + " > " + key);
        lastKey = key;

        outs.println(ByteBufferUtil.string(key.key));
    }
    iter.close();
    outs.flush();
}

From source file:net.imagini.cassandra.DumpSSTables.SSTableExport.java

License:Apache License

/**
 * Export specific rows from an SSTable and write the resulting JSON to a
 * PrintStream./*from  w  ww.  j av a 2 s  . c  o  m*/
 * 
 * @param desc
 *            the descriptor of the sstable table to read from
 * @param outs
 *            PrintStream to write the output to
 * @param toExport
 *            the keys corresponding to the rows to export
 * @param excludes
 *            keys to exclude from export
 * @throws IOException
 *             on failure to read/write input/output
 */
public static void export(Descriptor desc, PrintStream outs, Collection<String> toExport, String[] excludes)
        throws IOException {
    SSTableReader reader = SSTableReader.open(desc);
    SSTableScanner scanner = reader.getDirectScanner();

    IPartitioner<?> partitioner = reader.partitioner;

    if (excludes != null)
        toExport.removeAll(Arrays.asList(excludes));

    int i = 0;

    // last key to compare order
    DecoratedKey lastKey = null;

    for (String key : toExport) {
        DecoratedKey decoratedKey = partitioner.decorateKey(ByteBufferUtil.hexToBytes(key));

        if (lastKey != null && lastKey.compareTo(decoratedKey) > 0)
            throw new IOException("Key out of order! " + lastKey + " > " + decoratedKey);

        lastKey = decoratedKey;

        scanner.seekTo(decoratedKey);

        if (!scanner.hasNext())
            continue;

        SSTableIdentityIterator row = (SSTableIdentityIterator) scanner.next();
        if (!row.getKey().equals(decoratedKey))
            continue;

        serializeRow(row, decoratedKey, outs);

        if (i != 0)
            outs.println(",");

        i++;
    }

    outs.println("\n");
    outs.flush();

    scanner.close();
}