HBASE-JAVA客户端入门操作

java客户端对hbase进行建表、增删改数据

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

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

public class HbaseDemo {
    //代表了对数据库的管理类
    private HBaseAdmin admin = null;
    //代表了对表的数据进行操作的类
    private HTable table = null;
    private String tableName = "tb_user";

    @Before
    public void init() throws IOException {
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "192.168.1.201,192.168.1.202,192.168.1.203");
        //conf.set("hbase.zookeeper.property","2181");
        //conf.set("zookeeper.znode.parent","/hbase");
        admin = new HBaseAdmin(conf);
        // table = new HTable(conf,tableName);
//        table = new HTable(conf,tableName.getBytes());
        table = new HTable(conf, TableName.valueOf(tableName));
    }

    @After
    public void destroy() throws IOException {
        if (null != table) {
            table.close();
        }
        if (null != admin) {
            admin.close();
        }


    }

    @Test
    public void createTable() throws IOException {
        if (admin.tableExists(tableName)) {
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
        }
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
        HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("cf".getBytes());
        hTableDescriptor.addFamily(hColumnDescriptor);
        admin.createTable(hTableDescriptor);
    }

    @Test
    public void testInsert() throws InterruptedIOException, RetriesExhaustedWithDetailsException {
        // scan , put , get , delete\
        //构造器参数为行键
        Put put = new Put("1001".getBytes());
        put.add("cf".getBytes(), "name".getBytes(), "zhangsan".getBytes());
        put.add("cf".getBytes(), "age".getBytes(), Bytes.toBytes(26));
        put.add("cf".getBytes(), "address".getBytes(), "shanghai".getBytes());
        table.put(put);

        put = new Put("1002".getBytes());
        put.add("cf".getBytes(), "name".getBytes(), "lisi".getBytes());
        put.add("cf".getBytes(), "age".getBytes(), Bytes.toBytes(25));
        put.add("cf".getBytes(), "likes".getBytes(), "reading".getBytes());
        table.put(put);
    }

    @Test
    public void testScan() throws IOException {
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);

        while (scanner.next() != null) {
            printMsg(scanner.next());
        }
        scanner.close();

    }

    @Test
    public void testGet() throws IOException {
        Get get = new Get("1001".getBytes());
        Result result = table.get(get);

        printMsg(result);

    }

    public void printMsg(Result result) {
        Cell namecell = result.getColumnLatestCell("cf".getBytes(), "name".getBytes());
        Cell agecell = result.getColumnLatestCell("cf".getBytes(), "age".getBytes());
        Cell addcell = result.getColumnLatestCell("cf".getBytes(), "address".getBytes());
        Cell likescell = result.getColumnLatestCell("cf".getBytes(), "likes".getBytes());

        System.out.println(Bytes.toString(CellUtil.cloneValue(namecell)));
        System.out.println(Bytes.toString(likescell == null ? new byte[]{} : CellUtil.cloneValue(likescell)));
        System.out.println(Bytes.toString(addcell == null ? new byte[]{} : CellUtil.cloneValue(addcell)));
        System.out.println(Bytes.toInt(CellUtil.cloneValue(agecell)));
    }

    @Test
    public void testAddColumnFamily() throws IOException {
        HColumnDescriptor columnDescriptor = new HColumnDescriptor("cf2".getBytes());
        columnDescriptor.setMaxVersions(3);//指定最大保留几个版本
        //给指定表添加列族
        admin.addColumn(tableName,columnDescriptor);

    }

    @Test
    public void testDelete() throws IOException {
        Delete delete = new Delete("1002".getBytes());
        table.delete(delete);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值