import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.metrics.ScanMetrics; import java.io.IOException; /**
* Created by similarface on 16/8/23.
*/
public class ScanDataUseCache {
private static Table table=null;
public static Table getTable() {
if(table==null){
try {
Configuration configuration = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(configuration);
//建立表的连接
return connection.getTable(TableName.valueOf("testtable"));
}catch (IOException e){
return table;
}
}
return table;
}
private static void scan(int caching,int batch,boolean small) {
int count=0;
//setCaching 设置的值为每次rpc的请求记录数,默认是1;cache大可以优化性能,但是太大了会花费很长的时间进行一次传输。
//setBatch 设置每次取的column size;有些row特别大,所以需要分开传给client,就是一次传一个row的几个column。
//setSmall 是否为小扫描
//setScanMetricsEnabled 使用了集合
Scan scan = new Scan().setCaching(caching).setBatch(batch).setSmall(small).setScanMetricsEnabled(true);
ResultScanner scanner=null;
try {
scanner = getTable().getScanner(scan);
}catch (IOException e){
System.out.println(e);
}
if (scanner!=null){
for (Result result:scanner){
count++;
}
scanner.close();
ScanMetrics metrics = scan.getScanMetrics();
System.out.println("Caching: " + caching + ", Batch: " + batch + ", Small: " + small + ", Results: " + count + ", RPCs: " + metrics.countOfRPCcalls);
}
else {
System.out.println("Error");
}
} public static void main(String[] args) throws IOException {
// Caching: 1, Batch: 1, Small: false, Results: 9, RPCs: 12
scan(1, 1, false); //Caching: 1, Batch: 0, Small: false, Results: 4, RPCs: 7
scan(1, 0, false); // Caching: 1, Batch: 0, Small: true, Results: 4, RPCs: 0
scan(1, 0, true); //Caching: 200, Batch: 1, Small: false, Results: 9, RPCs: 3
scan(200, 1, false); //Caching: 200, Batch: 0, Small: false, Results: 4, RPCs: 3
scan(200, 0, false); //Caching: 200, Batch: 0, Small: true, Results: 4, RPCs: 0
scan(200, 0, true); // Caching: 2000, Batch: 100, Small: false, Results: 4, RPCs: 3
scan(2000, 100, false); // Caching: 2, Batch: 100, Small: false, Results: 4, RPCs: 5
scan(2, 100, false); // Caching: 2, Batch: 10, Small: false, Results: 4, RPCs: 5
scan(2, 10, false); // Caching: 2, Batch: 10, Small: false, Results: 4, RPCs: 5
scan(5, 100, false); // Caching: 5, Batch: 100, Small: false, Results: 4, RPCs: 3
scan(5, 20, false); // Caching: 10, Batch: 10, Small: false, Results: 4, RPCs: 3
scan(10, 10, false);
}
} /**
Caching: 1, Batch: 0, Small: false, Results: 5, RPCs: 8
Caching: 1, Batch: 0, Small: true, Results: 5, RPCs: 0
Caching: 200, Batch: 1, Small: false, Results: 1009, RPCs: 8
Caching: 200, Batch: 0, Small: false, Results: 5, RPCs: 3
Caching: 200, Batch: 0, Small: true, Results: 5, RPCs: 0
Caching: 2000, Batch: 100, Small: false, Results: 14, RPCs: 3
Caching: 2, Batch: 100, Small: false, Results: 14, RPCs: 10
Caching: 2, Batch: 10, Small: false, Results: 104, RPCs: 55
Caching: 5, Batch: 100, Small: false, Results: 14, RPCs: 5
Caching: 5, Batch: 20, Small: false, Results: 54, RPCs: 13
Caching: 10, Batch: 10, Small: false, Results: 104, RPCs: 13
**/

这是一个9行数据的表

每行包含一些列

使用缓存为6  批量为3的扫描器

需要3个RPC

3个列装入一个Result实例

