【云计算】HBase常用过滤器API操作解析
小标 2018-12-24 来源 : 阅读 942 评论 0

摘要:本文主要向大家介绍了【云计算】HBase常用过滤器API操作解析,通过具体的内容向大家展现,希望对大家学习云计算有所帮助。

本文主要向大家介绍了【云计算】HBase常用过滤器API操作解析,通过具体的内容向大家展现,希望对大家学习云计算有所帮助。


Before


 Table table = null;
 Connection conn = null;
 @Before
 public void init() throws Exception {
  Configuration conf = HBaseConfiguration.create();
  conn = ConnectionFactory.createConnection(conf);
  table = conn.getTable(TableName.valueOf("t_user"));
  
 }


列值过滤器



 /**
  * 列值过滤器
  * @throws Exception 
  * @throws Exception
  */
 @Test
 public void testScanDataByColumnValue() throws Exception {
  Scan scan = new Scan();
  //列值过滤器  参数一:列簇名  参数二:列名  参数三:关系  参数四:值
  SingleColumnValueFilter filter = new SingleColumnValueFilter("base_info".getBytes(), "name".getBytes(), CompareOp.EQUAL, "zhangsan".getBytes());
  scan.setFilter(filter);
  ResultScanner scanner = table.getScanner(scan);
  for (Result result : scanner) {
   //打印行键
   System.out.println(Bytes.toString(result.getRow()));
   //打印base_info列簇里面key为name的value的值
   byte[] value = result.getValue("base_info".getBytes(), "name".getBytes());
   System.out.println(Bytes.toString(value));
   
   byte[] value2 = result.getValue("base_info".getBytes(), "age".getBytes());
   System.out.println(Bytes.toString(value2));
  }
 }


列名前缀过滤器



 /**
  * 列名前缀过滤器
  * @throws Exception 
  * @throws Exception
  */
 @Test
 public void testScanDataByColumn() throws Exception {
  Scan scan = new Scan();
  //带有这个列名的都给过滤出来
  ColumnPrefixFilter filter = new ColumnPrefixFilter(Bytes.toBytes("name"));
  scan.setFilter(filter);
  
  ResultScanner scanner = table.getScanner(scan);
  for (Result result : scanner) {
   System.out.println(Bytes.toString(result.getRow()));
   byte[] value = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
   System.out.println(Bytes.toString(value));
   byte[] age = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
   System.out.println(Bytes.toString(age));
   System.out.println("-----------------------");
  }
  
 }


rowKey过滤器



 /**
  * rowKey过滤器
  * @throws Exception 
  * @throws Exception
  */
 @Test
 public void testScanDataByRowkey() throws Exception {
  Scan scan = new Scan();
  //针对rowkey做过滤
  RowFilter filter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("^rk"));
  
  scan.setFilter(filter);
  ResultScanner scanner = table.getScanner(scan);
  for (Result result : scanner) {
   System.out.println(Bytes.toString(result.getRow()));
   byte[] value = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
   System.out.println(Bytes.toString(value));
   byte[] age = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
   System.out.println(Bytes.toString(age));
   System.out.println("-----------------------");
  }
 }


多个过滤器的联合使用



 /**
  * 多个过滤器的联合使用
  * @throws Exception 
  * @throws Exception
  */
 @Test
 public void testScanListFilter() throws Exception {
  Scan scan = new Scan();
  //过滤器集合
  //满足所有条件
  FilterList list = new FilterList(Operator.MUST_PASS_ALL);
  //满足任意一个条件
  //FilterList list = new FilterList(Operator.MUST_PASS_ONE);
  
  RowFilter filter1 = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("^rk"));
  ColumnPrefixFilter filter2 = new ColumnPrefixFilter(Bytes.toBytes("name"));
  
  //把过滤器添加到集合中
  list.addFilter(filter1);
  list.addFilter(filter2);
  
  //把过滤器集合添加到scan
  scan.setFilter(list);
  
  ResultScanner scanner = table.getScanner(scan);
  for (Result result : scanner) {
   System.out.println(Bytes.toString(result.getRow()));
   byte[] value = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
   System.out.println(Bytes.toString(value));
   byte[] age = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
   System.out.println(Bytes.toString(age));
   System.out.println("-----------------------");
  }
 }


After



 @After
 public void close() throws Exception {
  table.close();
  conn.close();
 }


          

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标大数据云计算大数据安全频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved