-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHbaseUtils.java
More file actions
636 lines (535 loc) · 20.5 KB
/
HbaseUtils.java
File metadata and controls
636 lines (535 loc) · 20.5 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
import com.huawei.common.constants.PathCons;
import com.huawei.security.LoginUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.index.ColumnQualifier;
import org.apache.hadoop.hbase.index.Constants;
import org.apache.hadoop.hbase.index.IndexSpecification;
import org.apache.hadoop.hbase.index.client.IndexAdmin;
import org.apache.hadoop.hbase.index.coprocessor.master.IndexMasterObserver;
import org.apache.hadoop.hbase.io.compress.Compression;
import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
import org.apache.hadoop.hbase.security.User;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.File;
import java.io.IOException;
public class HbaseUtils {
private static HbaseUtils hbaseUtils;
private final static Log LOG = LogFactory.getLog(HbaseUtils.class.getName());
//kerberos用户名
private static String PRNCIPAL_NAME;
private static final String ZOOKEEPER_DEFAULT_LOGIN_CONTEXT_NAME = "Client";
private static final String ZOOKEEPER_SERVER_PRINCIPAL_KEY = "zookeeper.server.principal";
private static final String ZOOKEEPER_DEFAULT_SERVER_PRINCIPAL = "zookeeper/hadoop";
private static final String PATH_TO_KEYTAB = PathCons.PATH_TO_KEYTAB;
private static final String PATH_TO_KRB5_CONF = PathCons.PATH_TO_KRB5_CONF;
private static final String PATH_TO_HDFS_SITE_XML = PathCons.PATH_TO_HDFS_SITE_XML;
private static final String PATH_TO_CORE_SITE_XML = PathCons.PATH_TO_CORE_SITE_XML;
private static final String PATH_TO_HBASE_SITE_XML = PathCons.PATH_TO_HBASE_SITE_XML;
private static Configuration conf = null;
private static Connection conn;
private HbaseUtils() {
}
//获取habase单例对象
public static HbaseUtils getInstance(String keberosUser) {
PRNCIPAL_NAME = keberosUser;
//安全校验
if (hbaseUtils == null) {
try {
init();
login();
conn = ConnectionFactory.createConnection(conf);
LOG.info("Login in success");
return new HbaseUtils();
} catch (IOException e) {
LOG.error("Failed to login because ", e);
System.exit(1);
}
}
return hbaseUtils;
}
private static void init() throws IOException {
conf = HBaseConfiguration.create();
conf.addResource(new Path(PATH_TO_CORE_SITE_XML));
conf.addResource(new Path(PATH_TO_HDFS_SITE_XML));
conf.addResource(new Path(PATH_TO_HBASE_SITE_XML));
}
private static void login() throws IOException {
String userdir = System.getProperty("user.dir") + File.separator + "src" + File.separator
+ "main" + File.separator + "resource" + File.separator;
String userKeytabFile = userdir + "user.keytab";
if (User.isHBaseSecurityEnabled(conf)) {
LoginUtil.setJaasConf(ZOOKEEPER_DEFAULT_LOGIN_CONTEXT_NAME, PRNCIPAL_NAME, userKeytabFile);
LoginUtil.setZookeeperServerPrincipal(ZOOKEEPER_SERVER_PRINCIPAL_KEY,
ZOOKEEPER_DEFAULT_SERVER_PRINCIPAL);
LoginUtil.login(PRNCIPAL_NAME, PATH_TO_KEYTAB, PATH_TO_KRB5_CONF, conf);
}
}
/**
* 创建表
* 列
*
* @param tableName 表名
* @param familyName 列簇名称
*/
public void createTable(String tableName, String familyName) {
LOG.info("Entering testCreateTable.");
HTableDescriptor htd = new HTableDescriptor(tableName);
HColumnDescriptor hcd = new HColumnDescriptor(familyName);
hcd.setDataBlockEncoding(DataBlockEncoding.FAST_DIFF);
hcd.setCompressionType(Compression.Algorithm.SNAPPY);
htd.addFamily(hcd);
Admin admin = null;
try {
// Instantiate an Admin object.
admin = conn.getAdmin();
if (!admin.tableExists(TableName.valueOf(tableName))) {
LOG.info("Creating table...");
admin.createTable(htd);
LOG.info(admin.getClusterStatus());
LOG.info(admin.listNamespaceDescriptors());
LOG.info("Table created successfully.");
} else {
LOG.warn("table already exists");
}
} catch (IOException e) {
LOG.error("Create table failed.", e);
} finally {
if (admin != null) {
try {
// Close the Admin object.
admin.close();
} catch (IOException e) {
LOG.error("Failed to close admin ", e);
}
}
}
LOG.info("Exiting testCreateTable.");
}
/**
* 删除表
*
* @param tableName 表名
*/
public void dropTable(String tableName) {
LOG.info("Entering dropTable.");
Admin admin = null;
try {
admin = conn.getAdmin();
if (admin.tableExists(TableName.valueOf(tableName))) {
// admin.enableTable(TableName.valueOf(tableName));
admin.disableTable(TableName.valueOf(tableName));
// Delete table.
admin.deleteTable(TableName.valueOf(tableName));
}
LOG.info("Drop table successfully.");
} catch (IOException e) {
LOG.error("Drop table failed ", e);
} finally {
if (admin != null) {
try {
// Close the Admin object.
admin.close();
} catch (IOException e) {
LOG.error("Close admin failed ", e);
}
}
}
LOG.info("Exiting dropTable.");
}
/**
* 修改表添加列簇
* HBase通过org.apache.hadoop.hbase.client.Admin的modifyTable方法修改表信息。
*
* @param tableName
* @param family_Name
*/
public void addFamily(String tableName, String family_Name) {
LOG.info("Entering ModifyTable.");
// Specify the column family name.
byte[] familyName = Bytes.toBytes(family_Name);
Admin admin = null;
try {
admin = conn.getAdmin();
HTableDescriptor htd = admin.getTableDescriptor(TableName.valueOf(tableName));
// 检查是否指定列簇名
if (!htd.hasFamily(familyName)) {
// Create the column descriptor.
HColumnDescriptor hcd = new HColumnDescriptor(familyName);
htd.addFamily(hcd);
admin.disableTable((TableName.valueOf(tableName)));
admin.modifyTable(TableName.valueOf(tableName), htd);
admin.enableTable(TableName.valueOf(tableName));
}
LOG.info("Modify table successfully.");
} catch (IOException e) {
LOG.error("Modify table failed ", e);
} finally {
if (admin != null) {
try {
// Close the Admin object.
admin.close();
} catch (IOException e) {
LOG.error("Close admin failed ", e);
}
}
}
LOG.info("Exiting ModifyTable.");
}
/**
* 插入数据
*
* @param tableName 表名
* @param rowkey
* @param family_Name 列簇
* @param qualifier 列名
* @param content 内容
*/
public void insertData(String tableName, String rowkey, String family_Name, String qualifier, String content) {
LOG.info("Entering Put Data");
// 指定列簇名
byte[] familyName = Bytes.toBytes(family_Name);
Table table = null;
try {
table = conn.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowkey));
put.addColumn(familyName, Bytes.toBytes(qualifier), Bytes.toBytes(content));
table.put(put);
LOG.info("Put successfully.");
} catch (IOException e) {
LOG.error("Put failed ", e);
} finally {
if (table != null) {
try {
table.close();
} catch (IOException e) {
LOG.error("Close table failed ", e);
}
}
}
LOG.info("Exiting Put data");
}
/**
* 根据rowkey删除数据
*
* @param tableName 表名
* @param rowkey
*/
public void deleteByRowkey(String tableName, String rowkey) {
LOG.info("Entering deleteByRowkey.");
byte[] rowKey = Bytes.toBytes(rowkey);
Table table = null;
try {
table = conn.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(rowKey);
table.delete(delete);
LOG.info("Delete table successfully.");
} catch (IOException e) {
LOG.error("Delete table failed ", e);
} finally {
if (table != null) {
try {
// Close the HTable object.
table.close();
} catch (IOException e) {
LOG.error("Close table failed ", e);
}
}
}
LOG.info("Exiting deleteByRowkey");
}
/**
* 根据rowkey获取数据
*
* @param tableName 表名
* @param rowkey
*/
public void getDataByRowkey(String tableName, String rowkey) {
LOG.info("Entering GetDataByRowKey");
byte[] rowKey = Bytes.toBytes(rowkey);
Table table = null;
try {
table = conn.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey);
Result result = table.get(get);
// 打印row
for (Cell cell : result.rawCells()) {
LOG.info(Bytes.toString(CellUtil.cloneRow(cell)) + ":"
+ Bytes.toString(CellUtil.cloneFamily(cell)) + ","
+ Bytes.toString(CellUtil.cloneQualifier(cell)) + ","
+ Bytes.toString(CellUtil.cloneValue(cell)));
}
LOG.info("Get data successfully.");
} catch (IOException e) {
LOG.error("Get data failed ", e);
} finally {
if (table != null) {
try {
// Close the HTable object.
table.close();
} catch (IOException e) {
LOG.error("Close table failed ", e);
}
}
}
LOG.info("Exiting GetDataByRowKey.");
}
/**
* 扫描表
*
* @param tableName 表名
*/
public void scanData(String tableName) {
LOG.info("Entering ScanData.");
Table table = null;
ResultScanner rScanner = null;
try {
table = conn.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
// scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
// 设置缓存大小
scan.setCaching(1000);
rScanner = table.getScanner(scan);
// 打印结果
for (Result r = rScanner.next(); r != null; r = rScanner.next()) {
for (Cell cell : r.rawCells()) {
LOG.info(Bytes.toString(CellUtil.cloneRow(cell)) + ":"
+ Bytes.toString(CellUtil.cloneFamily(cell)) + ","
+ Bytes.toString(CellUtil.cloneQualifier(cell)) + ","
+ Bytes.toString(CellUtil.cloneValue(cell)));
}
}
LOG.info("Scan data successfully.");
} catch (IOException e) {
LOG.error("Scan data failed ", e);
} finally {
if (rScanner != null) {
rScanner.close();
}
if (table != null) {
try {
table.close();
} catch (IOException e) {
LOG.error("Close table failed ", e);
}
}
}
LOG.info("Exiting testScanData.");
}
/**
* 扫描表
*
* @param tableName 表名
*/
public ResultScanner getScanData(String tableName) {
LOG.info("Entering ScanData.");
Table table = null;
ResultScanner rScanner = null;
try {
table = conn.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
// scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
// 设置缓存大小
scan.setCaching(1000);
LOG.info("Scan data successfully.");
return table.getScanner(scan);
/*ResultScanner scanner = table.getScanner(scan);
// 打印结果
for (Result r = rScanner.next(); r != null; r = rScanner.next()) {
for (Cell cell : r.rawCells()) {
LOG.info(Bytes.toString(CellUtil.cloneRow(cell)) + ":"
+ Bytes.toString(CellUtil.cloneFamily(cell)) + ","
+ Bytes.toString(CellUtil.cloneQualifier(cell)) + ","
+ Bytes.toString(CellUtil.cloneValue(cell)));
}
}*/
} catch (IOException e) {
LOG.error("Scan data failed ", e);
} finally {
if (rScanner != null) {
rScanner.close();
}
if (table != null) {
try {
table.close();
} catch (IOException e) {
LOG.error("Close table failed ", e);
}
}
}
return null;
}
/**
* 设置单列过滤器
*
* @param tableName 表名
* @param familyName 列簇名
* @param qualifier 列名
* @param compareOp 比较方式
* @param value 比较值
*/
public void singleColumnValueFilter(String tableName, String familyName, String qualifier, CompareFilter.CompareOp compareOp, String value) {
LOG.info("Entering SingleColumnValueFilter.");
Table table = null;
ResultScanner rScanner = null;
try {
table = conn.getTable(TableName.valueOf(tableName));
// 实例化扫描器对象
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(qualifier));
// 设置过滤条件
SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(familyName),
Bytes.toBytes(qualifier), CompareFilter.CompareOp.EQUAL, Bytes.toBytes(value));
scan.setFilter(filter);
// 提交扫描
rScanner = table.getScanner(scan);
// 打印
for (Result r = rScanner.next(); r != null; r = rScanner.next()) {
for (Cell cell : r.rawCells()) {
LOG.info(Bytes.toString(CellUtil.cloneRow(cell)) + ":"
+ Bytes.toString(CellUtil.cloneFamily(cell)) + ","
+ Bytes.toString(CellUtil.cloneQualifier(cell)) + ","
+ Bytes.toString(CellUtil.cloneValue(cell)));
}
}
LOG.info("Single column value filter successfully.");
} catch (IOException e) {
LOG.error("Single column value filter failed ", e);
} finally {
if (rScanner != null) {
rScanner.close();
}
if (table != null) {
try {
table.close();
} catch (IOException e) {
LOG.error("Close table failed ", e);
}
}
}
LOG.info("SingleColumnValueFilter exit");
}
/**
* 创建二级索引
*
* @param tableName 表名
* @param indexName 索引名
* @param familyName 需要索引的列族
* @param qualifier 索引列名
*/
public void createIndex(String tableName, String indexName, String familyName, String qualifier) {
LOG.info("Entering createIndex.");
// 创建索引对象
IndexSpecification iSpec = new IndexSpecification(indexName);
iSpec.addIndexColumn(new HColumnDescriptor(familyName), qualifier, ColumnQualifier.ValueType.String);
IndexAdmin iAdmin = null;
Admin admin = null;
try {
iAdmin = new IndexAdmin(conf);
// 创建二级索引
iAdmin.addIndex(TableName.valueOf(tableName), iSpec);
admin = conn.getAdmin();
// 指定索引列加密方式
admin.disableTable(TableName.valueOf(tableName));
HTableDescriptor htd = admin.getTableDescriptor(TableName.valueOf(tableName));
// 实例化索引列描述
HColumnDescriptor indexColDesc = new HColumnDescriptor(
IndexMasterObserver.DEFAULT_INDEX_COL_DESC);
htd.setValue(Constants.INDEX_COL_DESC_BYTES, indexColDesc.toByteArray());
admin.modifyTable(TableName.valueOf(tableName), htd);
admin.enableTable(TableName.valueOf(tableName));
LOG.info("Create index successfully.");
} catch (IOException e) {
LOG.error("Create index failed.", e);
} finally {
if (admin != null) {
try {
admin.close();
} catch (IOException e) {
LOG.error("Close admin failed ", e);
}
}
if (iAdmin != null) {
try {
iAdmin.close();
} catch (IOException e) {
LOG.error("Close admin failed ", e);
}
}
}
LOG.info("Exiting createIndex.");
}
/**
* 删除二级索引
* @param tableName 表名
* @param indexName 索引名
*/
public void dropIndex(String tableName, String indexName) {
LOG.info("Entering dropIndex.");
IndexAdmin iAdmin = null;
try {
iAdmin = new IndexAdmin(conf);
iAdmin.dropIndex(TableName.valueOf(tableName), indexName);
LOG.info("Drop index successfully.");
} catch (IOException e) {
LOG.error("Drop index failed ", e);
} finally {
if (iAdmin != null) {
try {
iAdmin.close();
} catch (IOException e) {
LOG.error("Close admin failed ", e);
}
}
}
LOG.info("Exiting dropIndex.");
}
/**
* 根据二级索引扫描数据
*/
public void scanDataByIndex(String tableName, String familyName, String qualifier, CompareFilter.CompareOp compareOp, String value) {
LOG.info("Entering ScanDataByIndex.");
Table table = null;
ResultScanner scanner = null;
try {
table = conn.getTable(TableName.valueOf(tableName));
// 为索引列创建过滤器
Filter filter = new SingleColumnValueFilter(Bytes.toBytes(familyName), Bytes.toBytes(qualifier),
CompareFilter.CompareOp.EQUAL, value.getBytes());
Scan scan = new Scan();
scan.setFilter(filter);
scanner = table.getScanner(scan);
LOG.info("Scan indexed data.");
for (Result result : scanner) {
for (Cell cell : result.rawCells()) {
LOG.info(Bytes.toString(CellUtil.cloneRow(cell)) + ":"
+ Bytes.toString(CellUtil.cloneFamily(cell)) + ","
+ Bytes.toString(CellUtil.cloneQualifier(cell)) + ","
+ Bytes.toString(CellUtil.cloneValue(cell)));
}
}
LOG.info("Scan data by index successfully.");
} catch (IOException e) {
LOG.error("Scan data by index failed ", e);
} finally {
if (scanner != null) {
scanner.close();
}
try {
if (table != null) {
table.close();
}
} catch (IOException e) {
LOG.error("Close table failed ", e);
}
}
LOG.info("Exiting scanDataByIndex.");
}
}