28-hadoop-hbase入门小程序

 2023-09-05 阅读 448 评论 0

摘要:hbase的完全分布式建立起来了, 可以试下好使不 1, 导包, {HBASE_HOME}/lib 下所有的jar包, 导入 2, 使用junit测试, 会报错, 因为缺少一个jar 3, 获取链接, 只需要提供zookeeper的地址即可 /*** 获取链接, 只需要zookeeper的链接就可了*/@Beforepublic void beforeTest()

hbase的完全分布式建立起来了, 可以试下好使不

1, 导包, {HBASE_HOME}/lib 下所有的jar包, 导入

2, 使用junit测试, 会报错, 因为缺少一个jar

3, 获取链接, 只需要提供zookeeper的地址即可

    /*** 获取链接, 只需要zookeeper的链接就可了*/@Beforepublic void beforeTest() throws IOException {Configuration conf = HBaseConfiguration.create();// hbase端口默认2181, 可以使用hbase.zookeeper.property.clientPort设置conf.set("hbase.zookeeper.quorum", "192.168.208.106,192.168.208.107,192.168.208.108");// 去的数据库连接对象connect = ConnectionFactory.createConnection(conf);}/*** 关闭资源*/@Afterpublic void afterTest() throws IOException {if (connect != null) {connect.close();}}

 

4, 新建表

    @Testpublic void testCreate() throws IOException {//去的一个数据库元操作对象Admin admin = connect.getAdmin();if (admin.tableExists(table)) {// 删除需要先disable
            admin.disableTable(table);admin.deleteTable(table);}// 创建表描述对象HTableDescriptor htable = new HTableDescriptor(table);// 创建列族描述对象HColumnDescriptor hColumn = new HColumnDescriptor("cf1".getBytes());hColumn.setMaxVersions(5);hColumn.setBlockCacheEnabled(true);hColumn.setBlocksize(1800000);// 数据库中新建一个列族
        htable.addFamily(hColumn);// 新建数据库
        admin.createTable(htable);}

5, 插入模拟数据 

    /*** 数据插入*/@Testpublic void testInsert() throws IOException {Table table = connect.getTable(tableName);List<Put> list = new ArrayList<>();for (int i = 0; i < 1000; i++) {int month = i % 3;int day = i % 9;Put put = new Put(getRowKey("138", month, day).getBytes());// 列1put.addColumn("cf1".getBytes(), "address".getBytes(), "bj".getBytes());// 列2put.addColumn("cf1".getBytes(), "type".getBytes(), String.valueOf(random.nextInt(2)).getBytes());list.add(put);}table.put(list);
     table.flushCommits();}

生成模拟rowkey的方法为: 

    /*** rowkey 生成策略, 电话号 + 日期*/public String getRowKey(String prePhone, int month, int day) {return prePhone + random.nextInt(99999999) + "_2017" + month + day;}

6, 根据rowkey进行查询

因为, rowkey 默认按照字典进行排序

    /*** 根据rowkey 查询* @throws IOException*/@Testpublic void scan() throws IOException {Table table = connect.getTable(tableName);//rowkey 按照字典排序, 所以可以截取Scan scan = new Scan("13862288854_201700".getBytes(), "13899338829_201714".getBytes());ResultScanner scanner = table.getScanner(scan);for (Iterator<Result> iterator = scanner.iterator(); iterator.hasNext(); ) {Result next = iterator.next();byte[] value = next.getValue("cf1".getBytes(), "type".getBytes());System.out.println(new String(value, "utf8"));}}

7, filter条件过滤

更多可见: http://hbase.apache.org/book.html#client.filter 第67条, 82.8 条 

更多filter, 找了个比较详细的博客: http://blog.csdn.net/cnweike/article/details/42920547

    @Testpublic void filter() throws IOException {// All表示全部匹配, 相当于and, one 相当于 orFilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);// 查询某一个电话, type 等于 1PrefixFilter prefixFilter = new PrefixFilter("1388".getBytes());SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("cf1".getBytes(), "type".getBytes(), CompareFilter.CompareOp.EQUAL, "1".getBytes());// 过滤器是对结果排序, 所以顺序影响效率
        filterList.addFilter(prefixFilter);filterList.addFilter(singleColumnValueFilter);// 创建查询对象Scan scan = new Scan();scan.setFilter(filterList);Table table = connect.getTable(tableName);ResultScanner scanner = table.getScanner(scan);for (Result result : scanner) {byte[] value = result.getValue("cf1".getBytes(), "type".getBytes());System.out.println(new String(value, "utf8"));}}

8, 根据单个rowkey查询

    @Testpublic void testGet() throws IOException {Table table = connect.getTable(tableName);Get get = new Get("13899126419_201717".getBytes());Result result = table.get(get);Cell cell = result.getColumnLatestCell("cf1".getBytes(), "address".getBytes());System.out.println(new String(cell.getValueArray(), "utf8"));}

 

系列来自尚学堂视频

 

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/486.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息