这篇都是纯代码,看基础的可以查看 https://blog.csdn.net/weixin_41122339/article/details/81905663
zhe这篇不会像之前写的那么详细了,我就补充一下特殊的属性和方法
1,增删改查增强
package myhbasedemo; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; 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.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; import org.junit.Test; public class demo { /** * get方式查询数据 * @throws IOException */ @Test public void getdata() throws IOException { Configuration conf = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(conf); Table tab = conn.getTable(TableName.valueOf("zxz", "testZ")); Get get=new Get(Bytes.toBytes("2"));//创建get对象指定rowkey Result re = tab.get(get);//提交get操作返回一个一个结果集 List<Cell> cells = re.getColumnCells(Bytes.toBytes("f2"),Bytes.toBytes("weidu")); for (Cell cell : cells) { System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));//使用cellutil这个工具类的clonevalue方法来获取值,在把他转成string类型,要不会乱码 } } @Test public void getdata1() throws IOException { Configuration conf = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(conf); Table tab = conn.getTable(TableName.valueOf("zxz", "testZ")); Get get=new Get(Bytes.toBytes("2"));//创建get对象指定rowkey //get可以设置一下属性 get.setMaxVersions(7);//设置最大版本数 get.setTimeRange(1000000L, 10000000L);//可以设置查询时会安装时间戳范围查 Result re = tab.get(get);//提交get操作返回一个一个结果集 List<Cell> cells = re.getColumnCells(Bytes.toBytes("f2"),Bytes.toBytes("weidu")); for (Cell cell : cells) { System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));//使用cellutil这个工具类的clonevalue方法来获取值,在把他转成string类型,要不会乱码 } } //批量put数据 @Test public void putdata() throws IOException { Configuration conf = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(conf); Table tab = conn.getTable(TableName.valueOf("zxz", "testZ")); List<Put> puts=new ArrayList<>(); for(int i=1;i<=10000;i++) { Put put=new Put(Bytes.toBytes("row"+i)); put.addColumn(Bytes.toBytes("z1"), Bytes.toBytes("id"),Bytes.toBytes(i) ); puts.add(put); } tab.put(puts); } /** * 通过扫描器缓存实现出现 */ @Test public void scannerCache() throws Exception { Configuration conf = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(conf); Table table = conn.getTable(TableName.valueOf("t1")); Scan scan = new Scan(); // 查询缓存 // System.out.println(scan.getCaching()); scan.setStartRow(Bytes.toBytes("row1")); scan.setStopRow(Bytes.toBytes("row999999")); scan.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name")); scan.setCaching(10000); // 缓存10000 scan.setBatch(3); ResultScanner s = table.getScanner(scan); Iterator<Result> it = s.iterator(); long start = System.currentTimeMillis(); while (it.hasNext()) { Result r = it.next(); String str = Bytes.toString(r.getColumnLatestCell(Bytes.toBytes("f1"), Bytes.toBytes("name")).getValue()); // System.out.println(str); } System.out.println(System.currentTimeMillis() - start); } /** * 通过扫描器缓存和batch组合出现 */ @Test public void scannerCacheNBatch() throws Exception { Configuration conf = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(conf); Table table = conn.getTable(TableName.valueOf("t4")); Scan scan = new Scan(); scan.setCaching(3); scan.setBatch(1); ResultScanner s = table.getScanner(scan); Iterator<Result> it = s.iterator(); while (it.hasNext()) { System.out.println("===================="); Result r = it.next(); System.out.println(toStr(r,"f1","age")); System.out.println(toStr(r,"f1","id")); System.out.println(toStr(r,"f1","name")); System.out.println(toStr(r,"f2","age")); System.out.println(toStr(r,"f2","id")); System.out.println(toStr(r,"f2","name")); } } private String toStr(Result r, String f1,String c1){ Cell cell = r.getColumnLatestCell(Bytes.toBytes(f1), Bytes.toBytes(c1)); if(cell != null){ String row = Bytes.toString(cell.getRow()); return new String(row + "/" + f1 + ":" + c1 + "=" + Bytes.toString(cell.getValue())); } return new String("/" + f1 + ":" + c1 + "= null "); } }
2,filter过滤器
org.apache.hadoop.hbase.filter.Filter org.apache.hadoop.hbase.filter.FilterBase org.apache.hadoop.hbase.filter.ColumnCountGetFilter org.apache.hadoop.hbase.filter.ColumnPaginationFilter org.apache.hadoop.hbase.filter.ColumnPrefixFilter org.apache.hadoop.hbase.filter.ColumnRangeFilter org.apache.hadoop.hbase.filter.CompareFilter org.apache.hadoop.hbase.filter.DependentColumnFilter org.apache.hadoop.hbase.filter.FamilyFilter org.apache.hadoop.hbase.filter.QualifierFilter org.apache.hadoop.hbase.filter.RowFilter org.apache.hadoop.hbase.filter.ValueFilter org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter org.apache.hadoop.hbase.filter.FirstKeyValueMatchingQualifiersFilter org.apache.hadoop.hbase.filter.FuzzyRowFilter org.apache.hadoop.hbase.filter.InclusiveStopFilter org.apache.hadoop.hbase.filter.KeyOnlyFilter org.apache.hadoop.hbase.filter.MultipleColumnPrefixFilter org.apache.hadoop.hbase.filter.PageFilter org.apache.hadoop.hbase.filter.PrefixFilter org.apache.hadoop.hbase.filter.RandomRowFilter org.apache.hadoop.hbase.filter.SingleColumnValueFilter org.apache.hadoop.hbase.filter.SingleColumnValueExcludeFilter org.apache.hadoop.hbase.filter.SkipFilter org.apache.hadoop.hbase.filter.TimestampsFilter org.apache.hadoop.hbase.filter.WhileMatchFilter org.apache.hadoop.hbase.filter.FilterList
上面就是hbase集成的filter过滤器,在这里我们介绍几个常用的,剩下的大家自己试试吧
1,比较过滤器
他们通常会有两个参数,第一个参数可以使用下面的值
第二个参数他们会使用哪种类型的比较器
package myhbasedemo; import java.util.Iterator; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; 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.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.filter.BinaryComparator; import org.apache.hadoop.hbase.filter.ColumnPaginationFilter; import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; import org.apache.hadoop.hbase.filter.FamilyFilter; import org.apache.hadoop.hbase.filter.FilterList; import org.apache.hadoop.hbase.filter.KeyOnlyFilter; import org.apache.hadoop.hbase.filter.PageFilter; import org.apache.hadoop.hbase.filter.QualifierFilter; import org.apache.hadoop.hbase.filter.RegexStringComparator; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.filter.RowFilter; import org.apache.hadoop.hbase.filter.SingleColumnValueExcludeFilter; import org.apache.hadoop.hbase.filter.SubstringComparator; import org.apache.hadoop.hbase.filter.ValueFilter; import org.junit.Test; public class filterdemo { //创建一个工具类避免使用重复的代码,来获取值 public String tostr(Result re,String cf,String c) { Cell cell = re.getColumnLatestCell(Bytes.toBytes(cf),Bytes.toBytes(c)); if(cell !=null) { String row = Bytes.toString(CellUtil.cloneRow(cell)); int value = Bytes.toInt(CellUtil.cloneValue(cell)); return new String(row +" "+cf+" "+c+" "+value); } return new String(cf+" "+c+"=null"); } /** * 使用rowkey过滤器,字节对比器 * @throws Exception */ @Test public void filter1() throws Exception { Configuration conf = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(conf); Table tab = conn.getTable(TableName.valueOf("zxz","testZ")); Scan scan =new Scan(); //创建RowFilter过滤器,条件为,使用字节对比器结果返回rowkey小于10的数据 scan.setFilter(new RowFilter(CompareOp.LESS,new BinaryComparator(Bytes.toBytes("row10")))); ResultScanner re = tab.getScanner(scan); Iterator<Result> it = re.iterator(); while(it.hasNext()) { Result res = it.next(); System.out.println(tostr(res, "z1", "id")); System.out.println(tostr(res, "z2", "id")); } } /** * 使用容rowkey过滤器,正则串对比器 */ @Test public void rowFilterRegex() throws Exception{ Configuration conf = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(conf); Table t = conn.getTable(TableName.valueOf("zxz","testZ")); Scan scan = new Scan(); //创建RowFilter过滤器,条件为,使用正则对比器返回rowkey含有123内容的数据 scan.setFilter(new RowFilter(CompareOp.EQUAL, new RegexStringComparator(".*[123]"))); ResultScanner scanner = t.getScanner(scan); Iterator<Result> it = scanner.iterator(); while(it.hasNext()){ Result res = it.next(); System.out.println(tostr(res, "z1", "id")); System.out.println(tostr(res, "z2", "id")); } } /** * 使用rowkey过滤器,子串对比器: */ @Test public void rowFilterSubstring() throws Exception{ Configuration conf = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(conf); Table t = conn.getTable(TableName.valueOf("zxz","testZ")); Scan scan = new Scan(); //创建RowFilter过滤器,条件为使用字串对比器返回rowkey中含有w5的数据 scan.setFilter(new RowFilter(CompareOp.EQUAL, new SubstringComparator("w5"))); ResultScanner scanner = t.getScanner(scan); Iterator<Result> it = scanner.iterator(); while(it.hasNext()){ Result res = it.next(); System.out.println(tostr(res, "z1", "id")); System.out.println(tostr(res, "z2", "id")); } } /** * 列族过滤器:字节对比器 */ @Test public void familyFilter() throws Exception{ Configuration conf = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(conf); Table t = conn.getTable(TableName.valueOf("zxz","testZ")); Scan scan = new Scan(); //创建FamilyFilter过滤器,条件为使用字节对比器返回列族为z2的数据 scan.setFilter(new FamilyFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("z2")))); ResultScanner scanner = t.getScanner(scan); Iterator<Result> it = scanner.iterator(); while(it.hasNext()){ Result res = it.next(); System.out.println(tostr(res, "z1", "id")); System.out.println(tostr(res, "z2", "id")); } } /** * 列过滤器: Qualifier,字节对比器 */ @Test public void qualifierFilter() throws Exception{ Configuration conf = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(conf); Table t = conn.getTable(TableName.valueOf("zxz","testZ")); Scan scan = new Scan(); //创建QualifierFilter过滤器,条件为,使用字节对比器返回列为id的数据 scan.setFilter(new QualifierFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("id")))); ResultScanner scanner = t.getScanner(scan); Iterator<Result> it = scanner.iterator(); while(it.hasNext()){ Result res = it.next(); System.out.println(tostr(res, "z1", "id")); System.out.println(tostr(res, "z2", "id")); } } /** * 值过滤器: ValueFilter,字节对比器 */ @Test public void valueFilter() throws Exception{ Configuration conf = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(conf); Table t = conn.getTable(TableName.valueOf("zxz","testZ")); Scan scan = new Scan(); //创建ValueFilter过滤器,条件为使用字节对比器返回值小于9999的数据 scan.setFilter(new ValueFilter(CompareOp.LESS, new BinaryComparator(Bytes.toBytes("9999")))); ResultScanner scanner = t.getScanner(scan); Iterator<Result> it = scanner.iterator(); while(it.hasNext()){ Result res = it.next(); System.out.println(tostr(res, "z1", "id")); System.out.println(tostr(res, "z2", "id")); } }
2,专用过滤器(不在使用对比器,反而很简单)
/** * 单列值过滤 */ @Test public void singleColValFilter() throws Exception{ Configuration conf = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(conf); Table t = conn.getTable(TableName.valueOf("zxz","testZ")); Scan scan = new Scan(); SingleColumnValueExcludeFilter f = new SingleColumnValueExcludeFilter(Bytes.toBytes("z1"), Bytes.toBytes("id"),CompareOp.EQUAL, Bytes.toBytes("999")); scan.setFilter(f); ResultScanner scanner = t.getScanner(scan); Iterator<Result> it = scanner.iterator(); while(it.hasNext()){ Result res = it.next(); System.out.println(tostr(res, "z1", "id")); System.out.println(tostr(res, "z2", "id")); } } /** * 分页过滤器,类似于limit */ @Test public void pageFilter() throws Exception{ Configuration conf = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(conf); Table t = conn.getTable(TableName.valueOf("t4")); Scan scan = new Scan(); PageFilter f = new PageFilter(5); scan.setFilter(f); ResultScanner scanner = t.getScanner(scan); Iterator<Result> it = scanner.iterator(); while(it.hasNext()){ Result res = it.next(); System.out.println(tostr(res, "z1", "id")); System.out.println(tostr(res, "z2", "id")); } } /** * 列分页过滤器,类似于limit */ @Test public void columnPageFilter() throws Exception{ Configuration conf = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(conf); Table t = conn.getTable(TableName.valueOf("zxz","testZ")); Scan scan = new Scan(); ColumnPaginationFilter f = new ColumnPaginationFilter(2, 1); scan.setFilter(f); ResultScanner scanner = t.getScanner(scan); Iterator<Result> it = scanner.iterator(); while(it.hasNext()){ Result res = it.next(); System.out.println(tostr(res, "z1", "id")); System.out.println(tostr(res, "z2", "id")); } } /** * 只返回key,不返回value */ @Test public void keyOnlyFilter() throws Exception{ Configuration conf = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(conf); Table t = conn.getTable(TableName.valueOf("zxz","testZ")); Scan scan = new Scan(); KeyOnlyFilter f = new KeyOnlyFilter(); scan.setFilter(f); ResultScanner scanner = t.getScanner(scan); Iterator<Result> it = scanner.iterator(); while(it.hasNext()){ Result res = it.next(); System.out.println(tostr(res, "z1", "id")); System.out.println(tostr(res, "z2", "id")); } } /** *过滤器链,FilterList, */ @Test public void filterList() throws Exception{ Configuration conf = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(conf); Table t = conn.getTable(TableName.valueOf("zxz","testZ")); Scan scan = new Scan(); FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ALL); //使用过滤器链链接下面两个过滤条件 //MUST_PASS_ALL属性是把下面的条件全部执行 MUST_PASS_ONE是任意一个条件成功就行 list.addFilter(new FamilyFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("z1")))); list.addFilter(new QualifierFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("id")))); ResultScanner scanner = t.getScanner(scan); Iterator<Result> it = scanner.iterator(); while(it.hasNext()){ Result res = it.next(); System.out.println(tostr(res, "z1", "id")); System.out.println(tostr(res, "z2", "id")); }