6个result到缓存中 组成一个RPC

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.metrics.ScanMetrics; import java.io.IOException; /**
* Created by similarface on 16/8/24.
*/
public class ScanWithOffsetAndLimit {
private static Table table = null; public static Table getTable() {
if (table == null) {
try {
Configuration configuration = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(configuration);
//建立表的连接
return connection.getTable(TableName.valueOf("testtable"));
} catch (IOException e) {
return table;
}
}
return table;
} /**
* 遍历访问数据
* @param num 运行次序
* @param caching
* @param batch
* @param offset
* @param maxResults
* @param maxResultSize
* @param dump
* @throws IOException
*/
private static void scan(int num, int caching, int batch, int offset, int maxResults, int maxResultSize, boolean dump
) throws IOException {
int count = 0;
Scan scan = new Scan().setCaching(caching).setBatch(batch)
.setRowOffsetPerColumnFamily(offset)
.setMaxResultsPerColumnFamily(maxResults)
.setMaxResultSize(maxResultSize)
.setScanMetricsEnabled(true);
ResultScanner scanner = getTable().getScanner(scan);
System.out.println("Scan #" + num + " running...");
for (Result result : scanner) {
count++;
if (dump)
System.out.println("Result [" + count + "]:" + result);
}
scanner.close();
ScanMetrics metrics = scan.getScanMetrics();
System.out.println("Caching: " + caching + ", Batch: " + batch +
", Offset: " + offset + ", maxResults: " + maxResults +
", maxSize: " + maxResultSize + ", Results: " + count +
", RPCs: " + metrics.countOfRPCcalls);
} public static void main(String[] args) throws IOException {
//偏移为0 最大2个cell 所以会扫描到列1 和列2
scan(1, 11, 0, 0, 2, -1, true);
//偏移为4 最大2个cell 所以会扫描到列5 和列6
scan(2, 11, 0, 4, 2, -1, true);
//
scan(3, 5, 0, 0, 2, -1, false);
scan(4, 11, 2, 0, 5, -1, true);
scan(5, 11, -1, -1, -1, 1, false);
scan(6, 11, -1, -1, -1, 10000, false);
}
} /**
Caching: 11, Batch: 0, Offset: 0, maxResults: 2, maxSize: -1, Results: 5005, RPCs: 458
Caching: 11, Batch: 0, Offset: 4, maxResults: 2, maxSize: -1, Results: 1, RPCs: 3
Caching: 5, Batch: 0, Offset: 0, maxResults: 2, maxSize: -1, Results: 5005, RPCs: 1004
Caching: 11, Batch: 2, Offset: 0, maxResults: 5, maxSize: -1, Results: 5009, RPCs: 458
Caching: 11, Batch: -1, Offset: -1, maxResults: -1, maxSize: 1, Results: 5005, RPCs: 11012
Caching: 11, Batch: -1, Offset: -1, maxResults: -1, maxSize: 10000, Results: 5005, RPCs: 469
**/

