表的预切割,版本数,原生扫描,TTL,计数器
预先切割
---------------
创建表时,预先对表进行切割。
切割线是rowkey.
$hbase>create 'ns1:t2','f1',SPLITS=>['row3000','row6000']
创建表时指定列族的版本数,该列族的所有列都具有相同数量版本
-----------------------------------------------------------
$hbase>create 'ns1:t3',{NAME=>'f1',VERSIONS=>3} //创建表时,指定列族的版本数。
$hbase>get 'ns1:t3','row1',{COLUMN=>'f1',VERSIONS=>4} //检索的时候,查询多少版本。
@Test/** * @Description:按照指定版本数进行查询 * @param ${tags} * @return ${return_type} * @throws * @author 邹培贤 * @date 2018/7/21 11:04 */public void getWithVersions() throws IOException { Configuration conf = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(conf); TableName tableName = TableName.valueOf("ns1:t3"); Table table = conn.getTable(tableName); Get get = new Get(Bytes.toBytes("row1")); //设置要检查的版本数(setMaxVersions(可以设置要检查的版本数,不设置默认最大值2147483647)) get.setMaxVersions(); Result result = table.get(get); List<Cell> cells = result.getColumnCells(Bytes.toBytes("f1"), Bytes.toBytes("name")); for(Cell c:cells){ String f=Bytes.toString(c.getFamily()); String col=Bytes.toString(c.getQualifier()); long time=c.getTimestamp(); String val=Bytes.toString(c.getValue()); System.out.println(f + "/" + col + "/" + time + "=" + val); } }
原生扫描(专家拥有)
-------------
1.原生扫描
$hbase>scan 'ns1:t3',{COLUMN=>'f1',RAW=>true,VERSIONS=>10} //包含标记了delete的数据
2.删除数据
$hbase>delete 'ns1:t3','row1','f1:name',1532094430085(时间戳) //删除数据,标记为删除.
使用scan 'ns1:t3'扫面一下,发现没有数据,因为删除了最新事件的数据,所以小于该删除时间的数据都作废。
3.TTL
time to live ,存活时间。
影响所有的数据,包括没有删除的数据。
超过该时间,原生扫描也扫不到数据。
$hbase>create 'ns1:tx' , {NAME=>'f1',TTL=>10,VERSIONS=>3}
计数器
------------------------
有点像数据库的自增,没调用一次,加1,比如在统计网站点击量时候,不可能每次都get取值,然后加1再put。使用incr可以自行控制。
$hbase>incr 'ns1:t8','row1','f1:click',步长(步长如果省略,默认加1)
$hbase>get_counter 'ns1:t8','row1','f1:click'
@Test
public void testIncr() throws IOException {
Configuration conf = HBaseConfiguration.create();
Connection conn = ConnectionFactory.createConnection(conf);
TableName tname = TableName.valueOf("ns1:t8");
Table t = conn.getTable(tname);
Increment incr = new Increment(Bytes.toBytes("row1"));
incr.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("daily"),1);
incr.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("weekly"),10);
incr.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("monthly"),100);
t.increment(incr);
}