1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase;
19
20 import java.io.IOException;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 import java.util.Map;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29 import org.apache.hadoop.hbase.classification.InterfaceStability;
30 import org.apache.hadoop.hbase.io.util.HeapMemorySizeUtil;
31 import org.apache.hadoop.hbase.util.VersionInfo;
32 import org.apache.hadoop.hbase.zookeeper.ZKConfig;
33
34
35
36
37 @InterfaceAudience.Public
38 @InterfaceStability.Stable
39 public class HBaseConfiguration extends Configuration {
40
41 private static final Log LOG = LogFactory.getLog(HBaseConfiguration.class);
42
43
44
45
46
47 @Deprecated
48 public HBaseConfiguration() {
49
50 super();
51 addHbaseResources(this);
52 LOG.warn("instantiating HBaseConfiguration() is deprecated. Please use"
53 + " HBaseConfiguration#create() to construct a plain Configuration");
54 }
55
56
57
58
59
60 @Deprecated
61 public HBaseConfiguration(final Configuration c) {
62
63 this();
64 merge(this, c);
65 }
66
67 private static void checkDefaultsVersion(Configuration conf) {
68 if (conf.getBoolean("hbase.defaults.for.version.skip", Boolean.FALSE)) return;
69 String defaultsVersion = conf.get("hbase.defaults.for.version");
70 String thisVersion = VersionInfo.getVersion();
71 if (!thisVersion.equals(defaultsVersion)) {
72 throw new RuntimeException(
73 "hbase-default.xml file seems to be for an older version of HBase (" +
74 defaultsVersion + "), this version is " + thisVersion);
75 }
76 }
77
78 public static Configuration addHbaseResources(Configuration conf) {
79 conf.addResource("hbase-default.xml");
80 conf.addResource("hbase-site.xml");
81
82 checkDefaultsVersion(conf);
83 HeapMemorySizeUtil.checkForClusterFreeMemoryLimit(conf);
84 return conf;
85 }
86
87
88
89
90
91 public static Configuration create() {
92 Configuration conf = new Configuration();
93
94
95
96 conf.setClassLoader(HBaseConfiguration.class.getClassLoader());
97 return addHbaseResources(conf);
98 }
99
100
101
102
103
104
105 public static Configuration create(final Configuration that) {
106 Configuration conf = create();
107 merge(conf, that);
108 return conf;
109 }
110
111
112
113
114
115
116
117 public static void merge(Configuration destConf, Configuration srcConf) {
118 for (Map.Entry<String, String> e : srcConf) {
119 destConf.set(e.getKey(), e.getValue());
120 }
121 }
122
123
124
125
126
127
128
129
130 public static Configuration subset(Configuration srcConf, String prefix) {
131 Configuration newConf = new Configuration(false);
132 for (Map.Entry<String, String> entry : srcConf) {
133 if (entry.getKey().startsWith(prefix)) {
134 String newKey = entry.getKey().substring(prefix.length());
135
136 if (!newKey.isEmpty()) {
137 newConf.set(newKey, entry.getValue());
138 }
139 }
140 }
141 return newConf;
142 }
143
144
145
146
147
148
149 public static void setWithPrefix(Configuration conf, String prefix,
150 Iterable<Map.Entry<String, String>> properties) {
151 for (Map.Entry<String, String> entry : properties) {
152 conf.set(prefix + entry.getKey(), entry.getValue());
153 }
154 }
155
156
157
158
159 public static boolean isShowConfInServlet() {
160 boolean isShowConf = false;
161 try {
162 if (Class.forName("org.apache.hadoop.conf.ConfServlet") != null) {
163 isShowConf = true;
164 }
165 } catch (LinkageError e) {
166
167 LOG.warn("Error thrown: ", e);
168 } catch (ClassNotFoundException ce) {
169 LOG.debug("ClassNotFound: ConfServlet");
170
171 }
172 return isShowConf;
173 }
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193 public static int getInt(Configuration conf, String name,
194 String deprecatedName, int defaultValue) {
195 if (conf.get(deprecatedName) != null) {
196 LOG.warn(String.format("Config option \"%s\" is deprecated. Instead, use \"%s\""
197 , deprecatedName, name));
198 return conf.getInt(deprecatedName, defaultValue);
199 } else {
200 return conf.getInt(name, defaultValue);
201 }
202 }
203
204
205
206
207
208
209
210
211
212
213
214 public static String getPassword(Configuration conf, String alias,
215 String defPass) throws IOException {
216 String passwd = null;
217 try {
218 Method m = Configuration.class.getMethod("getPassword", String.class);
219 char[] p = (char[]) m.invoke(conf, alias);
220 if (p != null) {
221 LOG.debug(String.format("Config option \"%s\" was found through" +
222 " the Configuration getPassword method.", alias));
223 passwd = new String(p);
224 }
225 else {
226 LOG.debug(String.format(
227 "Config option \"%s\" was not found. Using provided default value",
228 alias));
229 passwd = defPass;
230 }
231 } catch (NoSuchMethodException e) {
232
233
234 LOG.debug(String.format(
235 "Credential.getPassword method is not available." +
236 " Falling back to configuration."));
237 passwd = conf.get(alias, defPass);
238 } catch (SecurityException e) {
239 throw new IOException(e.getMessage(), e);
240 } catch (IllegalAccessException e) {
241 throw new IOException(e.getMessage(), e);
242 } catch (IllegalArgumentException e) {
243 throw new IOException(e.getMessage(), e);
244 } catch (InvocationTargetException e) {
245 throw new IOException(e.getMessage(), e);
246 }
247 return passwd;
248 }
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263 public static Configuration createClusterConf(Configuration baseConf, String clusterKey)
264 throws IOException {
265 return createClusterConf(baseConf, clusterKey, null);
266 }
267
268
269
270
271
272
273
274
275
276
277
278
279
280 public static Configuration createClusterConf(Configuration baseConf, String clusterKey,
281 String overridePrefix) throws IOException {
282 Configuration clusterConf = HBaseConfiguration.create(baseConf);
283 if (clusterKey != null && !clusterKey.isEmpty()) {
284 applyClusterKeyToConf(clusterConf, clusterKey);
285 }
286
287 if (overridePrefix != null && !overridePrefix.isEmpty()) {
288 Configuration clusterSubset = HBaseConfiguration.subset(clusterConf, overridePrefix);
289 HBaseConfiguration.merge(clusterConf, clusterSubset);
290 }
291 return clusterConf;
292 }
293
294
295
296
297
298
299
300
301 private static void applyClusterKeyToConf(Configuration conf, String key)
302 throws IOException{
303 ZKConfig.ZKClusterKey zkClusterKey = ZKConfig.transformClusterKey(key);
304 conf.set(HConstants.ZOOKEEPER_QUORUM, zkClusterKey.getQuorumString());
305 conf.setInt(HConstants.ZOOKEEPER_CLIENT_PORT, zkClusterKey.getClientPort());
306 conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, zkClusterKey.getZnodeParent());
307 }
308
309
310
311
312
313
314 public static void main(String[] args) throws Exception {
315 HBaseConfiguration.create().writeXml(System.out);
316 }
317 }