使用java API操作hbase增删改查的几个操作示例

使用java代码来实现Hbase数据库对数据的增删改查操作,总结示例如下:

首先创建一个Maven工程,需要导入的包如下:

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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>cn.wjqixige</groupId>
<artifactId>hbase</artifactId>
<version>1.0-SNAPSHOT</version>


<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-server -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.2.1</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<!-- <verbal>true</verbal>-->
</configuration>
</plugin>
</plugins>
</build>
</project>

创建表

创建一张myuser表,带有两个列族f1,f2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public void createTable() throws IOException {
//连接hbase集群
Configuration configuration = HBaseConfiguration.create();
//指定hbase的zk连接地址
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//获取管理员对象
Admin admin = connection.getAdmin();
//通过管理员对象创建表
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("myuser"));
//给表添加列族
HColumnDescriptor f1 = new HColumnDescriptor("f1");
HColumnDescriptor f2 = new HColumnDescriptor("f2");
//将两个列族设置到 创建的表中
hTableDescriptor.addFamily(f1);
hTableDescriptor.addFamily(f2);
//创建表
admin.createTable(hTableDescriptor);
//关闭连接
admin.close();
connection.close();
}

插入数据

向创建的myuser表中插入数据;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void addData() throws IOException {
//获取连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//获取表对象
Table myuser = connection.getTable(TableName.valueOf("myuser"));
Put put = new Put("0001".getBytes());
put.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(1));
put.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("小李子"));
put.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(18));
put.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("火星人"));
put.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("13033607199"));
myuser.put(put);
//关闭表
myuser.close();
}

查询数据

为了便于下面查询操作,这里插入一批初始数据。

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
public void insertBatchData() throws IOException {
//获取连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//获取表
Table myuser = connection.getTable(TableName.valueOf("myuser"));
//创建put对象,并指定rowkey
Put put = new Put("0002".getBytes());
put.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(1));
put.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("曹操"));
put.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(30));
put.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("沛国谯县"));
put.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("16888888888"));
put.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("helloworld"));

Put put2 = new Put("0003".getBytes());
put2.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(2));
put2.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("刘备"));
put2.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(32));
put2.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put2.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("幽州涿郡涿县"));
put2.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("17888888888"));
put2.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("talk is cheap , show me the code"));

Put put3 = new Put("0004".getBytes());
put3.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(3));
put3.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("孙权"));
put3.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(35));
put3.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put3.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("下邳"));
put3.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("12888888888"));
put3.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("what are you 弄啥嘞!"));

Put put4 = new Put("0005".getBytes());
put4.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(4));
put4.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("诸葛亮"));
put4.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(28));
put4.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put4.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("四川隆中"));
put4.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("14888888888"));
put4.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("出师表你背了嘛"));

Put put5 = new Put("0005".getBytes());
put5.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(5));
put5.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("司马懿"));
put5.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(27));
put5.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put5.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("哪里人有待考究"));
put5.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15888888888"));
put5.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("跟诸葛亮死掐"));

Put put6 = new Put("0006".getBytes());
put6.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(5));
put6.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("xiaobubu—吕布"));
put6.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(28));
put6.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put6.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("内蒙人"));
put6.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15788888888"));
put6.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("貂蝉去哪了"));

List<Put> listPut = new ArrayList<Put>();
listPut.add(put);
listPut.add(put2);
listPut.add(put3);
listPut.add(put4);
listPut.add(put5);
listPut.add(put6);

myuser.put(listPut);
myuser.close();
}

通过Get查找单行数据

在查询的时候需要注意,列值的类型。

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
public void getData() throws IOException {
//获取连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//获取表对象
Table myuser = connection.getTable(TableName.valueOf("myuser"));
Get get = new Get("0003".getBytes());
get.addColumn("f1".getBytes(),"id".getBytes());
//获取返回结果
Result result = myuser.get(get);
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
//获取列族的名称
String familyName = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
//获取列的名称
String columnName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
if ( familyName.equals("f1") && columnName.equals("id") || columnName.equals("age")){
int value = Bytes.toInt(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
System.out.println("列族名: " + familyName + " ,列名: " + columnName + " ,列值:" + value);
} else {
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
System.out.println("列族名: " + familyName + " ,列名: " + columnName + " ,列值:" + value);
}
}
//关闭表
myuser.close();
}

