HBase工具类

1.HBASEUtil.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
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
package HBaseUtil;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.TreeSet;
/**
* @ClassName HBASEUTIL
* @MethodDesc: TODO HBASEUTIL功能介绍
* @Author Movle
* @Date 5/10/20 8:04 下午
* @Version 1.0
* @Email movle_xjk@foxmail.com
**/


public class HBASEUtil {

/**
* 创建命名
* */
public static void create_Namecpace(Configuration conf,String namecpace) throws IOException {
//获取hbase的客户端
Connection connection = ConnectionFactory.createConnection(conf);
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
//创建命名描述器
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(namecpace).
addConfiguration("author","plus").build();

//创建命名空间
admin.createNamespace(namespaceDescriptor);
System.out.println("初始化命名空间");
close(admin, connection);
}

/**
* 关闭资源
* */
private static void close(Admin admin, Connection connection) throws IOException {
if (admin != null) {
admin.close();
}
if (connection != null) {
connection.close();
}
}

/**
* 创建HBase的表
* @param conf
* @param tableName
* @param regions
* @param columnFamily
*/
public static void createTable(Configuration conf, String tableName, int regions, String... columnFamily) throws IOException {
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
//判断表
if (isExistTable(conf, tableName)) {
return;
}
//表描述器 HTableDescriptor

HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
for (String cf : columnFamily) {
//列描述器 :HColumnDescriptor
htd.addFamily(new HColumnDescriptor(cf));
}
//htd.addCoprocessor("hbase.CalleeWriteObserver");
//创建表
admin.createTable(htd,genSplitKeys(regions));
System.out.println("已建表");
//关闭对象
close(admin,connection);
}

/**
* 分区键
* @param regions region个数
* @return splitKeys
*/
private static byte[][] genSplitKeys(int regions) {
//存放分区键的数组
String[] keys = new String[regions];
//格式化分区键的形式 00 01 02
DecimalFormat df = new DecimalFormat("000");
for (int i = 0; i < regions; i++) {
keys[i] = df.format(i*10) + "";
}


byte[][] splitKeys = new byte[regions][];
//排序 保证你这个分区键是有序得
TreeSet<byte[]> treeSet = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
for (int i = 0; i < regions; i++) {
treeSet.add(Bytes.toBytes(keys[i]));
}

//输出
Iterator<byte[]> iterator = treeSet.iterator();
int index = 0;
while (iterator.hasNext()) {
byte[] next = iterator.next();
splitKeys[index++]= next;
}

return splitKeys;
}


/**
* 判断表是否存在
* @param conf 配置 conf
* @param tableName 表名
*/
public static boolean isExistTable(Configuration conf, String tableName) throws IOException {
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();


boolean result = admin.tableExists(TableName.valueOf(tableName));
close(admin, connection);
return result;
}
}
2.HBaseDAO.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
package HBaseUtil;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
/**
* @ClassName HBaseDAO
* @MethodDesc:
* @Author Movle
* @Date 5/10/20 8:06 下午
* @Version 1.0
* @Email movle_xjk@foxmail.com
**/


public class HBaseDAO {

private static String namespace = PropertiesUtil.getProperty("hbase.calllog.namespace");
private static String tableName = PropertiesUtil.getProperty("hbase.calllog.tablename");
private static Integer regions = Integer.valueOf(PropertiesUtil.getProperty("hbase.calllog.regions"));

public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.zookeeper.quorum", "hadoop2");
conf.set("zookeeper.znode.parent", "/hbase");

if (!HBASEUtil.isExistTable(conf, tableName)) {
HBASEUtil.create_Namecpace(conf, namespace);
HBASEUtil.createTable(conf, tableName, regions, "f1", "f2");
}
}
}
3.PropertiesUtil.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
package HBaseUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* @ClassName PropertiesUtil
* @MethodDesc: TODO PropertiesUtil功能介绍
* @Author Movle
* @Date 5/10/20 8:07 下午
* @Version 1.0
* @Email movle_xjk@foxmail.com
**/


public class PropertiesUtil {

public static Properties properties = null;
static {
//获取配置文件、方便维护
InputStream is = ClassLoader.getSystemResourceAsStream("hbase_consumer.properties");
properties = new Properties();

try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
}


/**
* 获取参数值
* @param key 名字
* @return 参数值
*/
public static String getProperty(String key){
return properties.getProperty(key);
}
}
4.hbase_consumer.properties
1
2
3
hbase.calllog.namespace=plus
hbase.calllog.tablename=plus:split
hbase.calllog.regions=5
打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2015-2021 Movle
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信