/** * get the data of path * @param path the node path * @return the byte data of the path */ public byte[] get(String path){ try { return client.getData().forPath(path); } catch (Exception e){ handleConnectionLoss(e); throw new ZkException(e); } }
/** * Check the path exists or not * @param path the path * @return return true if the path exists, or false */ public Boolean checkExists(String path){ try { Stat pathStat = client.checkExists().forPath(path); return pathStat != null; } catch (Exception e) { handleConnectionLoss(e); throw new ZkException(e); } }
/** * Create an ephemeral path * @param path path * @param data string data * @return the path created */ public String createEphemeral(String path, String data){ try { return client.create().withMode(CreateMode.EPHEMERAL).forPath(path, data.getBytes("UTF-8")); } catch (Exception e) { handleConnectionLoss(e); throw new ZkException(e); } }
public Boolean update(String path, byte[] data){ try { client.setData().forPath(path, data); return Boolean.TRUE; } catch (Exception e) { handleConnectionLoss(e); throw new ZkException(e); } }
/** * Create an persistent path * @param path path * @param data byte data * @return the path created */ public String create(String path, byte[] data) { try { return client.create().withMode(CreateMode.PERSISTENT).forPath(path, data); } catch (Exception e) { handleConnectionLoss(e); throw new ZkException(e); } }
/** * Create an persistent path * @param path path * @param data byte data * @return the path created */ public String createSequential(String path, byte[] data) { try { return client.create().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath(path, data); } catch (Exception e) { handleConnectionLoss(e); throw new ZkException(e); } }
/** * Delete the node * @param path node path */ public void delete(String path) { try { client.delete().forPath(path); } catch (Exception e){ handleConnectionLoss(e); throw new ZkException(e); } }
/** * Create an persistent path * @param path path * @param obj a object * @return the path created */ public String createSequentialJson(String path, Object obj) { try { return createSequential(path, JSON.toJSONString(obj).getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { handleConnectionLoss(e); throw new ZkException(e); } }
/** * Create an ephemeral path * @param path path * @param data byte data * @return the path created */ public String createEphemeral(String path, byte[] data) { try { return client.create().withMode(CreateMode.EPHEMERAL).forPath(path, data); } catch (Exception e) { handleConnectionLoss(e); throw new ZkException(e); } }
/** * Get the children of the path * @param path the path * @return the children of the path */ public List<String> gets(String path){ try { if (!checkExists(path)){ return Collections.emptyList(); } return client.getChildren().forPath(path); } catch (Exception e) { handleConnectionLoss(e); throw new ZkException(e); } }
/** * Create an ephemeral path * @param path path * @param data byte data * @return the path created * @throws Exception */ public String createEphemeralSequential(String path, byte[] data) { try { return client.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(path, data); } catch (Exception e) { handleConnectionLoss(e); throw new ZkException(e); } }
/** * Create an ephemeral path * @param path path * @param data string data * @return the path created * @throws Exception */ public String createEphemeralSequential(String path, String data) { try { return createEphemeralSequential(path, data.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { handleConnectionLoss(e); throw new ZkException(e); } }
/** * Create a node if not exists * @param path path * @param data path data * @return return true if create * @throws Exception */ public Boolean createIfNotExists(String path, String data) { try { return createIfNotExists(path, data.getBytes("UTF-8")); } catch (Exception e) { handleConnectionLoss(e); throw new ZkException(e); } }
/** * Create an persistent path * @param path path * @param data byte data * @return the path created */ public String createSequential(String path, String data) { try { return createSequential(path, data.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { handleConnectionLoss(e); throw new ZkException(e); } }
/** * Create a node if not exists * @param path path * @param data path data * @return return true if create */ public Boolean createIfNotExists(String path, byte[] data) { try { Stat pathStat = client.checkExists().forPath(path); if (pathStat == null){ String nodePath = client.create().forPath(path, data); return Strings.isNullOrEmpty(nodePath) ? Boolean.FALSE : Boolean.TRUE; } } catch (Exception e) { handleConnectionLoss(e); throw new ZkException(e); } return Boolean.FALSE; }
/** * Create an persistent path * @param path path * @param data string data * @return the path created */ public String create(String path, String data){ try { return create(path, data.getBytes("UTF-8")); } catch (Exception e) { handleConnectionLoss(e); throw new ZkException(e); } }
/** * Delete the node recursively * @param path the node path */ public void deleteRecursively(String path){ try { client.delete().deletingChildrenIfNeeded().forPath(path); } catch (Exception e){ handleConnectionLoss(e); throw new ZkException(e); } }
/** * Delete the node if the node exists * @param path node path */ public void deleteIfExists(String path) { try { if(checkExists(path)){ delete(path); } } catch (Exception e){ handleConnectionLoss(e); throw new ZkException(e); } }
/** * Delete the node recursively if the path exists * @param path the node path */ public void deleteRecursivelyIfExists(String path){ try { if(checkExists(path)){ deleteRecursively(path); } } catch (Exception e){ handleConnectionLoss(e); throw new ZkException(e); } }
/** * Make directories if necessary * @param dir the dir * @return return true if mkdirs successfully, or throw ZkException */ public Boolean mkdirs(String dir){ try { EnsurePath clientAppPathExist = new EnsurePath("/" + client.getNamespace() + slash(dir)); clientAppPathExist.ensure(client.getZookeeperClient()); return Boolean.TRUE; } catch (Exception e) { handleConnectionLoss(e); throw new ZkException(e); } }