TableStorage-HBase-Client开发示例
更新时间:2019-06-18
配置示例
下面是hbase-site.xml示例:
Plain Text
1<?xml version="1.0"?>
2<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
3<configuration>
4 <property>
5 <name>hbase.client.connection.impl</name>
6 <value>com.baidubce.services.tablestoragehbaseclient.hbase.TablestorageConnection</value>
7 </property>
8 <property>
9 <name>tablestorage.client.endpoint</name>
10 <value>http://bts.bd.baidubce.com</value>
11 </property>
12 <property>
13 <name>tablestorage.client.instancename</name>
14 <value>tablestorage_test</value>
15 </property>
16 <property>
17 <name>tablestorage.client.accesskeyid</name>
18 <value>your-access-key-id</value>
19 </property>
20 <property>
21 <name>tablestorage.client.secretaccesskey</name>
22 <value>your-secret-access-key</value>
23 </property>
24</configuration>
详细示例
初始化
Plain Text
1Configuration conf = HBaseConfiguration.create();
2Connection connection = ConnectionFactory.createConnection(conf);
3Admin admin = connection.getAdmin();
4Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
5BufferedMutator bufferedMutator = connection.getBufferedMutator(TableName.valueOf(TABLE_NAME));
6RegionLocator regionLocator = connection.getRegionLocator(TableName.valueOf(TABLE_NAME));
Admin接口示例
创建表
Plain Text
1try {
2 System.out.println("--------------createTable---------------");
3 HTableDescriptor createDescriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
4 HColumnDescriptor createColumnDescriptor = new HColumnDescriptor(COLUMN_FAMILY_NAME);
5 createColumnDescriptor.setTimeToLive(24 * 3600);
6 createColumnDescriptor.setCompressionType(Compression.Algorithm.SNAPPY);
7 createDescriptor.addFamily(createColumnDescriptor);
8 admin.createTable(createDescriptor);
9} catch (IOException e) {
10 e.printStackTrace();
11}
修改column信息
Plain Text
1try {
2 System.out.println("--------------modifyColumn---------------");
3 HColumnDescriptor modifyColumnDescriptor = new HColumnDescriptor(COLUMN_FAMILY_NAME);
4 modifyColumnDescriptor.setTimeToLive(HConstants.FOREVER);
5 modifyColumnDescriptor.setCompressionType(Compression.Algorithm.NONE);
6 admin.modifyColumn(TableName.valueOf(TABLE_NAME), modifyColumnDescriptor);
7} catch (IOException e) {
8 e.printStackTrace();
9}
删除表
Plain Text
1try {
2 System.out.println("--------------deleteTable---------------");
3 admin.deleteTable(TableName.valueOf(TABLE_NAME));
4} catch (IOException e) {
5 e.printStackTrace();
6}
获取表信息
Plain Text
1try {
2 System.out.println("--------------getTable---------------");
3 HTableDescriptor descriptor = admin.getTableDescriptor(TableName.valueOf(TABLE_NAME));
4 for (HColumnDescriptor columnDescriptor : descriptor.getFamilies()) {
5 System.out.println(columnDescriptor.getCompressionType());
6 System.out.println(columnDescriptor.getTimeToLive());
7 }
8} catch (IOException e) {
9 e.printStackTrace();
10}
列举表
Plain Text
1try {
2 System.out.println("--------------listTable---------------");
3 TableName[] tableNames = admin.listTableNames();
4 for (TableName tableName : tableNames) {
5 System.out.println(tableName.getNameAsString());
6 }
7
8 System.out.println("--------------listTable with pattern---------------");
9 tableNames = admin.listTableNames("Table.*");
10 for (TableName tableName : tableNames) {
11 System.out.println(tableName.getNameAsString());
12 }
13} catch (IOException e) {
14 e.printStackTrace();
15}
Table接口示例
向Table写入数据
Plain Text
1try {
2 System.out.println("--------------put---------------");
3 Put put = new Put(ROW_KEY);
4 put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, DEFAULT_VALUE);
5 table.put(put);
6
7 System.out.println("--------------batchPut---------------");
8 List<Put> putList = new ArrayList<>();
9 for (int i = 0; i < 50; i++) {
10 Put put1 = new Put(Bytes.toBytes("row_" + i));
11 for (int j = 0; j < 10; j++) {
12 put1.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("col_" + j),
13 Bytes.toBytes("value_" + i + "_" + j));
14 }
15 putList.add(put1);
16 }
17 table.put(putList);
18} catch (IOException e) {
19 e.printStackTrace();
20}
从Table读取数据
Plain Text
1try {
2 System.out.println("--------------get---------------");
3 Get get = new Get(ROW_KEY);
4 get.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME);
5 Result result = table.get(get);
6 System.out.println("result: " + result.toString());
7 System.out.println("result: " + Bytes.toString(result.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME)));
8
9 System.out.println("--------------batchGet---------------");
10 List<Get> getList = new ArrayList<>();
11 for (int i = 0; i < 55; i++) {
12 Get get1 = new Get(Bytes.toBytes("row_" + i));
13 for (int j = 0; j < 10; j++) {
14 get1.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("col_" + j));
15 }
16 getList.add(get1);
17 }
18 getList.add(get);
19 Result[] results = table.get(getList);
20 for (Result result1 : results) {
21 System.out.println("result: " + result1.toString());
22 }
23
24 System.out.println("--------------scan---------------");
25 Scan scan = new Scan();
26 scan.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME);
27 ResultScanner scanner = table.getScanner(scan);
28 try {
29 for (Result row : scanner) {
30 byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME);
31 System.out.println("\t row: " + Bytes.toString(row.getRow())
32 + ", value: " + Bytes.toString(valueBytes));
33 }
34 } catch (Exception e) {
35 e.printStackTrace();
36 } finally {
37 scanner.close();
38 }
39} catch (IOException e) {
40 e.printStackTrace();
41}
删除Table中的数据
Plain Text
1try {
2 System.out.println("--------------delete---------------");
3 Delete delete = new Delete(ROW_KEY);
4 delete.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME);
5 table.delete(delete);
6
7 System.out.println("--------------batchDelete---------------");
8 List<Delete> deleteList = new ArrayList<>();
9 for (int i = 0; i < 50; i++) {
10 Delete delete1 = new Delete(Bytes.toBytes("row_" + i));
11 for (int j = 0; j < 10; j++) {
12 delete1.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("col_" + j));
13 }
14 deleteList.add(delete1);
15 }
16 table.delete(deleteList);
17} catch (IOException e) {
18 e.printStackTrace();
19}
BufferedMutator接口示例
mutate操作
Plain Text
1try {
2 System.out.println("--------------buffered mutation mutate---------------");
3 Put put1 = new Put(ROW_KEY);
4 put1.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, DEFAULT_VALUE);
5 bufferedMutator.mutate(put1);
6 System.out.println("buffered size: " + bufferedMutator.getWriteBufferSize());
7 Delete delete2 = new Delete(ROW_KEY);
8 delete2.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME);
9 bufferedMutator.mutate(delete2);
10 System.out.println("buffered size: " + bufferedMutator.getWriteBufferSize());
11
12 System.out.println("--------------buffered mutation batchMutate---------------");
13 List<Mutation> mutationList = new ArrayList<>();
14 for (int i = 0; i < 50; i++) {
15 Put putMutation = new Put(Bytes.toBytes("row_" + i));
16 for (int j = 0; j < 10; j++) {
17 putMutation.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("col_" + j),
18 Bytes.toBytes("value_" + i + "_" + j));
19 }
20 mutationList.add(putMutation);
21 }
22 for (int i = 0; i < 50; i++) {
23 Delete deleteMutation = new Delete(Bytes.toBytes("row_" + i));
24 for (int j = 0; j < 10; j++) {
25 deleteMutation.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("col_" + j));
26 }
27 mutationList.add(deleteMutation);
28 }
29 bufferedMutator.mutate(mutationList);
30 System.out.println("buffered size: " + bufferedMutator.getWriteBufferSize());
31
32
33 System.out.println("--------------buffered mutation flush---------------");
34 bufferedMutator.flush();
35} catch (IOException e) {
36 e.printStackTrace();
37}
RegionLocator接口示例
获取表中指定Region的信息
Plain Text
1try {
2 System.out.println("--------------Region locator getRegionLocation---------------");
3 HRegionLocation location = regionLocator.getRegionLocation(ROW_KEY);
4 System.out.println(location.getHostnamePort());
5 location = regionLocator.getRegionLocation(ROW_KEY, true);
6 System.out.println(location.getHostnamePort());
7} catch (IOException e) {
8 e.printStackTrace();
9}
获取表中所有Region的信息
Plain Text
1try {
2 System.out.println("--------------Region locator getAllRegionLocations---------------");
3 List<HRegionLocation> hRegionLocationList = regionLocator.getAllRegionLocations();
4 for (HRegionLocation location : hRegionLocationList) {
5 System.out.println(location.getHostnamePort());
6 }
7} catch (IOException e) {
8 e.printStackTrace();
9}
获取表中所有Region的startKey
Plain Text
1try {
2 System.out.println("--------------Region locator getStartKeys---------------");
3 byte[][] startKeys = regionLocator.getStartKeys();
4 for (byte[] startKey : startKeys) {
5 System.out.println(Bytes.toString(startKey));
6 }
7} catch (IOException e) {
8 e.printStackTrace();
9}
获取表中所有Region的endKey
Plain Text
1try {
2 System.out.println("--------------Region locator getEndKeys---------------");
3 byte[][] stopKeys = regionLocator.getEndKeys();
4 for (byte[] stopKey : stopKeys) {
5 System.out.println(Bytes.toString(stopKey));
6 }
7} catch (IOException e) {
8 e.printStackTrace();
9}
获取表中所有Region的startKey和endKey
Plain Text
1try {
2 System.out.println("--------------Region locator getStartEndKeys---------------");
3 Pair<byte[][], byte[][]> startEndKeys = regionLocator.getStartEndKeys();
4 for (int i = 0; i < startEndKeys.getFirst().length; i++) {
5 StringBuffer buffer = new StringBuffer();
6 buffer.append("startKey: ");
7 buffer.append(Bytes.toString(startEndKeys.getFirst()[i]));
8 buffer.append(", stopKey: ");
9 buffer.append(Bytes.toString(startEndKeys.getSecond()[i]));
10 System.out.println(buffer.toString());
11 }
12} catch (IOException e) {
13 e.printStackTrace();
14}
Hello World
Plain Text
1package com.baidubce.services.tablestoragehbaseclient.hbase;
2
3import org.apache.hadoop.conf.Configuration;
4import org.apache.hadoop.hbase.HBaseConfiguration;
5import org.apache.hadoop.hbase.HColumnDescriptor;
6import org.apache.hadoop.hbase.HConstants;
7import org.apache.hadoop.hbase.HRegionLocation;
8import org.apache.hadoop.hbase.HTableDescriptor;
9import org.apache.hadoop.hbase.TableName;
10import org.apache.hadoop.hbase.client.Admin;
11import org.apache.hadoop.hbase.client.BufferedMutator;
12import org.apache.hadoop.hbase.client.Connection;
13import org.apache.hadoop.hbase.client.ConnectionFactory;
14import org.apache.hadoop.hbase.client.Delete;
15import org.apache.hadoop.hbase.client.Get;
16import org.apache.hadoop.hbase.client.Put;
17import org.apache.hadoop.hbase.client.RegionLocator;
18import org.apache.hadoop.hbase.client.Result;
19import org.apache.hadoop.hbase.client.ResultScanner;
20import org.apache.hadoop.hbase.client.Scan;
21import org.apache.hadoop.hbase.client.Table;
22import org.apache.hadoop.hbase.io.compress.Compression;
23import org.apache.hadoop.hbase.util.Bytes;
24
25import java.io.IOException;
26import java.util.ArrayList;
27import java.util.List;
28
29public class TablestorageDemo {
30 private static final String TABLE_NAME = "HBaseClientTest";
31 private static final byte[] ROW_KEY = Bytes.toBytes("www.baidu.com/1");
32 private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("cf0");
33 private static final byte[] COLUMN_NAME = Bytes.toBytes("col_1");
34 private static final byte[] DEFAULT_VALUE = Bytes.toBytes("value_1");
35
36 public static void main(String[] args) {
37 try {
38 runHbaseClientDemo();
39 } catch (IOException e) {
40 e.printStackTrace();
41 }
42 }
43
44 private static void runHbaseClientDemo() throws IOException {
45 Configuration conf = HBaseConfiguration.create();
46 Connection connection = ConnectionFactory.createConnection(conf);
47 Admin admin = connection.getAdmin();
48 Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
49 BufferedMutator bufferedMutator = connection.getBufferedMutator(TableName.valueOf(TABLE_NAME));
50 RegionLocator regionLocator = connection.getRegionLocator(TableName.valueOf(TABLE_NAME));
51
52 try {
53 System.out.println("--------------createTable---------------");
54 HTableDescriptor createDescriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
55 HColumnDescriptor createColumnDescriptor = new HColumnDescriptor(COLUMN_FAMILY_NAME);
56 createColumnDescriptor.setTimeToLive(24 * 3600);
57 createColumnDescriptor.setCompressionType(Compression.Algorithm.SNAPPY);
58 createDescriptor.addFamily(createColumnDescriptor);
59 admin.createTable(createDescriptor);
60
61 System.out.println("--------------isTableAvailable---------------");
62 while (!admin.isTableAvailable(TableName.valueOf(TABLE_NAME))) {
63 System.out.println("table is not available now, please wait a moment.");
64 try {
65 Thread.sleep(10 * 1000);
66 } catch (InterruptedException e) {
67 e.printStackTrace();
68 }
69 }
70
71 System.out.println("--------------modifyColumn---------------");
72 HColumnDescriptor modifyColumnDescriptor = new HColumnDescriptor(COLUMN_FAMILY_NAME);
73 modifyColumnDescriptor.setTimeToLive(HConstants.FOREVER);
74 modifyColumnDescriptor.setCompressionType(Compression.Algorithm.NONE);
75 admin.modifyColumn(TableName.valueOf(TABLE_NAME), modifyColumnDescriptor);
76
77 System.out.println("--------------getTable---------------");
78 HTableDescriptor descriptor = admin.getTableDescriptor(TableName.valueOf(TABLE_NAME));
79 for (HColumnDescriptor columnDescriptor : descriptor.getFamilies()) {
80 System.out.println(columnDescriptor.getCompressionType());
81 System.out.println(columnDescriptor.getTimeToLive());
82 }
83
84 System.out.println("--------------listTable---------------");
85 TableName[] tableNames = admin.listTableNames();
86 for (TableName tableName : tableNames) {
87 System.out.println(tableName.getNameAsString());
88 }
89
90 System.out.println("--------------listTable with pattern---------------");
91 tableNames = admin.listTableNames("Table.*");
92 for (TableName tableName : tableNames) {
93 System.out.println(tableName.getNameAsString());
94 }
95
96 System.out.println("--------------put---------------");
97 Put put = new Put(ROW_KEY);
98 put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, DEFAULT_VALUE);
99 table.put(put);
100
101 System.out.println("--------------batchPut---------------");
102 List<Put> putList = new ArrayList<>();
103 for (int i = 0; i < 50; i++) {
104 Put put1 = new Put(Bytes.toBytes("row_" + i));
105 for (int j = 0; j < 10; j++) {
106 put1.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("col_" + j),
107 Bytes.toBytes("value_" + i + "_" + j));
108 }
109 putList.add(put1);
110 }
111 table.put(putList);
112
113 System.out.println("--------------get---------------");
114 Get get = new Get(ROW_KEY);
115 get.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME);
116 Result result = table.get(get);
117 System.out.println("result: " + result.toString());
118 System.out.println("result: " + Bytes.toString(result.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME)));
119
120 System.out.println("--------------batchGet---------------");
121 List<Get> getList = new ArrayList<>();
122 for (int i = 0; i < 55; i++) {
123 Get get1 = new Get(Bytes.toBytes("row_" + i));
124 for (int j = 0; j < 10; j++) {
125 get1.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("col_" + j));
126 }
127 getList.add(get1);
128 }
129 getList.add(get);
130 Result[] results = table.get(getList);
131 System.out.println(getList.size());
132 System.out.println(results.length);
133 for (Result result1 : results) {
134 System.out.println("result: " + result1.toString());
135 }
136
137 System.out.println("--------------scan---------------");
138 Scan scan = new Scan();
139 scan.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME);
140 ResultScanner scanner = table.getScanner(scan);
141 try {
142 for (Result row : scanner) {
143 byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME);
144 System.out.println("\t row: " + Bytes.toString(row.getRow())
145 + ", value: " + Bytes.toString(valueBytes));
146 }
147 } catch (Exception e) {
148 e.printStackTrace();
149 } finally {
150 scanner.close();
151 }
152
153 System.out.println("--------------delete---------------");
154 Delete delete = new Delete(ROW_KEY);
155 delete.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME);
156 table.delete(delete);
157
158 System.out.println("--------------batchDelete---------------");
159 List<Delete> deleteList = new ArrayList<>();
160 for (int i = 0; i < 50; i++) {
161 Delete delete1 = new Delete(Bytes.toBytes("row_" + i));
162 for (int j = 0; j < 10; j++) {
163 delete1.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("col_" + j));
164 }
165 deleteList.add(delete1);
166 }
167 table.delete(deleteList);
168
169 System.out.println("--------------buffered mutation mutate---------------");
170 Put put1 = new Put(ROW_KEY);
171 put1.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, DEFAULT_VALUE);
172 bufferedMutator.mutate(put1);
173 Put put2 = new Put(ROW_KEY);
174 put2.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, DEFAULT_VALUE);
175 bufferedMutator.mutate(put2);
176 System.out.println("buffered size: " + bufferedMutator.getWriteBufferSize());
177 bufferedMutator.flush();
178
179 Delete delete2 = new Delete(ROW_KEY);
180 delete2.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME);
181 bufferedMutator.mutate(delete2);
182 System.out.println("buffered size: " + bufferedMutator.getWriteBufferSize());
183 bufferedMutator.flush();
184
185 System.out.println("--------------buffered mutation batchMutate---------------");
186 List<Mutation> mutationList = new ArrayList<>();
187 for (int i = 0; i < 50; i++) {
188 Put putMutation = new Put(Bytes.toBytes("row_" + i));
189 for (int j = 0; j < 10; j++) {
190 putMutation.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("col_" + j),
191 Bytes.toBytes("value_" + i + "_" + j));
192 }
193 mutationList.add(putMutation);
194 }
195 for (int i = 0; i < 50; i++) {
196 Delete deleteMutation = new Delete(Bytes.toBytes("row_" + i));
197 for (int j = 0; j < 10; j++) {
198 deleteMutation.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("col_" + j));
199 }
200 mutationList.add(deleteMutation);
201 }
202 bufferedMutator.mutate(mutationList);
203 System.out.println("buffered size: " + bufferedMutator.getWriteBufferSize());
204 bufferedMutator.flush();
205
206 System.out.println("--------------Region locator getAllRegionLocations---------------");
207 List<HRegionLocation> hRegionLocationList = regionLocator.getAllRegionLocations();
208 for (HRegionLocation location : hRegionLocationList) {
209 System.out.println(location.getHostnamePort());
210 }
211
212 System.out.println("--------------Region locator getRegionLocation---------------");
213 HRegionLocation location = regionLocator.getRegionLocation(ROW_KEY);
214 System.out.println(location.getHostnamePort());
215 location = regionLocator.getRegionLocation(ROW_KEY, true);
216 System.out.println(location.getHostnamePort());
217
218 System.out.println("--------------Region locator getStartKeys---------------");
219 byte[][] startKeys = regionLocator.getStartKeys();
220 for (byte[] startKey : startKeys) {
221 System.out.println(Bytes.toString(startKey));
222 }
223
224 System.out.println("--------------Region locator getEndKeys---------------");
225 byte[][] stopKeys = regionLocator.getEndKeys();
226 for (byte[] stopKey : stopKeys) {
227 System.out.println(Bytes.toString(stopKey));
228 }
229
230 System.out.println("--------------Region locator getStartEndKeys---------------");
231 Pair<byte[][], byte[][]> startEndKeys = regionLocator.getStartEndKeys();
232 for (int i = 0; i < startEndKeys.getFirst().length; i++) {
233 StringBuffer buffer = new StringBuffer();
234 buffer.append("startKey: ");
235 buffer.append(Bytes.toString(startEndKeys.getFirst()[i]));
236 buffer.append(", stopKey: ");
237 buffer.append(Bytes.toString(startEndKeys.getSecond()[i]));
238 System.out.println(buffer.toString());
239 }
240 } catch (IOException e) {
241 e.printStackTrace();
242 } finally {
243 try {
244 System.out.println("--------------deleteTable---------------");
245 admin.deleteTable(TableName.valueOf(TABLE_NAME));
246 } catch (IOException e) {
247 e.printStackTrace();
248 } finally {
249 regionLocator.close();
250 bufferedMutator.close();
251 table.close();
252 admin.close();
253 connection.close();
254 }
255 }
256 }
257}