盒子
盒子

HBase基础操作(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
public class HBaseTest {

private Configuration conf;
private Connection conn;

@Before
public void getConfigAndConnection() throws IOException {
conf = HBaseConfiguration.create();
// qurom与clientPort默认属性位于hbase-common包中hbase-default.xml中
conf.set("hbase.zookeeper.quorum", "192.168.153.171");
conf.set("hbase.zookeeper.property.clientPort", "2181");
// 设置重试次数,第一次连接为了及时打印结果,重试1次
conf.set("hbase.client.retries.number", "1");
conn = ConnectionFactory.createConnection(conf);
}

@Test
public void testCreateTable() throws IOException {
Admin admin = conn.getAdmin();
if (!admin.isTableAvailable(TableName.valueOf("testtable"))) {
TableName tableName = TableName.valueOf("testtable");
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
// 获取列族描述器
ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("user")).build();
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
// 获取表描述器
TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
admin.createTable(tableDescriptor);
}
}

@Test
public void testPuts() throws IOException {
List<Put> puts = new ArrayList<>();

Put putOne = new Put(Bytes.toBytes("row1"));
putOne.addColumn(Bytes.toBytes("user"), Bytes.toBytes("qua1"), Bytes.toBytes("shenjian"));
puts.add(putOne);

Put putTwo = new Put(Bytes.toBytes("row1"));
putTwo.addColumn(Bytes.toBytes("user"), Bytes.toBytes("qual2"), Bytes.toBytes("domi"));
puts.add(putTwo);

Table table = conn.getTable(TableName.valueOf("testtable"));
table.put(puts);
}

@Test
public void testCAS() throws IOException {
Put put = new Put(Bytes.toBytes("row1"));
put.addColumn(Bytes.toBytes("user"), Bytes.toBytes("qua1"), Bytes.toBytes("shenjian"));

Table table = conn.getTable(TableName.valueOf("testtable"));
table.checkAndMutate(Bytes.toBytes("row1"), Bytes.toBytes("user"))
.qualifier(Bytes.toBytes("qual"))
.ifNotExists()
.thenPut(put);
}

@Test
public void testGet() throws IOException {
Table table = conn.getTable(TableName.valueOf("testtable"));
Get get = new Get(Bytes.toBytes("row1"));
get.addColumn(Bytes.toBytes("user"), Bytes.toBytes("qua1"));

Result result = table.get(get);
byte[] val = result.getValue(Bytes.toBytes("user"), Bytes.toBytes("qua1"));
Assert.assertEquals("shenjian", Bytes.toString(val));
}

@Test
public void testDelete() throws IOException {
Table table = conn.getTable(TableName.valueOf("testtable"));
Delete delete = new Delete(Bytes.toBytes("row1"));
delete.addColumns(Bytes.toBytes("user"), Bytes.toBytes("qual2"), 1576391337431l);
table.delete(delete);
table.close();
}

@Test
public void testScan() throws IOException {
Table table = conn.getTable(TableName.valueOf("testtable"));
Scan scan = new Scan();
ResultScanner resultScanner = table.getScanner(scan);
scan.addColumn(Bytes.toBytes("user"), Bytes.toBytes("name"));
for (Result res : resultScanner) {
System.out.println(res);
}
}

@After
public void close() throws IOException {
conn.close();
}
}
支持一下
扫一扫,支持沈健
  • 微信扫一扫
  • 支付宝扫一扫