Hbase的命令行客户端操作、Java客户端操作、过滤器

Hbase的命令行客户端操作(shell操作)

  • 描述表:describe “t_user”
    • 判断表是否存在:exists “t_user”
    • 向表中添加数据:put “t_user”,”rk001”,”basic_info:name”,”huihui”
      put “t_user”,”rk001”,”family:father”,”bingbing”
    • 更新记录:与put相同,追加不是覆盖
    • 查看表中数据:get “t_user”,”rk001”
    • 计算总条数: count “t_user”
    • 获取某个列族:get “t_user”,”rk001”,”base_info”
    • 获取某个列族中的列: get “t_user”,”rk001”,”base_info”,”name”
    • 删除记录:delete “t_user”,”rk001”,”base_info”,”name”
    • 删除整行:delete “t_user”,”rk001”
    • 删除表:先将表设置为不可用,disable “t_user”
      再删除,drop “t_user”
    • 清空表:truncate “t_user”
    • 查看所有记录:scan “t_user”

Hbase的Java客户端操作(JavaAPI)

public class TableAdmin {
    Connection connection = null;
    Admin admin = null;
    Table table = null;
    @Before
    public void info() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
        //获取连接
        connection = ConnectionFactory.createConnection(conf);
        //从连接中获取表管理对象
        admin = connection.getAdmin();
    }
    /**
     * 
     * 建表
     */
    @Test
    public void TestCreate() throws Exception {

        //表描述
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("t_user"));
        //列簇描述
        HColumnDescriptor hColumnDescriptor1 = new HColumnDescriptor("base_info");
        hColumnDescriptor1.setMaxVersions(3);
        HColumnDescriptor hColumnDescriptor2 = new HColumnDescriptor("family");

        hTableDescriptor.addFamily(hColumnDescriptor1);
        hTableDescriptor.addFamily(hColumnDescriptor2);

        admin.createTable(hTableDescriptor);

    }
    /**
     * 删除表
     * @throws Exception
     */
    @Test
    public void TestDelete() throws Exception {

        admin.disableTable(TableName.valueOf("t_user"));
        admin.deleteTable(TableName.valueOf("t_user"));
    }
    /**
     * 向表中添加数据
     * @throws Exception
     */
    @Test
    public void TestPutData() throws Exception {
        table = connection.getTable(TableName.valueOf("t_user"));
        Put put = new Put(Bytes.toBytes("007rk"));
        put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes("huishu"));
        put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes("11"));

        Put put2 = new Put(Bytes.toBytes("008rk"));

        put2.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes("shu"));
        put2.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes("20"));

        List<Put> list = new ArrayList<>();
        list.add(put);
        list.add(put2);
        table.put(list);    
    }
    /**
     * 删除表中数据
     * @throws Exception 
     * @throws Exception
     */
    @Test
    public void TestDeleteData() throws Exception {
        table = connection.getTable(TableName.valueOf("t_user"));
        Delete delete = new Delete(Bytes.toBytes("rk002"));
        //多版本
        //delete.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
        table.delete(delete);
    }
    /**
     * 查询单行数据
     * @throws Exception 
     * @throws Exception
     */
    @Test
    public void TestSelect() throws Exception {
        table = connection.getTable(TableName.valueOf("t_user"));
        Get get = new Get(Bytes.toBytes("rk002"));
        Result result = table.get(get);
        byte[] value = result.getValue(Bytes.toBytes("family"), Bytes.toBytes("mather"));
        System.out.println(Bytes.toString(value));

        byte[] value2 = result.getValue(Bytes.toBytes("family"), Bytes.toBytes("father"));
        System.out.println(new String(value2));

    }
    /**
     * 多行查询
     * @throws Exception 
     * @throws Exception
     */
    @Test
    public void TestSelects() throws Exception {
        table = connection.getTable(TableName.valueOf("t_user"));
        Scan scan = new Scan();
        //只包含开始,不包含结束
        scan.setStartRow(Bytes.toBytes("rk003"));
        scan.setStopRow(Bytes.toBytes("rk005"));
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            System.out.print(new String(result.getRow()) + " ");
            byte[] value = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
            System.out.println(Bytes.toString(value));

            byte[] value2 = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
            System.out.println(new String(value2));
            System.out.println("-----------------------------");
        }
    }
    /**
     * 列值过滤器
     * @throws Exception 
     * @throws Exception
     */
    @Test
    public void SingleColumnValueFilter() throws Exception {
        table = connection.getTable(TableName.valueOf("t_user"));
        Scan scan = new Scan();
        SingleColumnValueFilter f = new SingleColumnValueFilter(Bytes.toBytes("base_info"), Bytes.toBytes("name"), CompareOp.EQUAL, Bytes.toBytes("shu"));
        scan.setFilter(f);
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            System.out.print(new String(result.getRow()) + " ");
            byte[] value = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
            System.out.println(Bytes.toString(value));

            byte[] value2 = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
            System.out.println(new String(value2));
            System.out.println("-----------------------------");
        }
    }
    /**
     * 列名前缀过滤器
     * @throws Exception 
     * @throws Exception
     */
    @Test
    public void ColumnPrefixFilter1() throws Exception {
        table = connection.getTable(TableName.valueOf("t_user"));
        Scan scan = new Scan();
        ColumnPrefixFilter f = new ColumnPrefixFilter(Bytes.toBytes("name"));
        scan.setFilter(f);
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            System.out.print(new String(result.getRow()) + " ");
            byte[] value = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
            System.out.println(Bytes.toString(value));

            System.out.println("-----------------------------");
        }
    }
    /**
     * rowKey过滤器
     * @throws Exception 
     * @throws Exception
     */
    @Test
    public void row() throws Exception {
        table = connection.getTable(TableName.valueOf("t_user"));
        Scan scan = new Scan();
        RowFilter f = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("^rk"));
        scan.setFilter(f);
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            System.out.print(new String(result.getRow()) + " ");
            byte[] value = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
            System.out.println(Bytes.toString(value));

            byte[] value2 = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
            System.out.println(new String(value2));
            System.out.println("-----------------------------");
        }
    }
    /**
     * FilterList过滤器列表
     * @throws Exception 
     * @throws Exception
     */
    @Test
    public void FilterByList() throws Exception {
        table = connection.getTable(TableName.valueOf("t_user"));
        Scan scan = new Scan();
        FilterList list = new FilterList(Operator.MUST_PASS_ALL);
        SingleColumnValueFilter f = new SingleColumnValueFilter(Bytes.toBytes("base_info"), Bytes.toBytes("name"), CompareOp.EQUAL, Bytes.toBytes("shu"));
        RowFilter f1 = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("^rk"));
        list.addFilter(f);
        list.addFilter(f1);
        scan.setFilter(list);
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            System.out.print(new String(result.getRow()) + " ");
            byte[] value = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
            System.out.println(Bytes.toString(value));

            byte[] value2 = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
            System.out.println(new String(value2));
            System.out.println("-----------------------------");
        }
    }
    @After
    public void close() throws Exception {
        table.close();
        admin.close();
        connection.close();
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值