HBase学习—增删改查API

实际工作中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();
    }
}
   
  1. 连接数据库
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;
}	
  1. 建库建表(三要素:表名、表类型、列族)

    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);
	}
}	
  1. 插入数据(数据都以二进制插入)

    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);
}
  1. 删除数据(方法类似插入数据)

    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);
}
  1. 删除表(删表前先禁用)

    HBase命令:drop ‘表名’

  public void droptable() throws IOException {
  	admin.disableTable(tn);
    admin.deleteTable(tn);
  }
  1. 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)));
	}
}	
  1. 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())));
	  }
}
  1. 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();
}

没有伞的孩子,只有努力奔跑!

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值