Hbase之缓存扫描加快读取速度的更多相关文章

  1. ASP.NET状缓存Cache的应用-提高数据库读取速度

    原文:ASP.NET状缓存Cache的应用-提高数据库读取速度 一. Cache概述       既然缓存中的数据其实是来自数据库的,那么缓存中的数据如何和数据库进行同步呢?一般来说,缓存中应该存放改 ...

  2. 优化SQLServer数据库加快查询速度

    查询速度慢的原因很多,常见如下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计算列导致查询不优化. 4.内存不足 ...

  3. 使用Openresty加快网页速度

    新年快乐~~~ 上一篇文章讲到使用多级缓存来减少数据库的访问来加快网页的速度,只是,仍旧没有"嗖"一下就加载出来的感觉,想再优化一下,优化代码什么的已经到了极限.上周无意中看到了o ...

  4. mysql千万级数据库插入速度和读取速度的调整记录

    一般情况下mysql上百万数据读取和插入更新是没什么问题了,但到了上千万级就会出现很慢,下面我们来看mysql千万级数据库插入速度和读取速度的调整记录吧. 1)提高数据库插入性能中心思想:尽量将数据一 ...

  5. 数据读取速度达1.5G/s,UFS 2.1存储技术曝光

    目前最快的是苹果NVME,当然UFS2.1也不差 iPhone6s与iPhone6s Plus在硬件的规格上有了很大的提升,但是它们身上的变化远没有苹果在发布会上所提到的A9处理器.1200万摄像头以 ...

  6. 160304-01、mysql数据库插入速度和读取速度的调整记录

    需求:由于项目变态,需要在一个比较短时间段急剧增加数据库记录(两三天内,由于0增加至5亿).在整个过程调优过程非常艰辛 思路: (1)提高数据库插入性能中心思想:尽量将数据一次性写入到Data Fil ...

  7. mysql千万级数据库插入速度和读取速度的调整

    mysql上百万数据读取和插入更新一般没什么问题,但上千万后速度会很慢,如何调整配置,提高效率.如下: 1.尽量将数据一次性写入DataFile和减少数据库的checkpoint操作,调整如下参数: ...

  8. Linux检测硬盘读取速度

    1. 清空缓存 > /proc/sys/vm/drop_caches 2. 测试读取速度 a. 将/dev/zero中数据按1M的数据单位写入testfile,共写512个单位,并不通过缓存 c ...

  9. Android开发之制作圆形头像自定义View,直接引用工具类,加快开发速度。带有源代码学习

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 博客园主页:http://www.cnblogs.com/mcxiaobing ...

随机推荐

  1. uva 437,巴比伦塔

    题目链接:https://uva.onlinejudge.org/external/4/437.pdf 题意:巴比伦塔: 给出n种立方体,一个立方体能放到另一个立方体上,必须满足,底面一定要小于下面的 ...

  2. HDU(2485),最小割最大流

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2485 Destroying the bus stations Time Limit: 40 ...

  3. winform中treeview中节点选中的技巧

    我想实现譬如选择某子节点的时候,父节点会自动选中,如果选择父节点,子节点会全部选中,如果子节点全部不选,父节点也要不选. 贴代码 private void tvwMenu_AfterCheck(obj ...

  4. [webkit移动开发笔记]之如何去除android上a标签产生的边框

    去年年底,做完最后一个项目就可以开开心心回家,可是在测试阶段,发现了不少bug,为了不影响回家时间,加班加点也要解决这些问题,这里算是工作回忆,也算是工作的一点小总结. 在ios4+和android2 ...

  5. DEBUG模式开关

    在.NET中,有一个特殊的特性可以用:[Conditional("DEBUG")]MyConstructor(IExtensionManager mgr){...}

  6. OLTP基准测试脚本

    关键语句:ll /local/sysbenchtest/sysbench-0.5/sysbench/tests/db--查看lua脚本/usr/local/mysql/bin/mysql -u roo ...

  7. Spring 框架 详解 (三)-----IOC装配Bean

    IOC装配Bean: 1.1.1 Spring框架Bean实例化的方式: 提供了三种方式实例化Bean. * 构造方法实例化:(默认无参数) * 静态工厂实例化: * 实例工厂实例化: 无参数构造方法 ...

  8. Spring注解【非单例】

    花了至少一整天的时间解决了这个问题,必须记录这个纠结的过程,问题不可怕,思路很绕弯. 为了能说清楚自己的问题,我都用例子来模拟. 我有一个类MyThread是这样的: @Service public ...

  9. git push 403

    1. 在github上新建一个空项目. 2. git clone 到本地仓库. 3. git add [一些文件]. 4. git commit -m "first commit" ...

  10. CVE-2015-7547

    危险漏洞补丁修复通知 漏洞编号 漏洞编号为CVE-2015-7547 漏洞说明: Google安全团队近日发现glibc存在的溢出漏洞. glibc的DNS客户端解析器中存在基于栈的缓冲区溢出漏洞.当 ...