HBase常用JavaAPI操作

前置条件

因为使用JavaAPI要和HBase的服务端建立连接,如果每测试一次就要创建一个连接,就会显得比较麻烦,所以使用junit单元测试,来减少代码的冗余。

private Configuration configuration;
private Connection connection;

/**
 *获取连接
 */
@Before
public void getConfigurationAndTable() throws IOException {
    configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
    configuration.set("hbase.master", "node01:60000");
    connection = ConnectionFactory.createConnection(configuration);
}

@After
public void closeResource() throws IOException {
    connection.close();
}

创建表格

	@Test
    public void createTable() throws IOException {
        //获取管理员对象
        Admin admin = connection.getAdmin();
        //通过HTableDescriptor来实现我们表的参数设置,包括表名,列族等等
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("myuser"));
        //添加列族
        hTableDescriptor.addFamily(new HColumnDescriptor("f1"));
        hTableDescriptor.addFamily(new HColumnDescriptor("f2"));

        //判断是否存在该表
        boolean flag = admin.tableExists(TableName.valueOf("myuser"));
        //如果不存在,则创建该表
        if(!flag)
            admin.createTable(hTableDescriptor);

        admin.close();
    }

向表中添加数据

    @Test
    public void addData() throws IOException {
        //获取表格
        Table myuser = connection.getTable(TableName.valueOf("myuser"));
        //创建Put对象并指定rowkwy
        Put put = new Put("0001".getBytes());
        //给put对象所对应的rowkey指定列族、列名和值
        put.addColumn("f1".getBytes(),"id".getBytes(), Bytes.toBytes(1));
        put.addColumn("f1".getBytes(),"name".getBytes(),"zhangsan".getBytes());
        put.addColumn("f1".getBytes(),"age".getBytes(), Bytes.toBytes(18));
        put.addColumn("f2".getBytes(),"address".getBytes(), Bytes.toBytes("地球人"));
        put.addColumn("f2".getBytes(),"phone".getBytes(), Bytes.toBytes("15874102589"));
        //向表中插入数据
        myuser.put(put);
        //关闭资源
        myuser.close();
    }

查询数据

   /**
     * 初始化一批数据用于查询
     */
    @Test
    public void insertBatchData() throws IOException {
        //获取连接
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        //获取表
        Table myuser = connection.getTable(TableName.valueOf("myuser"));
        //创建put对象,并指定rowkey
        Put put = new Put("0002".getBytes());
        put.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(1));
        put.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("曹操"));
        put.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(30));
        put.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("沛国谯县"));
        put.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("16888888888"));
        put.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("helloworld"));

        Put put2 = new Put("0003".getBytes());
        put2.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(2));
        put2.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("刘备"));
        put2.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(32));
        put2.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put2.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("幽州涿郡涿县"));
        put2.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("17888888888"));
        put2.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("talk is cheap , show me the code"));


        Put put3 = new Put("0004".getBytes());
        put3.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(3));
        put3.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("孙权"));
        put3.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(35));
        put3.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put3.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("下邳"));
        put3.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("12888888888"));
        put3.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("what are you 弄啥嘞!"));

        Put put4 = new Put("0005".getBytes());
        put4.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(4));
        put4.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("诸葛亮"));
        put4.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(28));
        put4.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put4.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("四川隆中"));
        put4.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("14888888888"));
        put4.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("出师表你背了嘛"));

        Put put5 = new Put("0005".getBytes());
        put5.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(5));
        put5.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("司马懿"));
        put5.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(27));
        put5.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put5.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("哪里人有待考究"));
        put5.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15888888888"));
        put5.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("跟诸葛亮死掐"));


        Put put6 = new Put("0006".getBytes());
        put6.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(5));
        put6.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("xiaobubu—吕布"));
        put6.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(28));
        put6.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put6.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("内蒙人"));
        put6.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15788888888"));
        put6.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("貂蝉去哪了"));

        List<Put> listPut = new ArrayList<Put>();
        listPut.add(put);
        listPut.add(put2);
        listPut.add(put3);
        listPut.add(put4);
        listPut.add(put5);
        listPut.add(put6);

        myuser.put(listPut);
        myuser.close();
    }


    /**
     * 查询数据
     */
    @Test
    public void queryData() throws IOException {
        //获取表格
        Table myuser = connection.getTable(TableName.valueOf("myuser"));
        //创建get对象并指定rowkey
        Get get = new Get("0003".getBytes());
        //获取该行键对应的所有cell
        Result result = myuser.get(get);
        Cell[] cells = result.rawCells();
        //遍历获取到的所有cell
        for (Cell cell : cells) {
            //这里需要考虑类型为整形的列
            if(Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()).equals("age")
                    || Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()).equals("id")){
                System.out.print(Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()));
                System.out.print("\t");
                System.out.println(Bytes.toInt(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()));
            }else{
                System.out.print(Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()));
                System.out.print("\t");
                System.out.println(Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()));
            }
        }
        myuser.close();
    }

使用过滤器查询

	/**
     *比较过滤器:rowkey过滤器RowFliter
     *除RowFilter外还有	FamilyFilter	QualifierFilter	   ValueFilter用法都是类似的
     */
    @Test
    public void rowkwyFilter() throws IOException {
        //获取表格
        Table myuser = connection.getTable(TableName.valueOf("myuser"));
        //获取scan对象
        Scan scan = new Scan();
        //获取RowFilter对象:获取行键小于等于0003的数据,HBase会根据字典顺序对行键进行排序
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
                new BinaryComparator("0003".getBytes()));
        scan.setFilter(rowFilter);
        ResultScanner resultScanner = myuser.getScanner(scan);
        //遍历查询出来的结果
        for (Result result : resultScanner) {
            //获取rowkey
            System.out.print(Bytes.toString(result.getRow()));
            System.out.print("\t");
            //指定列族及列并打印该列的所有数据
            System.out.print(Bytes.toInt(result.getValue("f1".getBytes(), "id".getBytes())));
            System.out.print("\t");
            System.out.print(Bytes.toInt(result.getValue("f1".getBytes(), "age".getBytes())));
            System.out.print("\t");
            System.out.println(Bytes.toString(result.getValue("f1".getBytes(), "name".getBytes())));

        }
        myuser.close();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值