org.apache.accumulo.core.client.lexicoder
Class PairLexicoder<A extends Comparable<A>,B extends Comparable<B>>
java.lang.Object
org.apache.accumulo.core.client.lexicoder.PairLexicoder<A,B>
- All Implemented Interfaces:
- Encoder<ComparablePair<A,B>>, Lexicoder<ComparablePair<A,B>>, TypedValueCombiner.Encoder<ComparablePair<A,B>>
public class PairLexicoder<A extends Comparable<A>,B extends Comparable<B>>
- extends Object
- implements Lexicoder<ComparablePair<A,B>>
This class is a lexicoder that sorts a ComparablePair. Each item in the pair is encoded with the given lexicoder and concatenated together. This makes it
easy to construct a sortable key based on two components. There are many examples of this- but a key/value relationship is a great one.
If we decided we wanted a two-component key where the first component is a string and the second component a date which is reverse sorted, we can do so with
the following example:
{
@code
StringLexicoder stringEncoder = new StringLexicoder();
ReverseLexicoder<Date> dateEncoder = new ReverseLexicoder<Date>(new DateLexicoder());
PairLexicoder<String,Date> pairLexicoder = new PairLexicoder<String,Date>(stringEncoder, dateEncoder);
byte[] pair1 = pairLexicoder.encode(new ComparablePair<String,Date>("com.google", new Date()));
byte[] pair2 = pairLexicoder.encode(new ComparablePair<String,Date>("com.google", new Date(System.currentTimeMillis() + 500)));
byte[] pair3 = pairLexicoder.encode(new ComparablePair<String,Date>("org.apache", new Date(System.currentTimeMillis() + 1000)));
}
In the example, pair2 will be sorted before pair1. pair3 will occur last since 'org' is sorted after 'com'. If we just used a DateLexicoder
instead
of a ReverseLexicoder
, pair1 would have been sorted before pair2.
- Since:
- 1.6.0
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
PairLexicoder
public PairLexicoder(Lexicoder<A> firstLexicoder,
Lexicoder<B> secondLexicoder)
encode
public byte[] encode(ComparablePair<A,B> data)
- Specified by:
encode
in interface TypedValueCombiner.Encoder<ComparablePair<A extends Comparable<A>,B extends Comparable<B>>>
decode
public ComparablePair<A,B> decode(byte[] data)
- Specified by:
decode
in interface TypedValueCombiner.Encoder<ComparablePair<A extends Comparable<A>,B extends Comparable<B>>>
Copyright © 2015 Apache Accumulo Project. All rights reserved.