Just Do IT !

Hbase Java API接口

字数统计: 624阅读时长: 3 min
2019/11/28 Share

实验环境

Hadoop2.8.0
Hbase-1.4.9
Centos7.2

Java代码

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
package pers.haohan.bigdata.hbase;

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

import java.io.IOException;

public class TestHbaseAPI_1 {

public static void main(String[] args) throws IOException {

// 通过java代码访问java数据库

// 1. 创建配置对象,获取hbase连接
Configuration conf = HBaseConfiguration.create();

// 2. 获取hbase连接对象
// classLoader : Thread.currentThread.getContextClassLoader 获取资源配置文件
// classpath : hbase-default.xml, hbase-site.xml

Connection connection = ConnectionFactory.createConnection(conf);
System.out.println(connection);

// 3. 获取操作对象 : Admin
// Admin admin = new HBaseAdmin(connection);
Admin admin = connection.getAdmin();

// 4. 操作数据库: 判断数据库中是否存在某张表
// 4-1. 判断命名空间

try {
NamespaceDescriptor namespace = admin.getNamespaceDescriptor("test");

} catch (NamespaceNotFoundException e) {
NamespaceDescriptor nd = NamespaceDescriptor.create("test").build();

admin.createNamespace(nd);
}

// 4-2. 判断hbase中是否存在某张表
TableName tableName = TableName.valueOf("test:student");
boolean flg = admin.tableExists(tableName);
System.out.println(flg);

if (flg) {

// 获取指定的表对象
Table table = connection.getTable(tableName);

// 查询数据
// DDL(create,drop, alter), DML(update, insert, delete), DQL(select)
String rowkey = "1001";
// string ==> byte[]

// 字符编码
Get get = new Get(Bytes.toBytes(rowkey));

// 查询结果
Result result = table.get(get);
boolean empty = result.isEmpty();
System.out.println("1001数据是否存在 = " + !empty);

if (empty) {
// 新增数据
Put put = new Put(Bytes.toBytes(rowkey));

String family = "info";
String column = "name";
String val = "zhangsan";

put.addColumn(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(val));

table.put(put);
System.out.println("增加数据");
} else {
// 展示数据
for (Cell cell : result.rawCells()) {

// 输出基本信息
// System.out.println("value = " + CellUtil.cloneValue(cell));
// value = [B@265adfad

System.out.println("value = " + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("raw = " + Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println("family = " + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("column = " + Bytes.toString(CellUtil.cloneQualifier(cell)));

}
}



} else {
// 创建表

// 创建表描述对象
HTableDescriptor td = new HTableDescriptor(tableName);

// 增加列族
HColumnDescriptor cd = new HColumnDescriptor("info");
td.addFamily(cd);

admin.createTable(td);

System.out.println("表格创建成功");

}
}
}

XML文件

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
<?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>pers.haohan.bigdata</groupId>
<artifactId>hbasestudy</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>hbase_mr</module>
</modules>

<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.1</version>
</dependency>

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


</dependencies>


</project>
CATALOG
  1. 1. 实验环境
  2. 2. Java代码
  3. 3. XML文件