通过Scan扫描全表

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
public void scanAllData() throws IOException {
//获取连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//获取表对象
Table myuser = connection.getTable(TableName.valueOf("myuser"));
Scan scan = new Scan();
//设置起始和结束的rowkey,扫描结果是:[)类型
scan.setStartRow("0004".getBytes());
scan.setStopRow("0008".getBytes());
ResultScanner scanner = myuser.getScanner(scan);
for (Result result : scanner) {
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
String rowkey = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
//获取列族的名称
String familyName = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
//获取列的名称
String columnName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
if ( familyName.equals("f1") && columnName.equals("id") || columnName.equals("age")){
int value = Bytes.toInt(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
System.out.println("列族名: " + familyName + " ,列名: " + columnName + " ,列值:" + value);
} else {
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
System.out.println("列族名: " + familyName + " ,列名: " + columnName + " ,列值:" + value);
}
}
}
//获取返回结果
myuser.close();
}

过滤器查询

过滤器主要分成两大类——比较过滤器、专用过滤器。其作用是在服务器端判断数据是否满足条件,然后只将满足条件的数据返回给客户端。

比较运算符:

  • LESS
  • LESS_OR_EQUAL
  • EQUAL
  • NOT_EQUAL
  • GREATER_OR_EQUAL
  • GREATER
  • NO_OP 排除所有

比较器:

  • BinaryComparator:按字节索引顺序比较指定字节数组
  • BinaryPrefixComparator:同上,比较左端的数据是否相同
  • NullComparator:判断给定的是否为空
  • BitComparator:按位比较
  • RegexStringComparator:提供一个正则比较强,仅支持EQUAL和非EQUAL
  • SubstringComparator:判断提供的字串是否出现在value中

比较过滤器

rowkey过滤器RowFilter
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
public void rowKeyFilter() throws IOException {
//获取连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//获取表对象
Table myuser = connection.getTable(TableName.valueOf("myuser"));
Scan scan = new Scan();

/**
* 过滤器代码部分 start
*/
RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS, new BinaryComparator(Bytes.toBytes("0003")));
scan.setFilter(rowFilter);
/**
* 过滤器代码部分 end
*/

ResultScanner scanner = myuser.getScanner(scan);
for (Result result : scanner) {
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
String rowkey = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
//获取列族的名称
String familyName = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
//获取列的名称
String columnName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
if ( familyName.equals("f1") && columnName.equals("id") || columnName.equals("age")){
int value = Bytes.toInt(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
System.out.println("列族名: " + familyName + " ,列名: " + columnName + " ,列值:" + value);
} else {
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
System.out.println("列族名: " + familyName + " ,列名: " + columnName + " ,列值:" + value);
}
}
}
//获取返回结果
myuser.close();
}

以下代码只提供过滤器部分的代码,其它的代码同上一样

列族过滤器 FamilyFilter
1
2
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.LESS, new SubstringComparator("f2"));
scan.setFilter(familyFilter);
列过滤器 QualifierFilter
1
2
QualifierFilter name = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("name"));
scan.setFilter(name);
列值过滤器 ValueFilter
1
2
ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("8"));
scan.setFilter(valueFilter);

专用过滤器

单列值过滤器 SingleColumnValueFilter

返回满足条件数据的所有字段

1
2
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("f1".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, "刘备".getBytes());
scan.setFilter(singleColumnValueFilter);
列值排除过滤器 SingleColumnValueExcludeFilter

与SingleColumnValueFilter相反,会排除掉指定的列,其他的列全部返回。

rowkey前缀过滤器 PrefixFilter
1
2
PrefixFilter prefixFilter = new PrefixFilter("00".getBytes());
scan.setFilter(prefixFilter);
分页过滤器 PageFilter
1
2


多过滤器查询 FilterList

当需要使用多个过滤器来筛选数据的时候,可以采用FilterList。

1
2
3
4
5
6
FilterList filterList = new FilterList();
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("f1".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, "刘备".getBytes());
PrefixFilter prefixFilter = new PrefixFilter("00".getBytes());
filterList.addFilter(singleColumnValueFilter);
filterList.addFilter(prefixFilter);
scan.setFilter(filterList);

删除数据

1
2
3
4
5
6
7
8
9
10
11
12
13
public void deleteData() throws IOException {
//获取连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//获取表对象
Table myuser = connection.getTable(TableName.valueOf("myuser"));

Delete delete = new Delete("0007".getBytes());
myuser.delete(delete);

myuser.close();
}

删除表

1
2
3
4
5
6
7
8
9
10
11
12
public void deleteTable() throws IOException {
//获取连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//获取管理员对象
Admin admin = connection.getAdmin();
//禁用表
admin.disableTable(TableName.valueOf("myuser"));
//删除表
admin.deleteTable(TableName.valueOf("myuser"));
}
-------------本文结束感谢您的阅读-------------

本文标题:使用java API操作hbase增删改查的几个操作示例

文章作者:Mr.wj

发布时间:2020年01月13日 - 21:05

最后更新:2020年01月13日 - 21:12

原始链接:https://www.wjqixige.cn/2020/01/13/使用java-API操作hbase增删改查的几个操作示例/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Mr.wj wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!