Apache HBase是一个开源的、分布式的、非关系型的列式数据库。

HBase位于Hadoop生态系统的结构化存储层,数据存储于分布式文件系统HDFS并且使用ZooKeeper作为协调服务。HDFS为HBase提供了高可靠性的底层存储支持,MapReduce为HBase提供了高性能的计算能力,ZooKeeper则为HBase提供了稳定的服务和失效恢复机制。

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-server</artifactId>
    <version>1.3.3</version>
</dependency>

 

package com.xc.xcspringboot.test;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class HBaseTest {

    static Connection conn;

    static {
        //创建HBase配置对象
        Configuration conf = HBaseConfiguration.create();
        //加上这一句,就不需要将代码发布到服务器中执行了,直接eclipse中运行就可以。不加这一句,需要将代码导出jar,上传到HBase服务器执行。
        conf.set("hbase.zookeeper.quorum", "172.19.25.168:2181,172.19.25.169:2181,172.19.25.170:2181");
        //创建连接对象Connection
        try {
            conn = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
//        createTable();
//        put();
//        get();
//        delete();
//        filterTest();
        multiFilterTest();
    }


    static void createTable() throws IOException {
        //得到数据库管理员对象
        Admin admin = conn.getAdmin();
        //创建表描述,并指定表名
        TableName tableName = TableName.valueOf("t1");
        HTableDescriptor desc = new HTableDescriptor(tableName);
        //创建列族描述
        HColumnDescriptor family = new HColumnDescriptor("f1");
        //指定列族
        desc.addFamily(family);
        //创建表
        admin.createTable(desc);
        System.out.println("create table success!!");
    }

    static void put() throws IOException {
        //Table负责与记录相关的操作,如增删改查等
        TableName tableName = TableName.valueOf("t1");
        Table table = conn.getTable(tableName);

        Put put = new Put(Bytes.toBytes("row7"));// 设置rowkey
        put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"), Bytes.toBytes("xiaoming24"));
        put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("age"), Bytes.toBytes("33"));
        put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("address"), Bytes.toBytes("beijing"));

        Put put2 = new Put(Bytes.toBytes("row8"));// 设置rowkey
        put2.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"), Bytes.toBytes("xiaoming23333"));
        put2.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("age"), Bytes.toBytes("30"));
        put2.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("address"), Bytes.toBytes("beijing2"));

        Put put3 = new Put(Bytes.toBytes("row9"));// 设置rowkey
        put3.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("age"), Bytes.toBytes("31"));
        put3.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("address"), Bytes.toBytes("beijing3"));

        //执行添加数据
        table.put(put);
        table.put(put2);
        table.put(put3);
        //释放资源
        table.close();
        System.out.println("put data success!!");
    }

    static void get() throws IOException {
        //获取Table对象,指定查询表名,Table负责与记录相关的操作,如增删改查等
        Table table = conn.getTable(TableName.valueOf("t1"));
        //创建Get对象,根据rowkey查询,rowkey=row1
        Get get = new Get("row7".getBytes());
        //查询数据,取得结果集
        Result r = table.get(get);
        //循环输出每个单元格的数据
        for (Cell cell : r.rawCells()) {
            //取得当前单元格所属的列族名称
            String family = new String(CellUtil.cloneFamily(cell));
            //取得当前单元格所属的列名称
            String qualifier = new String(CellUtil.cloneQualifier(cell));
            //取得当前单元格的列值
            String value = new String(CellUtil.cloneValue(cell));
            //输出结果
            System.out.println("列:" + family + ":" + qualifier + "—————值:" + value);
        }
    }

    static void delete() throws IOException {
        //获取Table对象,指定表名,Table负责与记录相关的操作,如增删改查等
        TableName tableName = TableName.valueOf("t1");
        Table table = conn.getTable(tableName);
        //创建删除对象Delete,根据rowkey删除一整条
        Delete delete = new Delete(Bytes.toBytes("row1"));
        table.delete(delete);
        //释放资源
        table.close();
        System.out.println("delete data success!!");
    }

    static void filterTest() throws IOException {
        Table table = conn.getTable(TableName.valueOf("t1"));
        Scan scan = new Scan();
//		RegexStringComparator comp = new RegexStringComparator("you."); // 以 you 开头的字符串
//		SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("family"), Bytes.toBytes("qualifier"), CompareOp.EQUAL, comp);
        //1. 行键过滤器:筛选出行键为row1的一行数据
        Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("row2")));
        //2. 列族过滤器:筛选出列族为f1的所有数据
//        Filter filter = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("f1")));
        //3. 列过滤器:筛选出列为name的所有数据
//        Filter filter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("name")));
        //4. 值过滤器:筛选出一行中的值包含"xiaoming"的所有单元格数据
//        Filter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("beijing"));
        //5. 单列值过滤器:用一列的值决定该行是否被过滤
        /*//筛选出name列不包含xiaoming的所有行数据
        SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("f1"), Bytes.toBytes("name"), CompareFilter.CompareOp.NOT_EQUAL, new SubstringComparator("xiaoming"));
        //如果某行列name不存在,那么该行将被过滤掉,false则不进行过滤,默认为false。
        filter.setFilterIfMissing(true);*/

        //6. 分页过滤器,未完善
        /*Filter filter = new PageFilter(2);
        scan.setStartRow(Bytes.toBytes("row2"));*/
        scan.setFilter(filter);
        ResultScanner rs = table.getScanner(scan);
        for (Result res : rs) {
            System.out.println(res);
        }
        rs.close();
    }

    static void multiFilterTest() throws IOException {
        // 指定要查询的表t1
        Table table = conn.getTable(TableName.valueOf("t1"));
        Scan scan = new Scan();
        // 创建过滤器1,查询年龄小于等于30岁的所有数据
        SingleColumnValueFilter filter1 = new SingleColumnValueFilter(Bytes.toBytes("f1"), Bytes.toBytes("age"), CompareFilter.CompareOp.LESS_OR_EQUAL, Bytes.toBytes("30"));
        filter1.setFilterIfMissing(true);
        // 创建过滤器2,查询年龄大于等于18岁的所有数据
        SingleColumnValueFilter filter2 = new SingleColumnValueFilter(Bytes.toBytes("f1"), Bytes.toBytes("age"), CompareFilter.CompareOp.GREATER_OR_EQUAL, Bytes.toBytes("18"));
        filter2.setFilterIfMissing(true);
        // 创建过滤器集合对象
        FilterList filterList = new FilterList();
        // 添加过滤器1
        filterList.addFilter(filter1);
        // 添加过滤器2
        filterList.addFilter(filter2);
        // 设置过滤器
        scan.setFilter(filterList);
        // 执行查询,得到结果集
        ResultScanner rs = table.getScanner(scan);
        // 输出结果,每个res代表一行数据
        for (Result res : rs) {
            System.out.println(res);
        }
        rs.close();
    }

}

  

书籍: Hadoop大数据技术开发实战 8.7 HBase Java API操作
https://gitee.com/caoyeoo0/xc-springboot/blob/HBaseAPI/src/main/java/com/xc/xcspringboot/test/HBaseTest.java

 

原文地址:http://www.cnblogs.com/ooo0/p/16877629.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性