java类 | HBase数据模型 |
---|---|
HBaseAdmin HBaseConfiguration | 数据库 |
HTable | 表(Table) |
HTableDescriptor | 列表(CF) |
Put Get Scanner | 列表修饰符 |
1.HBaseAdmin
作用:提供了一个接口来管理HBase数据库表信息,它提供的方法包括:创建表,删除表,列出表,使表有效或无效以及添加或删除列祖或成员等
函数 | 描述 |
---|---|
addColumn | 向一个已经存在的表中添加列 |
checkHBaseAvailable | 静态函数,查看Hbase是否处于运行状态 |
createTable | 创建一个表,同步操作 |
deleteTable | 删除一个已经存在的表 |
enableTable | 使表处于有效状态 |
disableTable | 使表处于无效状态 |
listTables() | 列出所有用户控件表项 |
modifyTable | 修改表的模式,使异步的操作,可能需要花费一定的时间 |
tableExists | 检查表是否存在 |
2.HBaseConfiguration
作用:对HBase进行配置
3.HTableDescriptor
作用:包含了表的名字及其对应的表的列族
4.HColumDescriptor
作用:维护着关于列祖的信息,例如版本号,压缩设置等,通常在创建表或者为表添加列族的时候使用
package Hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class hbasedemo {
private Configuration conf;
private Connection conn;
private Admin hbAdmin;
public void init() throws Exception{
conf=new Configuration();
conf.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
// conf= HBaseConfiguration.create();
conn= ConnectionFactory.createConnection(conf);
hbAdmin=conn.getAdmin();//创建表首先要创建一个Admin对象,然后让它来创建一张表
}
/**
*
* 表是否已经存在
*
*/
public void testExists() throws IOException{
System.out.println(hbAdmin.tableExists(TableName.valueOf("mytest")));
}
/**
* hbase表的创建
*/
public void creat() throws IOException{
TableName tablename= TableName.valueOf("mytest");//定义表名
if (!hbAdmin.tableExists(tablename)){
HTableDescriptor tabledesc = new HTableDescriptor(tablename);//定义表对象
HColumnDescriptor familyDesc1 =new HColumnDescriptor(Bytes.toBytes("info1"));//定义列族对象
HColumnDescriptor familyDesc2 =new HColumnDescriptor(Bytes.toBytes("info2"));
familyDesc1.setMaxVersions(3);//设置数据保存的最大保存数
tabledesc.addFamily(familyDesc1);//添加
tabledesc.addFamily(familyDesc2);
hbAdmin.createTable(tabledesc);//创建表
} else {
}
}
/**
*
* 禁用表
*/
public void testdisable() throws IOException{
TableName tablename =TableName.valueOf("mytest");
hbAdmin.disableTable(tablename);
}
public void testIsDisable()throws IOException{
TableName tablename =TableName.valueOf("mytest");
System.out.println(hbAdmin.isTableDisabled(tablename));
}
public void testdrop()throws IOException{
TableName tablename = TableName.valueOf("mytest");
hbAdmin.deleteTable(tablename);
}
/**
*一次增加一个单元格
*/
public void testput()throws IOException {
TableName tablename = TableName.valueOf("mytest");
Table table = conn.getTable(tablename);
Put put = new Put(Bytes.toBytes("aa"));//定义行
put.addColumn(Bytes.toBytes("info1"), Bytes.toBytes("name"), Bytes.toBytes("zhangfei"));
table.put(put);
}
/**
* 一次增加多个单元格
*/
public void testputbatch()throws IOException {
TableName tablename = TableName.valueOf("mytest");
Table table = conn.getTable(tablename);
List<Put> list = new ArrayList<>();
Put put1 = new Put(Bytes.toBytes("aa"));
put1.addColumn(Bytes.toBytes("info1"), Bytes.toBytes("name"), Bytes.toBytes("guanYu"));
Put put2 = new Put(Bytes.toBytes("aa"));
put2.addColumn(Bytes.toBytes("info1"), Bytes.toBytes("age"), Bytes.toBytes("20"));
Put put3 = new Put(Bytes.toBytes("aa"));
put3.addColumn(Bytes.toBytes("info2"), Bytes.toBytes("name"), Bytes.toBytes("liuBei"));
Put put4 = new Put(Bytes.toBytes("aa"));
put4.addColumn(Bytes.toBytes("info1"), Bytes.toBytes("otherName"), Bytes.toBytes("guan2"));
list.add(put1);
list.add(put2);
list.add(put3);
list.add(put4);
table.put(list);
}
/**
* 删除一个单元格
*/
public void testDelete() throws IOException{
TableName tablename=TableName.valueOf("mytest");
Table table =conn.getTable(tablename);
Delete delete =new Delete(Bytes.toBytes("aa"));
delete.addColumn(Bytes.toBytes("info1"), Bytes.toBytes("otherName"));
table.delete(delete);
}
/**
* 查询一行数据(一个rowKey对应的数据),hbase shell中get操作
* @throws IOException
*/
public void testGet() throws IOException {
TableName tableName = TableName.valueOf("mytest");
Table table = conn.getTable(tableName);
Get get = new Get(Bytes.toBytes("aa"));//指定行键
// get.addColumn(Bytes.toBytes("info1"), Bytes.toBytes("name"));
get.addFamily(Bytes.toBytes("info1"));//指定列族
Result result = table.get(get);
for(Cell cell : result.rawCells()) {
System.out.print("行键:"+ Bytes.toString(CellUtil.cloneRow(cell))+"\t");
System.out.print("列族:"+ Bytes.toString(CellUtil.cloneFamily(cell))+"\t");
System.out.print("列标识:"+ Bytes.toString(CellUtil.cloneQualifier(cell))+"\t");
System.out.println("值:"+ Bytes.toString(CellUtil.cloneValue(cell)));
}
}
/**
*扫描表中的数据
*
*/
public void testscan() throws IOException{
TableName tableName = TableName.valueOf("mytest");
Table table = conn.getTable(tableName);
Scan scan =new Scan();//得到用于扫描region的对象
ResultScanner rs = table.getScanner(scan);//使用Htable得到resultscanner实现类的对象
this.showResult(rs);
}
private void showResult(ResultScanner rs) {
for(Result result : rs) {
Cell[] cells = result.rawCells();
for(Cell cell : cells) {
System.out.print("rowkey:" + Bytes.toString(CellUtil.cloneRow(cell))+"\t");
System.out.print("family:" + Bytes.toString(CellUtil.cloneFamily(cell))+"\t");
System.out.print("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell))+"\t");
System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
/*
* 行键过滤器扫描
* */
public void testfilterscan() throws IOException{
TableName tableName =TableName.valueOf("mytest");
Table table = conn.getTable(tableName);
Scan scan =new Scan();
//正则表达式匹配
Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator("^\\[a-z][0-9]$"));
scan.setFilter(filter);
this.showResult(table.getScanner(scan));
}
/*
*
* 列族过滤器*/
public void testFamilyFilterScan() throws IOException {
TableName tableName = TableName.valueOf("test001");
Table table = conn.getTable(tableName);
Scan scan = new Scan();
scan.addFamily(Bytes.toBytes("info1"));
Filter filter = new FamilyFilter(
CompareFilter.CompareOp.EQUAL,
new BinaryComparator(Bytes.toBytes("info2"))
);
scan.setFilter(filter);
this.showResult(table.getScanner(scan));
}
/*
*
* 列标识过滤器*/
public void testQualifierFilterScan() throws IOException {
TableName tableName = TableName.valueOf("test001");
Table table = conn.getTable(tableName);
Scan scan = new Scan();
Filter filter = new QualifierFilter(
CompareFilter.CompareOp.EQUAL,
new RegexStringComparator("^\\w*m\\w*$")
);
scan.setFilter(filter);
this.showResult(table.getScanner(scan));
}
/*
*
* 值过滤器 ,对所有列族下所有列的值进行过滤*/
public void testValueFilterScan() throws IOException {
TableName tableName = TableName.valueOf("test001");
Table table = conn.getTable(tableName);
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes("info1"));//指定开始键值
// Filter filter = new ValueFilter(
// CompareOp.EQUAL,
// new RegexStringComparator("^\\d+")
// );
Filter filter = new ValueFilter(
CompareFilter.CompareOp.EQUAL,
new SubstringComparator("100")
);
scan.setFilter(filter);
this.showResult(table.getScanner(scan));
}
public static void main(String[] args) throws Exception {
System.setProperty("hadoop.home.dir", "E:\\hadoop-2.6.5\\");
hbasedemo testa=new hbasedemo();
testa.init();//抛出异常 alt+enter
testa.creat();
testa.testputbatch();
// testa.testGet();
testa.testscan();
}
}