实际工作中HBase的shell命令用的不多,大多数都会连接到java进行处理,以下介绍了HBase的CRUD的API
首先导包 hbase-client、hbase-common、hbase-server
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-common -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>1.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-server -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.2.0</version>
</dependency>
构建一个类
public class tb{
private Connection conn;
private TableName tn;
private Table table;
private Admin admin;
public tb(){}
//有参构造,对属性赋值
public tb(String tn) throws IOException {
//参数为表名(可以带命名空间)
this.tn = TableName.valueOf(tn);
//调用连接方法赋值,避免每调用一次方法就重新连接一次
this.conn=connect();
this.table=conn.getTable(this.tn);
this.admin=conn.getAdmin();
}
}
- 连接数据库
private Connection connect() throws IOException {
//创建配置文件
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","192.168.56.100");
conf.set("hbase.zookeeper.property.clientPort","2181");
//可以不设置,默认就是60000端口
conf.set("hbase.master","192.168.56.100:60000");
//创建连接
Connection conn = ConnectionFactory.createConnection(conf);
return conn;
}
-
建库建表(三要素:表名、表类型、列族)
HBase命令:create ‘表名’,‘columnFamily’,…
public void create() throws IOException {
//创建表的类型
HTableDescriptor htd = new HTableDescriptor(tn);
//添加列族
htd.addFamily(new HColumnDescriptor("Info"));
htd.addFamily(new HCoulumnDesciptor("Score"));
//创建命名空间
try{
admin.getNamespaceDescriptor("mydemo");
}catch(NamespaceNotFoundException e){
NamespaceDescriptor ns = NamespaceDescriptor.create("mydemo").build();
admin.createNamespace(ns);
}
//表不存在就创建表
if(!admin.tableExists(tn)){
admin.createTable(htd);
}
}
-
插入数据(数据都以二进制插入)
HBase命令:put ‘表名’,‘columnFamily:column’,‘value’
public void add() throws IOException {
//创建put对象,并指定行键
Put put = new Put("rowkey-01".getBytes());
//增加值
put.addColumn("Info".getBytes(),"name".getBytes(),"zs".getBytes());
put.addColumn("Score".getBytes(),"Math".getBytes(),"80".getBytes());
table.put(put);
}
-
删除数据(方法类似插入数据)
HBase命令:delete ‘表名’,‘rowkey’,‘columnFamily:column’,‘timestamp’
public void delete() throws IOException {
Delete delete = new Delete("rowkey-001".getBytes());
delete.deleteColumn("Info".getBytes(),"name".getBytes());
table.delete(delete);
}
-
删除表(删表前先禁用)
HBase命令:drop ‘表名’
public void droptable() throws IOException {
admin.disableTable(tn);
admin.deleteTable(tn);
}
-
get查询
HBase命令:get ‘表名’,‘rowkey’,‘columnFamily:column’
public void getDate() throws IOException {
Get get = new Get("rowkey-001".getBytes());
get.addColumn("Info".getBytes(),"name".getBytes());
//查询的结果返回Result类
Result result = table.get(get);
Cell[] cells = result.rawCells();
for(Cell cell:cells){
System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
}
}
-
scan扫描
HBase命令:scan命令语法 scan ‘表名’,{COLUMN=>‘columnFamily:column’}
public void scanDate() throws IOException {
Scan scan = new Scan();
scan.addColumn("Info".getBytes(),"name".getBytes());
ResultScanner scanner = table.getScanner(scan);
for (Result rst:scanner){
System.out.println(new String(rst.getRow()));
System.out.println(new String(rst.getValue("Info".getBytes(),"name".getBytes())));
}
}
-
Filter过滤
HBase命令:scan ‘表名’,FILTER=>“ValueFilter(=,‘binary:18’)”
public void scanFilter() throws IOException {
Scan scan = new Scan();
ValueFilter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator("80".getBytes()));
//列过滤
//ColumnPrefixFilter filter1 = new ColumnPrefixFilter("age".getBytes());
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for(Result result:scanner){
System.out.println(new String(result.getRow()));
System.out.println(new String(result.getValue("Score".getBytes(),"Math".getBytes())));
}
}
测试
public static void main(String[] args) throws IOException {
tb tb = new tb("mydemo:student");
tb.create();
tb.add();
tb.getDate();
tb.scanDate();
tb.scanFilter();
tb.delete();
tb.droptable();
}
没有伞的孩子,只有努力奔跑!