help out 用法用法和事例

让天下没有难学的技术
跟着实例学习ZooKeeper的用法: 缓存
跟着实例学习ZooKeeper的用法: 缓存
可以利用ZooKeeper在集群的各个节点之间缓存数据。 每个节点都可以得到最新的缓存的数据。 Curator提供了三种类型的缓存方式:Path Cache,Node Cache 和Tree Cache。
Path Cache
Path Cache用来监控一个ZNode的子节点. 当一个子节点增加, 更新,删除时, Path Cache会改变它的状态, 会包含最新的子节点, 子节点的数据和状态。 这也正如它的名字表示的那样, 那监控path。
实际使用时会涉及到四个类:
PathChildrenCache
PathChildrenCacheEvent
PathChildrenCacheListener
通过下面的构造函数创建Path Cache:
public PathChildrenCache(CuratorFramework client, String path, boolean cacheData)
想使用cache,必须调用它的start方法,不用之后调用close方法。 start有两个, 其中一个可以传入StartMode,用来为初始的cache设置暖场方式(warm):
NORMAL: 初始时为空。
BUILD_INITIAL_CACHE: 在这个方法返回之前调用rebuild()。
POST_INITIALIZED_EVENT: 当Cache初始化数据后发送一个PathChildrenCacheEvent.Type#INITIALIZED事件
public void addListener(PathChildrenCacheListener listener)可以增加listener监听缓存的改变。
getCurrentData()方法返回一个List&ChildData&对象,可以遍历所有的子节点。
这个例子摘自官方的例子, 实现了一个控制台的方式操作缓存。 它提供了三个命令, 你可以在控制台中输入。
set 用来新增或者更新一个子节点的值, 也就是更新一个缓存对象
remove 是删除一个缓存对象
list 列出所有的缓存对象
另外还提供了一个help命令提供帮助。
设置/更新、移除其实是使用client (CuratorFramework)来操作, 不通过PathChildrenCache操作:
client.setData().forPath(path, bytes);
client.create().creatingParentsIfNeeded().forPath(path, bytes);
client.delete().forPath(path);
而查询缓存使用下面的方法:
for (ChildData data : cache.getCurrentData()) {
System.out.println(data.getPath() + " = " + new String(data.getData()));
Node Cache
Path Cache用来监控一个ZNode. 当节点的数据修改或者删除时,Node Cache能更新它的状态包含最新的改变。
涉及到下面的三个类:
NodeCacheListener
想使用cache,依然要调用它的start方法,不用之后调用close方法。
getCurrentData()将得到节点当前的状态,通过它的状态可以得到当前的值。 可以使用public void addListener(NodeCacheListener listener)监控节点状态的改变。
我们依然使用上面的例子框架来演示Node Cache。
package com.colobu.zkrecipe.
import java.io.BufferedR
import java.io.InputStreamR
import java.util.A
import org.apache.curator.framework.CuratorF
import org.apache.curator.framework.CuratorFrameworkF
import org.apache.curator.framework.recipes.cache.NodeC
import org.apache.curator.framework.recipes.cache.NodeCacheL
import org.apache.curator.retry.ExponentialBackoffR
import org.apache.curator.test.TestingS
import org.apache.curator.utils.CloseableU
import org.apache.zookeeper.KeeperE
public class NodeCacheExample {
private static final String PATH = "/example/nodeCache";
public static void main(String[] args) throws Exception {
TestingServer server = new TestingServer();
CuratorFramework client = null;
NodeCache cache = null;
client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3));
client.start();
cache = new NodeCache(client, PATH);
cache.start();
processCommands(client, cache);
} finally {
CloseableUtils.closeQuietly(cache);
CloseableUtils.closeQuietly(client);
CloseableUtils.closeQuietly(server);
private static void addListener(final NodeCache cache) {
NodeCacheListener listener = new NodeCacheListener() {
public void nodeChanged() throws Exception {
if (cache.getCurrentData() != null)
System.out.println("Node changed: " + cache.getCurrentData().getPath() + ", value: " + new String(cache.getCurrentData().getData()));
cache.getListenable().addListener(listener);
private static void processCommands(CuratorFramework client, NodeCache cache) throws Exception {
printHelp();
addListener(cache);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
boolean done = false;
while (!done) {
System.out.print("& ");
String line = in.readLine();
if (line == null) {
String command = line.trim();
String[] parts = command.split("\\s");
if (parts.length == 0) {
String operation = parts[0];
String args[] = Arrays.copyOfRange(parts, 1, parts.length);
if (operation.equalsIgnoreCase("help") || operation.equalsIgnoreCase("?")) {
printHelp();
} else if (operation.equalsIgnoreCase("q") || operation.equalsIgnoreCase("quit")) {
done = true;
} else if (operation.equals("set")) {
setValue(client, command, args);
} else if (operation.equals("remove")) {
remove(client);
} else if (operation.equals("show")) {
show(cache);
Thread.sleep(1000);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
private static void show(NodeCache cache) {
if (cache.getCurrentData() != null)
System.out.println(cache.getCurrentData().getPath() + " = " + new String(cache.getCurrentData().getData()));
System.out.println("cache don't set a value");
private static void remove(CuratorFramework client) throws Exception {
client.delete().forPath(PATH);
} catch (KeeperException.NoNodeException e) {
private static void setValue(CuratorFramework client, String command, String[] args) throws Exception {
if (args.length != 1) {
System.err.println("syntax error (expected set &value&): " + command);
byte[] bytes = args[0].getBytes();
client.setData().forPath(PATH, bytes);
} catch (KeeperException.NoNodeException e) {
client.create().creatingParentsIfNeeded().forPath(PATH, bytes);
private static void printHelp() {
System.out.println("An example of using PathChildrenCache. This example is driven by entering commands at the prompt:\n");
System.out.println("set &value&: Adds or updates a node with the given name");
System.out.println("remove: Deletes the node with the given name");
System.out.println("show: Display the node's value in the cache");
System.out.println("quit: Quit the example");
System.out.println();
操作和上面的Path cache类似, 只是getCurrentData()返回的类型不同
这种类型的即可以监控节点的状态,还监控节点的子节点的状态, 类似上面两种cache的组合。 这也就是Tree的概念。 它监控整个树中节点的状态。 涉及到下面四个类。
TreeCacheListener
TreeCacheEvent
而关键的TreeCache的构造函数为
public TreeCache(CuratorFramework client, String path, boolean cacheData)
想使用cache,依然要调用它的start方法,不用之后调用close方法。
getCurrentChildren()返回cache的状态,类型为Map&String, ChildData&。 而getCurrentData()返回监控的path的数据。
public void addListener(TreeCacheListener listener)可以增加listener来监控状态的改变。
例子依然采用和上面的例子类似, 尤其和Path Cache类似。
package com.colobu.zkrecipe.
import java.io.BufferedR
import java.io.InputStreamR
import java.util.A
import java.util.M
import org.apache.curator.framework.CuratorF
import org.apache.curator.framework.CuratorFrameworkF
import org.apache.curator.framework.recipes.cache.ChildD
import org.apache.curator.framework.recipes.cache.TreeC
import org.apache.curator.framework.recipes.cache.TreeCacheE
import org.apache.curator.framework.recipes.cache.TreeCacheL
import org.apache.curator.retry.ExponentialBackoffR
import org.apache.curator.test.TestingS
import org.apache.curator.utils.CloseableU
import org.apache.curator.utils.ZKP
import org.apache.zookeeper.KeeperE
public class TreeCacheExample {
private static final String PATH = "/example/treeCache";
public static void main(String[] args) throws Exception {
TestingServer server = new TestingServer();
CuratorFramework client = null;
TreeCache cache = null;
client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3));
client.start();
cache = new TreeCache(client, PATH);
cache.start();
processCommands(client, cache);
} finally {
CloseableUtils.closeQuietly(cache);
CloseableUtils.closeQuietly(client);
CloseableUtils.closeQuietly(server);
private static void addListener(final TreeCache cache) {
TreeCacheListener listener = new TreeCacheListener() {
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
switch (event.getType()) {
case NODE_ADDED: {
System.out.println("TreeNode added: " + ZKPaths.getNodeFromPath(event.getData().getPath()) + ", value: "
+ new String(event.getData().getData()));
case NODE_UPDATED: {
System.out.println("TreeNode changed: " + ZKPaths.getNodeFromPath(event.getData().getPath()) + ", value: "
+ new String(event.getData().getData()));
case NODE_REMOVED: {
System.out.println("TreeNode removed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
System.out.println("Other event: " + event.getType().name());
cache.getListenable().addListener(listener);
private static void processCommands(CuratorFramework client, TreeCache cache) throws Exception {
printHelp();
addListener(cache);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
boolean done = false;
while (!done) {
System.out.print("& ");
String line = in.readLine();
if (line == null) {
String command = line.trim();
String[] parts = command.split("\\s");
if (parts.length == 0) {
String operation = parts[0];
String args[] = Arrays.copyOfRange(parts, 1, parts.length);
if (operation.equalsIgnoreCase("help") || operation.equalsIgnoreCase("?")) {
printHelp();
} else if (operation.equalsIgnoreCase("q") || operation.equalsIgnoreCase("quit")) {
done = true;
} else if (operation.equals("set")) {
setValue(client, command, args);
} else if (operation.equals("remove")) {
remove(client, command, args);
} else if (operation.equals("list")) {
list(cache);
Thread.sleep(1000);
} finally {
private static void list(TreeCache cache) {
if (cache.getCurrentChildren(PATH).size() == 0) {
System.out.println("* empty *");
for (Map.Entry&String, ChildData& entry : cache.getCurrentChildren(PATH).entrySet()) {
System.out.println(entry.getKey() + " = " + new String(entry.getValue().getData()));
private static void remove(CuratorFramework client, String command, String[] args) throws Exception {
if (args.length != 1) {
System.err.println("syntax error (expected remove &path&): " + command);
String name = args[0];
if (name.contains("/")) {
System.err.println("Invalid node name" + name);
String path = ZKPaths.makePath(PATH, name);
client.delete().forPath(path);
} catch (KeeperException.NoNodeException e) {
private static void setValue(CuratorFramework client, String command, String[] args) throws Exception {
if (args.length != 2) {
System.err.println("syntax error (expected set &path& &value&): " + command);
String name = args[0];
if (name.contains("/")) {
System.err.println("Invalid node name" + name);
String path = ZKPaths.makePath(PATH, name);
byte[] bytes = args[1].getBytes();
client.setData().forPath(path, bytes);
} catch (KeeperException.NoNodeException e) {
client.create().creatingParentsIfNeeded().forPath(path, bytes);
private static void printHelp() {
System.out.println("An example of using PathChildrenCache. This example is driven by entering commands at the prompt:\n");
System.out.println("set &name& &value&: Adds or updates a node with the given name");
System.out.println("remove &name&: Deletes the node with the given name");
System.out.println("list: List the nodes/values in the cache");
System.out.println("quit: Quit the example");
System.out.println();
原创文章,转载请注明: 转载自
本文链接地址:
Latest posts by colobu ()
Related posts:
(3 votes, average: 3.67 out of 5)
Loading...
(window.slotbydup=window.slotbydup || []).push({
id: '980251',
container: s,
size: '300,80',
display: 'inlay-fix'matlab中setdbprefs函数如何使用?matlab中help没有看懂,可以举个例子吗?_百度知道
matlab中setdbprefs函数如何使用?matlab中help没有看懂,可以举个例子吗?
如定义f(x)=x^2,第二个括号里面是表达式。于是f(2)=2.^2)定义了函数x^2+y^2f2=@(t)(f1(t,y)(x,@是函数指针f=@(x)(x,如f1(2.^2)表示将匿名函数@(x)(x.^2=[1 4 9]等等定义匿名函数时也可以调用别的匿名函数.^2)赋值给f.^2)就是匿名函数.^2)其中@(x)(x,x(2)))定义了函数x(1)^2+x(2)^2使用匿名函数时一定要注意函数本身的参数形式.^2+y;f(1,2))定义了函数t^2+4f3=@(x)(f1(x(1),于是f就表示该函数.^2=4,比如f1=@(x,3)表示2^2+3^2f2(3)=3表示3^2+4f3([1:3)=[1,第一个括号里面是自变量,可以写为f=@(x)(x:3]
其他类似问题
为您推荐:
matlab的相关知识
其他1条回答
不好意思 我的数学成绩不好
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁老师让举出生活中用到数学的五个例子,谁能帮帮我? HELP 写的一定要详细点!还有我是初中生,不是小学生!_百度作业帮
老师让举出生活中用到数学的五个例子,谁能帮帮我? HELP 写的一定要详细点!还有我是初中生,不是小学生!
1.自家计算每月电费、水费.
2.为室内装修户测量并计算铺地面用多少地板砖,粉刷四壁和屋顶要购买多少涂料,需多少材料费.
3.植树节活动中,根据种植面积和树苗棵数,计算行距、株距.
4.学校操场大约的面积,一件物体(一袋盐、几个苹果、一瓶墨水等)大概的重量,估计人或物的高度等.
5.帮助爸妈计算银行存款利息
6.外出旅行,帮爸妈设计旅行路线,并计算时间.旅客在车站候车室等候检票,并且排队的旅客按照一定的速度在增加,检票速度一定,当车站开放一个检票口,需用半小时可将待检旅客全部检票进站;同时开放两个检票口,只需十分钟便可将旅客全部进站,现有一班增开列车过境载客,必须在5分钟内旅客全部检票进站,问此车站至少要同时开放几个检票口?  分析:  (1) 本题是一个贴近实际的应用题,给出的数量关系具有一定的隐蔽性.仔细阅读后发现涉及到的量为:原排队人数,旅客按一定速度增加的人数,每个检票口检票的速度等.  (2) 给分析出的量一个代表符号:设检票开始时等候检票的旅客人数为x人,排队队伍每分钟增加y人,每个检票口每分钟检票z人,最少同时开n个检票口,就可在5分钟旅客全部进站.  (3) 把本质的内容翻译成数学语言:  开放一个检票口,需半小时检完,则x+3y=z  开放两个检票口,需10分钟检完,则x+10y=2×10z  开放n个检票口,最多需5分钟检完,则x+5y≤n×5z  可解得x=15z,y=0.5z  将以上两式带入得 n≥3.5z ,∴n=4.  答:需同时开放4个检票口. 买菜的价格,交水电费等等.都是可以转化为数学模型的 .最简单的就是过年的压岁钱,如何使用等等,肯定需要你详细的计算下,否则随便用,自己都不明白自己钱什么时候就用没有了.
其他类似问题
扫描下载二维码help的用法(加例子).wish和hope各自的用法和区别(例句)_百度作业帮
help的用法(加例子).wish和hope各自的用法和区别(例句)
1.help sb.to do sth. 帮助某人做某事.  Can you help me to learn English 你能帮助我学英语吗?  I can't help you to lift this stone.我不能帮你搬这块石头.  2.be of some/ no/ much help to sb.对某人有些 / 没有 /   This book is of great help to me.这本书对我很有帮助.  Is this magazine of any help to you 这本杂志对你有些帮助吗?  3.help oneself ( to )自用(食物等).  Help yourself to the fish.请随便吃鱼.  Please help yourself to some pork.请随便吃点肉.  4.help sb.into/ out of 搀扶某人进入 / 走出.  He helped the patient out of the hospital.他搀扶病人走出了医院.  Can you help the patient into the hospital 你能搀扶病人进医院吗?  5.help sb.out 帮助某人克服困难,渡过难关、解决问题、完成工作.  When I'm in trouble,he always helps me out with money.每当我处境困难时,他总是用金钱帮助我渡过难关.  Please help me out with this problem.请帮我解这道试题.  6.with the help of 在……帮助下.With the help of her,he found his lost child.在她的帮助下,他找到了失踪的小孩.  7.help sb.with sth.帮助某人做某事.  Please help me with my French.请帮我学法语.  Can you help him with this work 你能帮助他完成这项工作吗?  8.help to do sth.有助于做某事.This program helps to improve our English.这个计划有助于我们提高英语成绩.  His speech helps to understand the policy.他的演讲有助于理解这个政策.hope与wish的区别主要表现在以下几个方面:A.hope与wish都可以跟动词不定式(hope/wish to do sth.),但wish to do sth.比较正式,口气也比较强烈,而hope to do sth.所表达的愿望是最容易实现的,也就是说,是最现实的.1.Jennie hoped to give her a good education.珍妮希望给她良好的教育.2.You might tell them that I hope to be back tomorrow night.你可以告诉他们,我想明晚返回.3.I wish to express my warmest welcome to you.我愿向您表示最热烈的欢迎.4.I don't wish to leave my mother.我不希望离开母亲.B.wish之后可以跟含有动词不定式的复合宾语(wish sb.to do sth.),而hope却没有这种用法(×hope sb.to do sth).1.Why don't you wish your son to accept this post?为什么你不希望你儿子接受这个职位?2.You know I wish you to be happy,don't you?你知道我希望你幸福,你知道吗?C.hope与wish都可以跟从句,但意义和用法全然不同:从用法上讲,hope之后的宾语从句的谓语动词使用陈述语气的一般将来时或一般现在时.从意义上说,hope所表达的希望是能够实现的.而wish之后的宾语从句的谓语动词则只能使用虚拟语气,而所表达的愿望是无法实现的(宾语从句使用动词过去时或过去完成时)或难以实现的(宾语从句使用would do或could do时).1.I hope you will like(或like)the flowers.(能实现的愿望)我希望你喜欢这些花.2.I wish I were a bird.(不能实现的愿望)我希望我是一只鸟儿.3.A:“It would be nice if you had a lot of money.”如果你有许多钱就好了.B:“I do wish I had!”(=If only I had!)(与现在事实相反的愿望)我真希望能够这样.4.I wish I could have been at the committee yesterday.(与过去事实相反的愿望)要是昨天我能参加委员会会议就好了.D.有时候,wish可用来表示一种客气的请求:I wish you wouldn't smoke any more.我希望你不要再吸烟了.E.wish可用在wish+间宾+直宾(I wish you success.)结构中,而hope却不能.They wished him good luck.他们祝他好运气.
其他类似问题
扫描下载二维码Help sb. on something有这种用法吗?可否举个例子?_百度作业帮
Help sb. on something有这种用法吗?可否举个例子?
君子素荣82845
英语上没有Help sb.on something的说法!help的用法主要有三种1.help sb.do sth.(帮助某人做某事)For example:I can help you clean the classroom我可以帮你打扫教室2.help sb.to do sth.(帮助某人做某事)For example:I can help you to clean the classroom(同1一样的意思,没有区别)3.help sb.with sth.(在某方面帮助某人)For example:I can help you with your English我可以在英语方面帮助你楼主可能记错了吧还有楼上的help sb.doing sth也没有这种说法吧(反正我们班有同学考试写这样的都错了额~)
其他类似问题
是"help sb. to do sth." Or "help sb.doing sth"吧
没有。只有:help sb. to do sth.e.g.Please help me to carry this bag.
没听过这种用法
有这个用法
help sb. with sth.:
帮助某人做某事
help (sth.) on (sb) 大量给予(某人)(某物)英语教科书上的
扫描下载二维码

我要回帖

更多关于 help用法大全 的文章

 

随机推荐