本文共 2542 字,大约阅读时间需要 8 分钟。
1.代码
测试数据:
package hbase.hbasseAPI.Filter;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;import org.apache.hadoop.hbase.filter.CompareFilter;import org.apache.hadoop.hbase.filter.DependentColumnFilter;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;/** * 参考列过滤器 * public DependentColumnFilter(final byte [] family, final byte[] qualifier, * final boolean dropDependentColumn, final CompareOp valueCompareOp, * final ByteArrayComparable valueComparator) * family 和qualifier是选择参考列,会获取参考列时间戳(只是针对参考列的时间戳和该列所在行的其他字段的时间戳的比较) * dropDependentColumn,作为参考列 ,false输出,true 不输出, * CompareOp,//加上这个条件后,限定这个条件输出结果 * ByteArrayComparable */public class MyDependColFilter { public static void main(String[] args) throws IOException { System.setProperty("hadoop.home.dir", "E:\\software\\bigdate\\hadoop-2.6.0-cdh5.15.0\\hadoop-2.6.0-cdh5.15.0"); Configuration conf = new Configuration(); conf.set("zookeeper.znode.parent", "/hbase"); conf.set("hbase.zookeeper.quorum", "wangfutai"); conf.set("hbase.zookeeper.property.clientPort", "2181"); Connection connection = ConnectionFactory.createConnection(conf); TableName tableName = TableName.valueOf("hadoop:human"); Table table = connection.getTable(tableName); Scan scan = new Scan(); DependentColumnFilter dependentColumnFilter = new DependentColumnFilter( Bytes.toBytes("info"), Bytes.toBytes("name"), false, CompareFilter.CompareOp.EQUAL,//加上这个条件后,限定这个条件输出结果 new BinaryPrefixComparator(Bytes.toBytes("wangfutai"))); scan.setFilter(dependentColumnFilter); ResultScanner scanner = table.getScanner(scan); for (Result tmp : scanner) { System.out.println(tmp); } scanner.close(); }}
输出结果:
keyvalues={1/info:age/1547176413283/Put/vlen=3/seqid=0, 1/info:name/1547176413283/Put/vlen=9/seqid=0}keyvalues={12/info:age/1547526043294/Put/vlen=4/seqid=0, 12/info:id/1547526043294/Put/vlen=4/seqid=0, 12/info:name/1547526043294/Put/vlen=9/seqid=0}keyvalues={6/info:id/1547526231270/Put/vlen=4/seqid=0, 6/info:name/1547526231270/Put/vlen=9/seqid=0}keyvalues={8/info:id/1547526231270/Put/vlen=4/seqid=0, 8/info:name/1547526231270/Put/vlen=9/seqid=0}keyvalues={9/info:name/1547526231270/Put/vlen=9/seqid=0}
结果分析:
转载地址:http://dgjxi.baihongyu.com/