您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“ hadoop-common中Configuration的示例代碼 ”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“ hadoop-common中Configuration的示例代碼 ”這篇文章吧。
Configuration類實(shí)現(xiàn)了Iterable、Writable接口,使得可以遍歷和序列化(hadoop自己序列化)
<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="configuration.xsl"?><configuration><property><name>lala</name><value>${user.home}/hadoopdata</value><final>true</final></property></configuration>
hadoop是通過(guò)xml進(jìn)行配置的,同時(shí)支持屬性擴(kuò)展,user.home當(dāng)調(diào)用get的時(shí)候,會(huì)首先通過(guò)System.getProperty()判斷是否是系統(tǒng)參數(shù),例中${user.home}就被替換成當(dāng)前用戶的path。所以,當(dāng)我們?cè)谂渲胔adoop時(shí),可以直接用一些系統(tǒng)屬性,增強(qiáng)可移植性。
當(dāng)一個(gè)屬性被生命為final時(shí),后面添加配置,不會(huì)覆蓋先加在的配置。
同時(shí),因?yàn)槭褂玫氖莏ava的DOM解析,所以支持XML的包涵,在配置文件中可以用<xi:include href="" />
來(lái)分類管理。
private static class Resource { //私有內(nèi)部類,標(biāo)記資源名字和資源對(duì)象private final Object resource;private final String name; ... }
private static class DeprecatedKeyInfo {private final String[] newKeys;private final String customMessage;private final AtomicBoolean accessed = new AtomicBoolean(false);private final String getWarningMessage(String key) { } } public static class DeprecationDelta {private final String key;private final String[] newKeys;private final String customMessage; } private static class DeprecationContext {//存放oldkey-newkeysprivate final Map<String, DeprecatedKeyInfo> deprecatedKeyMap;//存放newkeys-oldkey,提供反查功能private final Map<String, String> reverseDeprecatedKeyMap; DeprecationContext(DeprecationContext other, DeprecationDelta[] deltas) { ...this.deprecatedKeyMap = UnmodifiableMap.decorate(newDeprecatedKeyMap);this.reverseDeprecatedKeyMap =UnmodifiableMap.decorate(newReverseDeprecatedKeyMap); } }
DeprecatedKeyInfo保存了新的key和信息,如果customMessage為空,在調(diào)用getWarningMessage會(huì)自動(dòng)生成默認(rèn)的信息。
DeprecationDelta 保存了被遺棄的key 和 建議用的新key。
DeprecationContext封裝講被遺棄的key和推薦使用的keys、提示封裝在一起。
private static AtomicReference<DeprecationContext> deprecationContext = new AtomicReference<DeprecationContext>( new DeprecationContext(null, defaultDeprecations));
一個(gè)全局的DeprecationContext對(duì)象,原子的,并且將默認(rèn)被遺棄的key加載進(jìn)去。
值得一提的是此方法很巧妙的使用無(wú)鎖的方法,但是,保證了數(shù)據(jù)的安全性,看具體代碼:
public static void addDeprecations(DeprecationDelta[] deltas) { DeprecationContext prev, next; do { prev = deprecationContext.get(); next = new DeprecationContext(prev, deltas); } while (!deprecationContext.compareAndSet(prev, next)); }
compareAndSet方法是當(dāng)前對(duì)象和prev相等(==)時(shí),更新當(dāng)前對(duì)象為next
分析源碼,我們發(fā)現(xiàn),setDeprecatedProperties的作用就是為了更新overlay和properties,使得,我們?cè)讷@得key時(shí),能得到最新的狀態(tài),看下面例子:
configuration.addDeprecation("xx", new String[]{"xx1","xx2","xx3"}); //configuration.setDeprecatedProperties(); System.out.println(configuration.get("xx"));
當(dāng)注釋掉configuration.setDeprecatedProperties
后,我get時(shí),獲得的事null值,所以我們要遍歷已經(jīng)被遺棄的key時(shí),需要更新setDeprecatedProperties,可以使得被遺棄的key依舊可以被使用。
首先判斷該key是否是被遺棄的,如果是,將得到建議用的key,否則更新overlay、properties,并返回建議使用的key數(shù)組。
用樣handleDeprecation方法是,執(zhí)行刷新操作。具體用在asXmlDocument中。
分析代碼我們可以得到一下幾點(diǎn):
如果在classpath下存在hadoop-site.xml,會(huì)log4j會(huì)打印警告信息,沒(méi)有加載到defaultResources。
默認(rèn)加載兩個(gè)核心配置文件core-default.xml、core-site.xml
不管用何種addResource,最終都是調(diào)用了addResourceObject(Resource resource),他首先將資源添加到一個(gè)全局的List集合,然后調(diào)用reloadConfiguration來(lái)觸發(fā)刷新properties并且標(biāo)記為final的key失效。
在hadoop-2.7之前,只有一個(gè)substituteVars方法,使用java自身的正則表達(dá)式來(lái)匹配獲得
${user.home }
中間的值(user.home)。hadoop-2.7版本之后,為了提升性能,自己實(shí)現(xiàn)了匹配獲取中間值的方法(
findSubVariable
) ps:可能是因?yàn)椋捎趈ava自身的正則表達(dá)式方式過(guò)于消耗性能,所以,通過(guò)自己手動(dòng)匹配,降低性能的消耗。
//此方法,將字符串中${}中間的位置的區(qū)間獲取到,詳細(xì)看代碼 private static int[] findSubVariable(String eval) { ... } //1.將獲取key進(jìn)行替換,如果System.getProperty()存在,替換 //2.不存在,查找properties,如果存在替換,不存在,原樣保留 private String substituteVars(String expr) { ... }
//通過(guò)程序設(shè)置key-value,source允許為空,當(dāng)用戶不設(shè)置源時(shí),程序自動(dòng)將programatically這是為source, //當(dāng)值為被遺棄的,此方法會(huì)先將新key的到,并設(shè)置source為 because old key is deprecated public void set(String name, String value, String source) { ... }
該方法是解析xml的,采用了DOM解析,分析代碼我們知道,xml格式需要和上面寫到的格式,同時(shí)DOM解析,支持xml文件引入。
和以前版本相比,xml配置文件中,在property中可以聲明source標(biāo)簽,聲明資源的信息?
if ("source".equals(field.getTagName()) && field.hasChildNodes()) source.add(StringInterner.weakIntern( ((Text)field.getFirstChild()).getData()));
以上是“ hadoop-common中Configuration的示例代碼 ”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。