Hbase学习3-java客户端基本使用

注意:有些优化参数,例如WAL,cache,autoCommit等等没有涉及,后面继续学习。

相关代码:https://github.com/carlosfu/hbase-train

一、pom依赖

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
<properties>
<hadoop.version>2.7.1</hadoop.version>
<hbase.version>1.2.4</hbase.version>
<junit.version>4.11</junit.version>
<slf4j.version>1.7.6</slf4j.version>
<logback.version>1.1.3</logback.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>jsp-api</artifactId>
<groupId>javax.servlet.jsp</groupId>
</exclusion>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>${hbase.version}</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-auth</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
</exclusion>
<exclusion>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

二、连接初始化

  • hbase-site.xml是直接从hbase服务器上拷贝过来的,直接用这个文件作为Hbase客户端初始化的客户端文件。
  • Hbase0.98后,HTable,HTablePool已经被废弃,所有连接使用Connection类管理。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public enum HbaseUtil {
HBASE_BX(HbaseUtil.MAJOR_HBASE_SITE);
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private static final String MAJOR_HBASE_SITE = "hbase/hbase-site.xml";
private Configuration config;
private Connection connection;
private HbaseUtil(String resource) {
try {
this.config = HBaseConfiguration.create();
this.config.addResource(resource);
this.connection = ConnectionFactory.createConnection(config);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
}

三、增删改查

3.1 查
  • 通过tableName, rowkey, family, qualifier来定位到cell(但是cell可能涉及到多版本)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public Result get(String tableName, byte[] rowKey, byte[] family, byte[] qualifier) throws Exception {
Table table = null;
try {
table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey);
get.addColumn(family, qualifier);
Result result = table.get(get);
return result;
} catch (Exception e) {
throw e;
} finally {
if (table != null) {
try {
table.close();
} catch (IOException e) {
throw e;
}
}
}
}
  • 通过tableName, rowkey, family来定位到所有qualifier的cell(但是cell可能涉及到多版本)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public Result get(String tableName, byte[] rowKey, byte[] family) throws Exception {
Table table = null;
try {
table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey);
get.addFamily(family);
Result result = table.get(get);
return result;
} catch (Exception e) {
throw e;
} finally {
if (table != null) {
try {
table.close();
} catch (IOException e) {
throw e;
}
}
}
}
  • 打印Result

    result.list()已经被废弃
    result.rawCells()中包含了rowkey,family,qualifier,value多维度信息,使用CellUtil即可获取。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private void printResult(Result result) {
if (result == null) {
return;
}
//list()已经被废弃
List<KeyValue> keyValues = result.list();
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
byte[] rowKeyByte = CellUtil.cloneRow(cell);
byte[] familyByte = CellUtil.cloneFamily(cell);
byte[] qualifierByte = CellUtil.cloneQualifier(cell);
byte[] valueByte = CellUtil.cloneValue(cell);
logger.info("rowkey:{}, family:{}, column:{}, value:{}", Bytes.toString(rowKeyByte),Bytes.toString(familyByte), Bytes.toString(qualifierByte), Bytes.toString(valueByte));
}
}
3.2 增

增涉及到元素: tableName, rowKey, family, qualifier, version(可选), value
批量增:table.put(puts);

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
public void put(String tableName, CustomHbaseModel customHbaseModel) throws Exception {
if (customHbaseModel == null) {
return;
}
Table table = null;
try {
table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(customHbaseModel.getRowKey());
long version = customHbaseModel.getVersion();
if (version > 0) {
put.addColumn(customHbaseModel.getFamily(), customHbaseModel.getQualifier(), version,
customHbaseModel.getValue());
} else {
put.addColumn(customHbaseModel.getFamily(), customHbaseModel.getQualifier(),
customHbaseModel.getValue());
}
table.put(put);
} catch (Exception e) {
throw e;
} finally {
if (table != null) {
try {
table.close();
} catch (IOException e) {
throw e;
}
}
}
}
public void batchPut(String tableName, List<CustomHbaseModel> customHbaseModelList) throws Exception {
Table table = null;
try {
List<Put> puts = new ArrayList<Put>();
for (CustomHbaseModel customHbaseModel : customHbaseModelList) {
Put put = new Put(customHbaseModel.getRowKey());
long version = customHbaseModel.getVersion();
if (version > 0) {
put.addColumn(customHbaseModel.getFamily(), customHbaseModel.getQualifier(), version,
customHbaseModel.getValue());
} else {
put.addColumn(customHbaseModel.getFamily(), customHbaseModel.getQualifier(),
customHbaseModel.getValue());
}
puts.add(put);
}
table = connection.getTable(TableName.valueOf(tableName));
table.put(puts);
} catch (Exception e) {
throw e;
} finally {
if (table != null) {
try {
table.close();
} catch (IOException e) {
throw e;
}
}
}
}
3.3 删

删: tableName, rowKey, family, qualifer, version(可选)
注意删只事做标记,只有在major compact才会真正执行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void delete(String tableName, byte[] rowKey, byte[] family, byte[] qualifier) throws Exception {
Table table = null;
try {
table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(rowKey);
delete.addColumn(family, qualifier);
table.delete(delete);
} catch (Exception e) {
throw e;
} finally {
if (table != null) {
try {
table.close();
} catch (IOException e) {
throw e;
}
}
}
}