Skip to content

Instantly share code, notes, and snippets.

@MikeThomsen
Last active September 13, 2019 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeThomsen/ed3e742d13adeb63d45ceca7a09bf176 to your computer and use it in GitHub Desktop.
Save MikeThomsen/ed3e742d13adeb63d45ceca7a09bf176 to your computer and use it in GitHub Desktop.
Custom filter that fails
import com.google.protobuf.InvalidProtocolBufferException;
import filters.generated.FilterProtos;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellBuilder;
import org.apache.hadoop.hbase.CellBuilderFactory;
import org.apache.hadoop.hbase.CellBuilderType;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.exceptions.DeserializationException;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterBase;
import java.io.IOException;
public class HBTag {
public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "localhost:2181");
conf.set("zookeeper.znode.parent", "/hbase");
Connection conn = ConnectionFactory.createConnection(conf);
Table table = conn.getTable(TableName.valueOf("filter_test"));
Get get = new Get("row-1".getBytes());
get = get.setFilter(new SimpleFilter());
System.out.println("Fetching...");
Result result = table.get(get);
System.out.println("Fetched?");
System.out.println("Val: " + new String(result.getValue("data".getBytes(), "message".getBytes())));
}
}
class SimpleFilter extends FilterBase {
@Override
public Cell transformCell(Cell v) {
System.out.println("\n\n\n\n\ntransforming....\n\n\n\n\n");
if (v.getFamilyArray() == "data".getBytes() && v.getQualifierArray() == "message".getBytes()) {
CellBuilder builder = CellBuilderFactory.create(CellBuilderType.DEEP_COPY);
return builder.setFamily(v.getFamilyArray()).setQualifier(v.getQualifierArray())
.setRow(v.getRowArray()).setValue("I have been transformed".getBytes()).build();
}
return v;
}
public boolean filterRow() throws IOException {
return false;
}
@Override
public byte [] toByteArray() {
FilterProtos.SimpleFilter.Builder builder =
FilterProtos.SimpleFilter.newBuilder();
return builder.build().toByteArray();
}
//@Override
public static Filter parseFrom(final byte[] pbBytes)
throws DeserializationException {
FilterProtos.SimpleFilter proto;
try {
proto = FilterProtos.SimpleFilter.parseFrom(pbBytes); // co SimpleFilter-7-Read Used by the servers to establish the filter instance with the correct values.
} catch (InvalidProtocolBufferException e) {
throw new DeserializationException(e);
}
return new SimpleFilter();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment