Hbase学习笔记-HbaseJava

Hbase Java API:

secureCRTXstart远程桌面连接

maven项目pom文件maven-cdh仓库地址hbase&Hadoop依赖

createTable ->读取配置文件HbaseConfiguration Hbaseconfiguration.create();

create HbaseAdmin -> HbaseAdmin admin =

new HbaseAdmin(conf); // hbase中对于表的操作封装成为了一个类为HbaseAdmin

表是否存在admin.tableExists(Bytes.toBytes(“t1”));

admin.disableTable(Bytes.toBytes(“t1”)); //先禁用再删除

admin.deleteTable(Bytes.toBytes(“t1”));

HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(“t1”));

desc.addFamily(newHColumnDescriptor(Bytes.toBytes(“info”)));

admin.createTable(HTableDescriptor desc); //新建表的描述对象

//关闭资源

Admin.colse();

getTable() ->

创建配置文件

Configuration conf =HbaseConfiguration.create();

创建一个表

HTable table = newHtable(conf,Bytes.toBytes(“t1”));

//数据插入Put

Put put = new Put(RowKey :Bytes.toBytes(“rk00001”));

Put.add(Bytes.toBytes(“info”), Bytes.toBytes(“name”),Bytes.toBytes(“lisi”));

Table.put(put);

// close resource

Table.close();

getData ->

Get get = newGet(Bytes.toBytes(“rk00001”));

Result rs = table.get(get);

Result.getValue(Bytes.toBytes(“info”), Bytes.toBytes(“name”));// Bytes Array

For(Cell cel:result.rawCells()) {

Cell.getFamily() // deprecatedctrl+o弹出所有方法

CellUtil. cloneFamily(cell);

CellUtil. cloneQualifier(cell); // Bytes

CellUtil. cloneRow(cell);

CellUtil. cloneValue(cell);

}

命名空间//

HbaseAdmin对象instance

NamespaceDescriptor descriptor =NamespaceDescriptor.create(“ns1”).build();

Admin.createNamespace(descriptor);

Admin.close();

Hbase全表扫描注意异常处理

Htable -> close resourceIOUtils.closeStream(table);

getTable -> scan(create Scaneinterface)getScanner(ResultScanner)-> Result

printResult -> Cell rs.rawCells()Bytes.toString(CellUtil.cloneFamily(cell))

family row qualifier value()列族rowkey列名值

范围查询:

StartRow(startRowKey:Bytes.toBytes(“”))stopRow(stopRowKey:Bytes.toBytes(“”)包头不包

尾scan.addfamily(Bytes.toBytes(“columnFamily”))

缓存set cache

Scan.setCacheBlocks(false)默认值为true

本地缓存数据一般只使用一次

Batch列缓存setBatch(2)每次返回的列有两行

setcaching行缓存setCaching(2)10行数据读取5次默认不缓存

setACL(permission) // set permission

过滤器:

ColumnPrefixFilter前缀过滤器

ColumnFilter列族过滤器

PageFilter分页过滤器

ByteArrayComparable过滤器与比较器结合使用

// filter

Table = getTable();

Scan scan = new Scan();

Filter filter = newColumnPrefixFilter(Bytes.toBytes(“rk”));//查看实现接口的类ctrl+7eclipseprefixFilter过滤rowkey

Filter = new PageFilter(3); //三条记录分页指定查询的条数

Scan.setFilter(filter);

Table.getScanner(scan);

// create hbase comparator

ByteArrayComparable com = null;

com = new subStringComparator(“lisi”);

filter = new SingleColumnValueFilter(

family,// BytestoBytes(“info”)

qualifier, // BytestoBytes(“name”)

compareOp,// compareOp.EQUAL

value // com

)

    原文作者:时待吾
    原文地址: https://www.jianshu.com/p/f4379aa38001
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