【云计算】Hbase的命令行客户端操作、Java客户端操作、过滤器解析
小标 2018-12-24 来源 : 阅读 978 评论 0

摘要:本文主要向大家介绍了【云计算】Hbase的命令行客户端操作、Java客户端操作、过滤器解析,通过具体的内容向大家展现,希望对大家学习云计算有所帮助。

本文主要向大家介绍了【云计算】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 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 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved