Skip to content

Instantly share code, notes, and snippets.

@robdaemon
Created March 20, 2013 23:46
Show Gist options
  • Save robdaemon/5209547 to your computer and use it in GitHub Desktop.
Save robdaemon/5209547 to your computer and use it in GitHub Desktop.
Every once in a while you need to modify an HBase KeyValue in various ways depending on conditional logic. This is a simple builder pattern applied to a KeyValue with a copy constructor.
package com.simplymeasured.doormouse.mapreduce.utils;
import org.apache.hadoop.hbase.KeyValue;
/**
* Stupid-simple builder pattern applied to the HBase KeyValue class.
*
* Makes it easier to manipulate KeyValues in map/reduce jobs
*
* @author rob@simplymeasured.com
* @since 3/11/13
*/
public class KeyValueBuilder {
private byte[] row;
private byte[] family;
private byte[] qualifier;
private byte[] value;
private long timestamp;
public KeyValueBuilder() {
}
public KeyValueBuilder(KeyValue kv) {
this.row = kv.getRow();
this.family = kv.getFamily();
this.qualifier = kv.getQualifier();
this.value = kv.getValue();
this.timestamp = kv.getTimestamp();
}
public byte[] getRow() {
return row;
}
public KeyValueBuilder setRow(byte[] row) {
this.row = row;
return this;
}
public byte[] getFamily() {
return family;
}
public KeyValueBuilder setFamily(byte[] family) {
this.family = family;
return this;
}
public byte[] getQualifier() {
return qualifier;
}
public KeyValueBuilder setQualifier(byte[] qualifier) {
this.qualifier = qualifier;
return this;
}
public byte[] getValue() {
return value;
}
public KeyValueBuilder setValue(byte[] value) {
this.value = value;
return this;
}
public long getTimestamp() {
return timestamp;
}
public KeyValueBuilder setTimestamp(long timestamp) {
this.timestamp = timestamp;
return this;
}
public KeyValue build() {
return new KeyValue(row, family, qualifier, timestamp, value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment