HBase Java API

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>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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/hadoopApi/src/main/java/com/xc/xcspringboot/test/HBaseTest.java

 

posted @   草木物语  阅读(39)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-11-10 IDEA的Diagram (UML类关系图)
2019-11-10 springboot docker 部署
点击右上角即可分享
微信分享提示