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
| private static final String CONNECT_STRING = "bigdata1:2181,bigdata2:2181,bigdata3:2181";
private static final Integer SESSION_TIMEOUT = 2000;
private ZooKeeper zkClient = null;
@Before public void init() throws IOException { zkClient = new ZooKeeper(CONNECT_STRING, SESSION_TIMEOUT, (event) -> { System.out.println(event.getType() + "---" + event.getPath()); try { zkClient.getChildren("/", true); } catch (Exception e) { e.printStackTrace(); } }); }
@Test public void create() throws KeeperException, InterruptedException { String create = zkClient.create("/idea", "hello world".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); System.out.println("create: " + create); }
@Test public void exists() throws KeeperException, InterruptedException { Stat stat = zkClient.exists("/idea", false); System.out.println(stat == null ? "not exists" : "exists"); }
@Test public void getChildren() throws KeeperException, InterruptedException { List<String> childs = zkClient.getChildren("/", true); childs.forEach(System.out::println); }
@Test public void get() throws KeeperException, InterruptedException { byte[] data = zkClient.getData("/idea", false, null); System.out.println(new String(data)); }
@Test public void delete() throws KeeperException, InterruptedException { zkClient.delete("/idea", -1); }
@Test public void set() throws KeeperException, InterruptedException { zkClient.setData("/idea", "hello world 2".getBytes(), -1); }
|