HBasePool.java
2.4 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
package com.yoho.trace.store;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import properties.PropertiesFactory;
import java.io.IOException;
/**
* Created by xjipeng on 2017/10/16.
*/
public class HBasePool {
private static final Logger logger = LoggerFactory.getLogger(TraceSpanStore.class) ;
/**
* hbase connection
*/
private static Connection connection = null ;
public static synchronized Connection getConnection(){
if( connection == null ){
try {
//PropertiesFactory.initProperties();
// init hbase config
Configuration hbaseConf = HBaseConfiguration.create();
hbaseConf.set("hbase.zookeeper.quorum", PropertiesFactory.hbase().getZkquorum());
hbaseConf.set("hbase.zookeeper.property.clientPort", "2181");
hbaseConf.set("hbase.defaults.for.version.skip", "true");
connection = ConnectionFactory.createConnection(hbaseConf);
logger.info("create hbase connection success {}, zk {} ", connection, PropertiesFactory.hbase().getZkquorum() );
} catch (IOException e) {
e.printStackTrace();
}
}
return connection ;
}
/**
*
* @param tableName
*/
public static void truncateTable(String tableName){
Table table = null ;
try {
logger.info("start truncateTable {}", tableName);
table = HBasePool.getConnection().getTable(TableName.valueOf(tableName));
HTableDescriptor td = table.getTableDescriptor();
HBasePool.getConnection().getAdmin().disableTable(TableName.valueOf(tableName));
HBasePool.getConnection().getAdmin().truncateTable(TableName.valueOf(tableName),false);
HBasePool.getConnection().getAdmin().enableTable(TableName.valueOf(tableName));
logger.info("after truncateTable {} ", tableName);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
table.close() ;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}