28-hadoop-hbase入门小程序

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

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

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

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

    /**
     * 获取链接, 只需要zookeeper的链接就可了
     */
    @Before
    public 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);
    }

    /**
     * 关闭资源
     */
    @After
    public void afterTest() throws IOException {
        if (connect != null) {
            connect.close();
        }
    }

4, 新建表

    @Test
    public 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, 插入模拟数据

    /**
     * 数据插入
     */
    @Test
    public 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());
            // 列1
            put.addColumn("cf1".getBytes(), "address".getBytes(), "bj".getBytes());
            // 列2
            put.addColumn("cf1".getBytes(), "type".getBytes(), String.valueOf(random.nextInt(2)).getBytes());
            list.add(put);
        }
        table.put(list);
    }

生成模拟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
     */
    @Test
    public 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

    @Test
    public void filter() throws IOException {
        // All表示全部匹配, 相当于and, one 相当于 or
        FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);

        // 查询某一个电话, type 等于 1
        PrefixFilter 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查询

    @Test
    public 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"));
    }

系列来自尚学堂视频

时间: 2024-08-03 14:18:56

28-hadoop-hbase入门小程序的相关文章

WebService概述和CXF入门小程序

一. 什么是WedService? WebService不是框架, 甚至不是一种技术, 而是一种跨平台,跨语言的规范, WebService的出现是为了解决这样的需求场景: 不同平台, 不同语言所编写的应用之间相互调用. 二. WedService有什么用? WebService可以集中解决以下问题: 1. 远程调用 2. 跨平台调用 3. 跨系统调用 那么WebService在企业中有什么用呢? 1. 同一个公司新旧系统的整合. 2. 不同公司的业务整合: 业务的整合就要带来不同公司的系统整合

Spring IoC、DI入门小程序

Alt+/智能提示xml配置文件节点及属性:在接口上使用Ctrl+T可以提示其实现类 一.IoC控制反转(将创建对象的权利交给spring)入门小程序 1.引入jar包 2.工程基本结构 3.新建UserService类 package hjp.spring.demo1; import org.junit.Test; public class UserService { public void addUser() { System.out.println("addUser"); } }

1小时实战入门小程序开发,历史上的今天案例讲解

我们前面学了这么多的小程序基础知识,一直没有用一个实际的案例来把前面的知识点串起来,今天我们就来开发一款简单的<历史上的今天>,来把我们前面的知识点完整的串起来. 老规矩,先看效果图 可以看到我们实现了如下功能 1,列表页 2,列表跳转详情页 3,视频播放(其实是假的,后面给大家讲这个视频播放) 4,网络请求 5,列表到详情数据携带好了,话不多说,我们来直接看代码实现. 一,网络数据的获取 网络数据获取我们用来官方提供的wx.request方法.下面红色框里就是我们的网络数据获取的代码是不是感

C++入门小程序练习

初识C++,向大家介绍几个我用来做练习编写的几道小程序,都是C++Primer上面的练习题,分享给大家. eg1:编写程序,使用递减运算符在循环中按递减顺序打印出10到1之间的整数. 代码如下: #include<iostream> #include<cstdlib> using namespace std; int main() { int i=10; while(i) { cout<<i; --i; } system("pause"); retu

C++ 入门小程序

1.控制台输出 hello world #include "stdafx.h" #include<iostream> using namespace std; int main() { cout << "Hello world ! "; system("pause"); return 0; } 2.交互小程序 加法计算 using namespace std; int main() { int a, b; cout <

Python入门小程序1

学习了FishC的Python零基础入门第4节,本次的内容是Python的while循环语句和条件语句. 1. 用一个条件语句实现猜数字的小程序 程序设定一个数字,用户输入一个数字,判断是否猜对. temp=input("猜猜我心中的数字:") guess=int(temp) if guess==8: print("猜对!") else: print("猜错了!") print("游戏结束!") 2. 改进程序1 上一个程序中

Python入门小程序(一)

学习了FishC的Python零基础入门第4节,本次的内容是Python的while循环语句和条件语句. 1. 用一个条件语句实现猜数字的小程序 程序设定一个数字,用户输入一个数字,判断是否猜对. temp=input("猜猜我心中的数字:") guess=int(temp) if guess==8: print("猜对!") else: print("猜错了!") print("游戏结束!") ###运行结果: 2. 改进程

springMVC学习笔记(二)-----注解和非注解入门小程序

最近一直在做一个电商的项目,周末加班,忙的都没有时间更新博客了.终于在上周五上线了,可以轻松几天了.闲话不扯淡了,继续谈谈springMvc的学习. 现在,用到SpringMvc的大部分使用全注解配置,但全注解配置也是由非注解发张而来的.所以,今天就谈谈springMvc最基础的注解和非注解的配置以及开发模式. 一:基础环境准备 1.功能需求:一个简单的商品列表查询 2.开发环境:eclipse,java1.7,springmvc版本:3.2 3.springMvc所需jar包(一定包括spri

三个入门小程序

package com.judge;//1.编写程序,输入一个数判断其是偶数还是奇数 import java.util.Scanner;// 先导入Scanner包于程序public class Numjudge{ 之前public class Numjudge { public static void main(String[] args){ Scanner sc=new Scanner(System.in); System.out.println("请输入一个数:"); int n

spring入门篇10 --- springMVC入门小程序

继续昨晚没有完成的这个小demo,完成这个,我们基本上算入门 首先,这个demo继续使用注解方式,源码地址github 首先看一下这个,这个就是maven起的项目结构,使用SpringMVC起项目,webapp就会与src同级,但是我们一般把src当作源码目录,所以做好可以做到同级,但这个就是为了练习. 首先明确一下,DispatcherServlet是实现servlet接口,Dispatcher使用Spring配置文件告知spring他需要请求反射,视图解析,异常处理等,而我们项目一般是跑在t