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