HBase API操作

##1. 新建Java Project
新建一个Java Project后,引入HBase的所有jar包,将服务器上的hbase-site.xml拷贝到src目录

##2. 总体架构
表管理类操作模式:

1
2
3
4
Configuration config = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(config);
......
admin.doSomething();

表操作类模式:

1
2
3
4
5
Configuration conf = HBaseConfiguration.create();
String tableName = ...;
HTable table = new HTable(conf, tableName);
......
table.doSomething();

###2.1 创建表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class CreateTableAction {
/**
* @param args
* @throws IOException
* @throws ZooKeeperConnectionException
* @throws MasterNotRunningException
*/
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
Configuration config = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(config);
TableName tableName = TableName.valueOf("table3");
HTableDescriptor descriptor = new HTableDescriptor(tableName);
HColumnDescriptor colDescriptor = new HColumnDescriptor("col1");
descriptor.addFamily(colDescriptor);
admin.createTable(descriptor);
}
}

###2.2 删除表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class DeleteTableAction {
/**
* @param args
* @throws IOException
* @throws ZooKeeperConnectionException
* @throws MasterNotRunningException
*/
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
Configuration config = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(config);
String tableName = "table3";
admin.disableTable(tableName); // delete前必须先disable
admin.deleteTable(tableName);
}
}

###2.3 列出所有表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class ListTablesAction {
/**
* @param args
* @throws IOException
* @throws ZooKeeperConnectionException
* @throws MasterNotRunningException
*/
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
Configuration config = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(config);
TableName[] tableNames = admin.listTableNames();
for (TableName name : tableNames) {
System.out.println("Table: " + name.getNameAsString());
}
}
}

###2.4 插入单条记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class InsertOneRowAction {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
String tableName = "table2";
HTable table = new HTable(conf, tableName);
Put put = new Put("row2".getBytes());
put.add("col1".getBytes(), null, "2".getBytes()); // 第一个参数是列簇名,第二个参数是限定符(我的理解就是子列名),第三个参数是值
table.put(put);
}
}

hbase shell查询

1
2
3
4
hbase(main):004:0> scan 'table2'
ROW COLUMN+CELL
row2 column=col1:, timestamp=1400593778072, value=2
1 row(s) in 0.0700 seconds

PS: put方法不是线程安全的

###2.5 批量插入

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
public class InsertRowsAction {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
String tableName = "table2";
HTable table = new HTable(conf, tableName);
Put put3 = new Put("row3".getBytes());
put3.add("col1".getBytes(), null, "3".getBytes());
Put put4 = new Put("row4".getBytes());
put4.add("col1".getBytes(), null, "4".getBytes());
List<Put> puts = new ArrayList<Put>();
puts.add(put3);
puts.add(put4);
table.put(puts); //参数为List<Put>就是批量插入
table.close();
}
}

###2.5 单条查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class GetOneRowAction {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
String tableName = "table2";
HTable table = new HTable(conf, tableName);
Get get = new Get("row2".getBytes());
Result result = table.get(get);
System.out.println(new String(result.getRow())); // 获取row key
System.out.println(new String(result.getValue("col1".getBytes(), null))); // 获取指定字段的值
}
}

###2.6 批量查询

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
public class GetRowsAction {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
String tableName = "table2";
HTable table = new HTable(conf, tableName);
System.out.println("======= 全表扫描 =========");
Scan scan = new Scan();
ResultScanner scaner = table.getScanner(scan);
for (Result result : scaner) {
System.out.println(new String(result.getRow()) + " : " + new String(result.getValue("col1".getBytes(), null))); // 获取row key
}
System.out.println("======= 指定范围扫描 =========");
Scan scan2 = new Scan("row2".getBytes());
ResultScanner scaner2 = table.getScanner(scan2);
for (Result result : scaner2) {
System.out.println(new String(result.getRow()) + " : " + new String(result.getValue("col1".getBytes(), null))); // 获取row key
}
}
}

输出结果:

1
2
3
4
5
6
7
======= 全表扫描 =========
ro43 : 4
row2 : 2
row3 : 3
======= 指定范围扫描 =========
row2 : 2
row3 : 3

###2.7 删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class DeleteOneRowAction {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
String tableName = "table2";
HTable table = new HTable(conf, tableName);
Delete delete = new Delete("ro43".getBytes());
table.delete(delete);
}
}

执行前

1
2
3
4
5
6
hbase(main):001:0> scan 'table2'
ROW COLUMN+CELL
ro43 column=col1:, timestamp=1400594899049, value=4
row2 column=col1:, timestamp=1400593778072, value=2
row3 column=col1:, timestamp=1400594899049, value=3
3 row(s) in 0.2390 seconds

运行后

1
2
3
4
5
hbase(main):002:0> scan 'table2'
ROW COLUMN+CELL
row2 column=col1:, timestamp=1400593778072, value=2
row3 column=col1:, timestamp=1400594899049, value=3
2 row(s) in 0.0680 seconds

###2.8 更新

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class UpdateOneRowAction {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
String tableName = "table2";
HTable table = new HTable(conf, tableName);
Put put = new Put("row2".getBytes());
put.add("col1".getBytes(), null, "2_new".getBytes()); // HBase的更新其实就是put一条row key一样的记录,HBase根据记录的时间戳可以知道这是哪条是最新的记录
table.put(put);
}
}

更新前

1
2
3
4
5
hbase(main):002:0> scan 'table2'
ROW COLUMN+CELL
row2 column=col1:, timestamp=1400593778072, value=2
row3 column=col1:, timestamp=1400594899049, value=3
2 row(s) in 0.0680 seconds

更新后

1
2
3
4
5
hbase(main):003:0> scan 'table2'
ROW COLUMN+CELL
row2 column=col1:, timestamp=1400598165614, value=2_new
row3 column=col1:, timestamp=1400594899049, value=3
2 row(s) in 0.0860 seconds