zookeeper常用java api

常用的zookeeper 操作api肮砾,持續(xù)更新中...

import java.io.IOException;

import java.security.NoSuchAlgorithmException;

import java.util.ArrayList;

import java.util.List;

import org.apache.zookeeper.CreateMode;

import org.apache.zookeeper.WatchedEvent;

import org.apache.zookeeper.Watcher;

import org.apache.zookeeper.ZooDefs;

import org.apache.zookeeper.ZooDefs.Ids;

import org.apache.zookeeper.ZooKeeper;

import org.apache.zookeeper.data.ACL;

import org.apache.zookeeper.data.Id;

import org.apache.zookeeper.data.Stat;

import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

/**

? * Test Method {create/exists/delete/getChildren/setData/getData/addAuthInfo/setACL/getACL}

? *

? * @author yinkaipeng

? *

? */

public class ZooJavaApi {

? ? private static final int SESSION_TIMEOUT = 1000;

? ? public static final Logger LOGGER = LoggerFactory.getLogger(ZooJavaApi.class);

? ? public static final String HOST = "192.168.3.17:2181,192.168.3.18:2181,192.168.3.9:2181";

? ? private static Watcher watcher = new Watcher() {

? ? ? ? public void process(WatchedEvent we) {

? ? ? ? ? ? LOGGER.info("process:" + we.getType());

? ? ? ? }

? ? };


? ? private static ZooKeeper zookeeper;

? ? private static void connect(){

? ? try {

zookeeper= new ZooKeeper(HOST, SESSION_TIMEOUT, watcher);

System.out.println("I am connected ok");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

? ? }



? ? public static void main(String[] args) throws Exception {

? ? connect();

? ? ZooJavaApi? test =new ZooJavaApi();

? ? //test.testGetChildren("/");

? ? //test.testCreate("/zk2","hello");

? ? //test.testGetAcl("/zk2");

? ? //test.testSetAcl("/zk2", "yinkp:yinkp");

? ? test.testAddAuthInfo("/zk2", "yinkp:yinkp");

? ? ?test.close();

}


? ? public void close() {

? ? ? ? try {

? ? ? ? ? ? zookeeper.close();

? ? ? ? } catch (InterruptedException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }finally{

? ? ? ? System.out.println("session is close !");

? ? ? ? }

? ? }



? ? public void testCreate(String znode,String data ) {

? ? ? ? String result = null;

? ? ? ? try {

? ? ? ? ? ? result = zookeeper.create(str, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? LOGGER.error(e.getMessage());

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? System.out.println("create znode " + result);

? ? }


? ? public void testDelete(String znode) {

? ? ? ? try {

? ? ? ? ? ? zookeeper.delete(znode, -1);

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? LOGGER.error(e.getMessage());

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? System.out.println("delete ok :" + znode);

? ? }


? ? public void testGetData(String znode) {

? ? ? ? String result = null;

? ? ? ? try {

? ? ? ? ? ? byte[] bytes = zookeeper.getData(znode, null, null);

? ? ? ? ? ? result = new String(bytes);

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? LOGGER.error(e.getMessage());

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? System.out.println(znode+"數(shù)據(jù)為:" + result);

? ? }


? ? public void testSetData(String znode,String data) {

? ? ? ? Stat tempStat = null;

? ? ? ? try {

? ? ? ? ? ? tempStat = zookeeper.setData(znode, data.getBytes(), -1);

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? LOGGER.error(e.getMessage());

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? System.out.println("設(shè)置數(shù)據(jù)成功" + znode + "版本為:" + tempStat.getVersion());

? ? }


? ? public void testExists(String znode) {

? ? ? ? Stat tempStat = null;

? ? ? ? try {

? ? ? ? ? ? tempStat = zookeeper.exists(znode, false);

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? LOGGER.error(e.getMessage());

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? System.out.println(tempStat.getCzxid() == 0 ? "不存在":"存在");

? ? }


? ? public void testGetChildren(String znode) {

? ? ? ? List<String> list = null;

? ? ? ? try {

? ? ? ? ? ? list = zookeeper.getChildren(znode, false);

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? LOGGER.error(e.getMessage());

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? if(list.isEmpty()) {

? ? ? ? ? ? System.out.println(path + "沒有子節(jié)點");

? ? ? ? }else {

? ? ? ? ? ? System.out.println(path + "有子節(jié)點");

? ? ? ? ? ? for(String childrenNode : list) {

? ? ? ? ? ? ? ? System.out.println(" " + childrenNode );

? ? ? ? ? ? }

? ? ? ? }

? ? }


? ? public void testSetAcl(String znode,String acl) {

? ? ? ? String aclNode = znode;

? ? ? ? String scheme = "digest";

? ? ? ? String authInfo = acl;

? ? ? ? List<ACL> acls = new ArrayList<ACL>();

? ? ? ? try {

? ? ? ? ? ? Id id1 = new Id(scheme,DigestAuthenticationProvider.generateDigest(authInfo));

? ? ? ? ? ? ACL acl1 = new ACL(ZooDefs.Perms.ALL, id1);

? ? ? ? ? ? acls.add(acl1);

? ? ? ? ? ? //Id id2 = new Id(scheme,DigestAuthenticationProvider.generateDigest("guest:guest"));

? ? ? ? ? ? // ACL acl2 = new ACL(ZooDefs.Perms.READ, id2);

? ? ? ? ? ? //acls.add(acl2);

? ? ? ? } catch (NoSuchAlgorithmException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? try {

? ? ? ? ? ? zookeeper.setACL(aclNode, acls, -1);

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? LOGGER.error(e.getMessage());

? ? ? ? ? ? e.printStackTrace();


? ? ? ? }

? ? ? ? System.out.println("znode:" + aclNode + "認(rèn)證方式:" + scheme + "認(rèn)證信息:" + authInfo);

? ? }



? ? public void testGetAcl(String znode) {

? ? ? ? String getAclNode = znode;

? ? ? ? List<ACL> list = null;

? ? ? ? try {

? ? ? ? ? ? list = zookeeper.getACL(getAclNode, new Stat());

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? LOGGER.error(e.getMessage());

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? if(list.isEmpty()) {

? ? ? ? ? ? System.out.println(getAclNode + " 不存在");

? ? ? ? }else {

? ? ? ? ? ? System.out.println(getAclNode + " ACL如下: ");

? ? ? ? ? ? for(ACL acl : list) {

? ? ? ? ? ? ? ? System.out.print("\t" + acl.toString());

? ? ? ? ? ? }

? ? ? ? }

? ? }



public void testAddAuthInfo(String znode,String acl) {

? ? ? ? String addAuthInfoNode = znode;

? ? ? ? String scheme = "digest";

? ? ? ? String authInfo = acl;

? ? ? ? String result = null;

? ? ? ? try {

? ? ? ? ? ? byte[] bytes = zookeeper.getData(addAuthInfoNode, null, null);

? ? ? ? ? ? result = new String(bytes);

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? System.out.println("獲取異常" + result + " 報錯:" + e.getMessage());

? ? ? ? }

? ? ? ? zookeeper.addAuthInfo(scheme, authInfo.getBytes());

? ? ? ? try {

? ? ? ? ? ? byte[] bytes = zookeeper.getData(addAuthInfoNode, null, null);

? ? ? ? ? ? result = new String(bytes);

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? System.out.println("添加異常" + result + " 報錯:" + e.getMessage());

? ? ? ? }

? ? ? ? System.out.println("添加成功" + result);

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末妓局,一起剝皮案震驚了整個濱河市扮休,隨后出現(xiàn)的幾起案子扫尺,更是在濱河造成了極大的恐慌启昧,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,640評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件疆柔,死亡現(xiàn)場離奇詭異咒精,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)旷档,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評論 3 395
  • 文/潘曉璐 我一進(jìn)店門模叙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人鞋屈,你說我怎么就攤上這事范咨」拭伲” “怎么了?”我有些...
    開封第一講書人閱讀 165,011評論 0 355
  • 文/不壞的土叔 我叫張陵渠啊,是天一觀的道長输吏。 經(jīng)常有香客問我,道長替蛉,這世上最難降的妖魔是什么贯溅? 我笑而不...
    開封第一講書人閱讀 58,755評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮躲查,結(jié)果婚禮上它浅,老公的妹妹穿的比我還像新娘。我一直安慰自己镣煮,他們只是感情好姐霍,可當(dāng)我...
    茶點故事閱讀 67,774評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著典唇,像睡著了一般镊折。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蚓聘,一...
    開封第一講書人閱讀 51,610評論 1 305
  • 那天腌乡,我揣著相機(jī)與錄音,去河邊找鬼夜牡。 笑死,一個胖子當(dāng)著我的面吹牛侣签,可吹牛的內(nèi)容都是我干的塘装。 我是一名探鬼主播,決...
    沈念sama閱讀 40,352評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼影所,長吁一口氣:“原來是場噩夢啊……” “哼蹦肴!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起猴娩,我...
    開封第一講書人閱讀 39,257評論 0 276
  • 序言:老撾萬榮一對情侶失蹤阴幌,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后卷中,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體矛双,經(jīng)...
    沈念sama閱讀 45,717評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,894評論 3 336
  • 正文 我和宋清朗相戀三年蟆豫,在試婚紗的時候發(fā)現(xiàn)自己被綠了议忽。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,021評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡十减,死狀恐怖栈幸,靈堂內(nèi)的尸體忽然破棺而出愤估,到底是詐尸還是另有隱情,我是刑警寧澤速址,帶...
    沈念sama閱讀 35,735評論 5 346
  • 正文 年R本政府宣布玩焰,位于F島的核電站,受9級特大地震影響芍锚,放射性物質(zhì)發(fā)生泄漏震捣。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,354評論 3 330
  • 文/蒙蒙 一闹炉、第九天 我趴在偏房一處隱蔽的房頂上張望蒿赢。 院中可真熱鬧,春花似錦渣触、人聲如沸羡棵。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,936評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽皂冰。三九已至,卻和暖如春养篓,著一層夾襖步出監(jiān)牢的瞬間秃流,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,054評論 1 270
  • 我被黑心中介騙來泰國打工柳弄, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留舶胀,地道東北人。 一個月前我還...
    沈念sama閱讀 48,224評論 3 371
  • 正文 我出身青樓碧注,卻偏偏與公主長得像嚣伐,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子萍丐,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,974評論 2 355

推薦閱讀更多精彩內(nèi)容