欢迎投稿

今日深度:

Hbase 缓存,

Hbase 缓存,


package com.mao.hbase;

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.util.Bytes;
import org.junit.Test;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.NavigableMap;

public class HbaseCacheTest {

    private Connection hbaseConn() throws IOException {
        //创建conf对象
        Configuration conf = HBaseConfiguration.create();
        //通过连接工厂创建连接对象
        return ConnectionFactory.createConnection(conf);
    }

    /**
     *  * hbase(main):009:0> scan 'ns1:t7'
     *      * ROW                           COLUMN+CELL
     *      *  row1                         column=f1:id, timestamp=1555471778281, value=1
     *      *  row1                         column=f2:addr, timestamp=1555471794994, value=shanghai
     *      *  row1                         column=f2:age, timestamp=1555471806494, value=12
     *      *  row1                         column=f2:id, timestamp=1555471814355, value=2
     *      *  row1                         column=f2:name, timestamp=1555471832130, value=tom
     *      *  row2                         column=f1:id, timestamp=1555471867622, value=3
     *      *  row2                         column=f1:name, timestamp=1555471881982, value=tom2.1
     *      *  row2                         column=f2:addr, timestamp=1555471901735, value=sichuan
     *      *  row2                         column=f2:age, timestamp=1555471910637, value=13
     *      *  row2                         column=f2:id, timestamp=1555471918777, value=4
     *      *  row2                         column=f2:name, timestamp=1555471928462, value=tom3.1
     *      *  row3                         column=f1:age, timestamp=1555474209734, value=14
     *      *  row3                         column=f1:id, timestamp=1555474223752, value=5
     *      *  row3                         column=f1:name, timestamp=1555474236507, value=tom4.1
     *      *  row3                         column=f2:addr, timestamp=1555474250923, value=chengdu
     *      *  row3                         column=f2:age, timestamp=1555474260092, value=15
     *      *  row3                         column=f2:id, timestamp=1555474266861, value=6
     *      *  row3                         column=f2:name, timestamp=1555474281534, value=tom5.2
     */

    /**
     * 扫描器缓存(面向行级别的)
     *
     * cache row nums : 1000			//632
     * 	cache row nums : 5000			//423
     * 	cache row nums : 1				//7359
     * 	时间长短跟内存相关
     * @throws IOException
     */
    @Test
    public void getScanCache() throws IOException {

        TableName tableName = TableName.valueOf("ns1:t1");
        Scan scan = new Scan();
        scan.setCaching(1000); //设置缓存
        Table t = hbaseConn().getTable(tableName);
        ResultScanner rs = t.getScanner(scan);
        long startTime = System.currentTimeMillis();
        Iterator<Result> it = rs.iterator();
        while (it.hasNext()){
            Result r = it.next();
            System.out.println(r.getColumnLatestCell(Bytes.toBytes("f1"),Bytes.toBytes("name")));
        }
        System.out.println(System.currentTimeMillis()-startTime);
    }

    /**
     * 扫描器缓存(面向行级别的)
     *
     * cache row nums : 1000			//632
     * 	cache row nums : 5000			//423
     * 	cache row nums : 1				//7359
     * 	时间长短跟内存相关
     * @throws IOException
     *
     */
    @Test
    public void getScanBatch() throws IOException {

        TableName tableName = TableName.valueOf("ns1:t7");
        Scan scan = new Scan();
        scan.setCaching(2);
        scan.setBatch(4);
        Table t = hbaseConn().getTable(tableName);
        ResultScanner rs = t.getScanner(scan);
        long startTime = System.currentTimeMillis();
        Iterator<Result> it = rs.iterator();
        while (it.hasNext()){
            Result r = it.next();
            System.out.println("======================================================");
            //得到一行的所有map,key=f1,value=Map<Col,Map<Timestamp,value>>
            NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = r.getMap();
            for (Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> entry : map.entrySet()){
                String f = Bytes.toString(entry.getKey()); //得到列族
                Map<byte[], NavigableMap<Long, byte[]>> colDataMap = entry.getValue();
                for (Map.Entry<byte[], NavigableMap<Long, byte[]>> ets : colDataMap.entrySet()){
                    String col = Bytes.toString(ets.getKey());  //id||name||age
                    Map<Long,byte[]> tsValueMap = ets.getValue();
                    for (Map.Entry<Long,byte[]> e : tsValueMap.entrySet()){
                        Long timeStamp = e.getKey();  //时间戳//
                        String value = Bytes.toString(e.getValue()); //获取name对应的值
                        System.out.println(f+ ":"+ col+":"+value+", timeStamp:"+timeStamp+",   ");

                    }
                }
            }
        }

    }

}

 

www.htsjk.Com true http://www.htsjk.com/hbase/42100.html NewsArticle Hbase 缓存, package com.mao.hbase;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.hb...
相关文章
    暂无相关文章
评论暂时关闭