hbase 常用的api

1.创建表

public class createTable {

    @Test
   public void createTableUntil() throws IOException {

        //配置连接hbase的参数
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.property.clientPort","2181");
        configuration.set("hbase.zookeeper.quorum","node01,node02,node03");
        configuration.set("hbase.master","node01:60000");

        Connection connection = ConnectionFactory.createConnection(configuration);
        Admin admin = connection.getAdmin();
        //编辑你要创建表的名称
        HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf("user10"));

        //添加你需要的列族
        descriptor.addFamily(new HColumnDescriptor("info1"));
        descriptor.addFamily(new HColumnDescriptor("info2"));


        //判断你的表 是否已经创建了
        if (admin.tableExists(TableName.valueOf("user10"))){
            System.out.println("该表已经存在了,请修改该表的名称!!!");

        }else {
            admin.createTable(descriptor);
        }


        //关闭连接
        admin.close();

    }
}

2.表中添加数据

public class addDatas {
    @Test
    public void addDataUntil() throws IOException {

        //配置连接hbase的参数
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.property.clientPort","2181");
        configuration.set("hbase.zookeeper.quorum","node01,node02,node03");
        configuration.set("hbase.master","node01:60000");

        Connection connection = ConnectionFactory.createConnection(configuration);

        //指明要添加数据的表
        Table table = connection.getTable(TableName.valueOf("user10"));

         //插入数据
        Put put = new Put("0002".getBytes());
        put.addColumn("info1".getBytes(),"id".getBytes(), Bytes.toBytes(2));
        put.addColumn("info1".getBytes(),"name".getBytes(), Bytes.toBytes("lisi wangwu"));
        put.addColumn("info1".getBytes(),"age".getBytes(), Bytes.toBytes(20));
        put.addColumn("info2".getBytes(),"address".getBytes(), Bytes.toBytes("binhu"));
        put.addColumn("info2".getBytes(),"area".getBytes(), Bytes.toBytes("wuxi"));
        table.put(put);

        //关闭连接
        table.close();

    }
}

3.扫描全部的数据

public class scanAllData {
    @Test
    public void scanAllDataUntil() throws IOException {

        //设置hbase连接
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.property.clientPort","2181");
        configuration.set("hbase.zookeeper.quorum","node01,node02,node03");
        configuration.set("hbase.master","node01:60000");

        Connection connection = ConnectionFactory.createConnection(configuration);

        //指定查询的表名称
        Table table = connection.getTable(TableName.valueOf("user10"));

        Scan scan = new Scan();
        ResultScanner results = table.getScanner(scan);
        for (Result result : results) {

            System.out.println(Bytes.toString(result.getRow()));

            //指定列族以及列打印列当中的数据出来
            System.out.println(Bytes.toInt(result.getValue("info1".getBytes(), "id".getBytes())));
            System.out.println(Bytes.toInt(result.getValue("info1".getBytes(), "age".getBytes())));
            System.out.println(Bytes.toString(result.getValue("info1".getBytes(), "name".getBytes())));
        }
        table.close();

    }
}

4.根据rowkey查询

public class ScanRowKey {
    @Test
    public void  ScanRowKey() throws IOException {

        //设置hbase连接
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.property.clientPort","2181");
        configuration.set("hbase.zookeeper.quorum","node01,node02,node03");
        configuration.set("hbase.master","node01:60000");

        Connection connection = ConnectionFactory.createConnection(configuration);

        //指定查询的表名称
        Table table = connection.getTable(TableName.valueOf("user10"));

        Scan scan = new Scan();
        scan.setStartRow("0001".getBytes());
        scan.setStopRow("0002".getBytes());

        ResultScanner results = table.getScanner(scan);
        for (Result result : results) {
            System.out.println(Bytes.toString(result.getRow()));

            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                System.out.println(Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength()));
                System.out.print(Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()));

            }

        }

        table.close();
    }

5.查询数据

public class searchData {
 @Test
    public void searchData() throws IOException {

        //设置hbase连接
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.property.clientPort","2181");
        configuration.set("hbase.zookeeper.quorum","node01,node02,node03");
        configuration.set("hbase.master","node01:60000");

        Connection connection = ConnectionFactory.createConnection(configuration);

        //指定查询的表名称
        Table table = connection.getTable(TableName.valueOf("user10"));


        Get get = new Get(Bytes.toBytes("0001"));

        Result result = table.get(get);
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            System.out.println(Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()));
            System.out.println(Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()));
        }

        table.close();


    }
}

6.插叙数据通过rowkey

public class searchDataByRowKey {
    @Test
    public void searchDataByRowKeyUntil() throws IOException {

        //设置hbase连接
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.property.clientPort","2181");
        configuration.set("hbase.zookeeper.quorum","node01,node02,node03");
        configuration.set("hbase.master","node01:60000");

        Connection connection = ConnectionFactory.createConnection(configuration);

        //指定查询的表名称
        Table table = connection.getTable(TableName.valueOf("user10"));


        Get get = new Get("0001".getBytes());

        get.addColumn("info2".getBytes(),"address".getBytes());
        get.addColumn("info2".getBytes(),"area".getBytes());
        Result result = table.get(get);

        System.out.println(Bytes.toString(result.getValue("info2".getBytes(), "address".getBytes())));
        System.out.println(Bytes.toString(result.getValue("info2".getBytes(), "area".getBytes())));

        table.close();
        
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值