大数据与人工智能入门到放弃(10 回顾篇 HBase java api操作)

记:

最近在公司写单元测试,因为要涉及到周任务达标问题,所以天天加班,今天周末,安排一波Hbase完全分布式的搭建安装

HBase java api操作

在很多情况下,操作hbase不是在命令行直接操作的,所以有了很多api,这里我用java api进行操作。所以打开eclipse。

1.解压hbase,然后把lib下的jar包拷贝到eclipse中;

2.选中那些jar包,bulid path一波

3.直接上代码嘿

package com.junxi.hbase;

import java.io.IOException;
import java.io.InterruptedIOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class HBaseDemo {

	HBaseAdmin admin = null;
	String tb = "person";
	HTable table = null;
	
	@Before
	public void init() throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
		Configuration conf = new Configuration();
		conf.set("hbase.zookeeper.quorum", "master,node1,node2");
		admin = new HBaseAdmin(conf);
		table = new HTable(conf, tb.getBytes());
	}
	
	@Test
	public void createTable() throws IOException {
		HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tb));
		HColumnDescriptor family = new HColumnDescriptor("cf".getBytes());
	
		desc.addFamily(family);
		
		if(admin.tableExists(tb)) {
			admin.disableTable(tb);
			admin.deleteTable(tb);
		}
		
		admin.createTable(desc);
	}
	
	@Test
	public void insert() throws RetriesExhaustedWithDetailsException, InterruptedIOException {
		Put put = new Put("111".getBytes());
		put.add("cf".getBytes(), "name".getBytes(), "zhangsan".getBytes());
		put.add("cf".getBytes(), "age".getBytes(), "11".getBytes());
		put.add("cf".getBytes(), "sex".getBytes(), "1".getBytes());
		table.put(put);
	}
	
	@Test
	public void get() throws IOException {
		
		Get get = new Get("111".getBytes());
		Result result = table.get(get);
		Cell cell1 = result.getColumnLatestCell("cf".getBytes(), "name".getBytes());
		Cell cell2 = result.getColumnLatestCell("cf".getBytes(), "age".getBytes());
		Cell cell3 = result.getColumnLatestCell("cf".getBytes(), "sex".getBytes());
		System.out.println(Bytes.toString(CellUtil.cloneValue(cell1)));
		System.out.println(Bytes.toString(CellUtil.cloneValue(cell2)));
		System.out.println(Bytes.toString(CellUtil.cloneValue(cell3)));
	}
	
	@Test
	public void scan() throws IOException {
		Scan scan = new Scan();
		ResultScanner res = table.getScanner(scan);
		for (Result result : res) {
			Cell cell1 = result.getColumnLatestCell("cf".getBytes(), "name".getBytes());
			Cell cell2 = result.getColumnLatestCell("cf".getBytes(), "age".getBytes());
			Cell cell3 = result.getColumnLatestCell("cf".getBytes(), "sex".getBytes());
			System.out.println(Bytes.toString(CellUtil.cloneValue(cell1)));
			System.out.println(Bytes.toString(CellUtil.cloneValue(cell2)));
			System.out.println(Bytes.toString(CellUtil.cloneValue(cell3)));
		}
	}
	
	@After
	public void destory() throws IOException {
		if(admin != null) {
			admin.close();
		}
	}
	
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值