频道栏目
首页 > 网络 > 云计算 > 正文

注册送白菜38

2018-09-11 14:39:12           
收藏   我要投稿

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();
	}
上一篇:安装配置yum源9.6教程
下一篇:Jumpserver3.0安装部署文档教程
相关文章
图文推荐
热门新闻

关于我们 | 联系我们 | 服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑--致力于做实用的IT技术学习网站