• ZooKeeper系列4:ZooKeeper API简介及编程


    问题导读:
    1.ZooKeeper API 共包含几个包?
    2.如何使用ZooKeeper API 创建zookeeper应用程序?



    1)ZooKeeper API 简介
     
    ZooKeeper API 共包含 5 个包,分别为: org.apache.zookeeper , org.apache.zookeeper.data ,org.apache.zookeeper.server , org.apache.zookeeper.server.quorum 和org.apache.zookeeper.server.upgrade 。其中 org.apache.zookeeper 包含 ZooKeeper 类,它我们编程时最常用的类文件。
    这个类是 ZooKeeper 客户端库的主要类文件。如果要使用 ZooKeeper 服务,应用程序首先必须创建一个Zookeeper 实例,这时就需要使用此类。一旦客户端和 ZooKeeper 服务建立起连接, ZooKeeper 系统将会分配给此连接回话一个 ID 值,并且客户端将会周期地向服务器发送心跳来维持会话的连接。只要连接有效,客户端就可以调用 ZooKeeper API 来做相应的处理。
    它提供了表 1 所示几类主要方法 , :
      表 1 : ZooKeeper API 描述
    功能
    描述
    create
    在本地目录树中创建一个节点
    delete
    删除一个节点
    exists
    测试本地是否存在目标节点
    get/set data
    从目标节点上读取 / 写数据
    get/set ACL
    获取 / 设置目标节点访问控制列表信息
    get children
    检索一个子节点上的列表
    sync
    等待要被传送的数据
     
     
    2)ZooKeeper API 的使用
     
    这里,笔者通过一个例子来简单介绍,如何使用 ZooKeeper API 编写自己的应用程序,见代码清单 1 :
    代码清单 1 : ZooKeeper API 的使用
    1. import java.io.IOException;
    2. import org.apache.zookeeper.CreateMode;
    3. import org.apache.zookeeper.KeeperException;
    4. import org.apache.zookeeper.Watcher;
    5. import org.apache.zookeeper.ZooDefs.Ids;
    6. import org.apache.zookeeper.ZooKeeper;
    7. public class demo {
    8.      // 会话超时时间,设置为与系统默认时间一致
    9.      private static final int SESSION_TIMEOUT=30000;
    10.     
    11.      // 创建 ZooKeeper 实例
    12.      ZooKeeper zk;
    13.     
    14.      // 创建 Watcher 实例
    15.      Watcher wh=new Watcher(){
    16.             public void process(org.apache.zookeeper.WatchedEvent event)
    17.             {
    18.                     System.out.println(event.toString());
    19.             }
    20.      };
    21.     
    22.      // 初始化 ZooKeeper 实例
    23.      private void createZKInstance() throws IOException
    24.      {             
    25.             zk=new ZooKeeper("localhost:2181",demo.SESSION_TIMEOUT,this.wh);
    26.            
    27.      }
    28.     
    29.      private void ZKOperations() throws IOException,InterruptedException,KeeperException
    30.      {
    31.             System.out.println("/n1. 创建 ZooKeeper 节点 (znode : zoo2, 数据: myData2 ,权限:OPEN_ACL_UNSAFE ,节点类型: Persistent");
    32.             zk.create("/zoo2","myData2".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    33.            
    34.             System.out.println("/n2. 查看是否创建成功: ");
    35.             System.out.println(new String(zk.getData("/zoo2",false,null)));
    36.                            
    37.             System.out.println("/n3. 修改节点数据 ");
    38.             zk.setData("/zoo2", "shenlan211314".getBytes(), -1);
    39.            
    40.             System.out.println("/n4. 查看是否修改成功: ");
    41.             System.out.println(new String(zk.getData("/zoo2", false, null)));
    42.                            
    43.             System.out.println("/n5. 删除节点 ");
    44.            zk.delete("/zoo2", -1);
    45.            
    46.             System.out.println("/n6. 查看节点是否被删除: ");
    47.             System.out.println(" 节点状态: ["+zk.exists("/zoo2", false)+"]");
    48.      }
    49.     
    50.      private void ZKClose() throws  InterruptedException
    51.      {
    52.             zk.close();
    53.      }
    54.     
    55.      public static void main(String[] args) throws IOException,InterruptedException,KeeperException {
    56.             demo dm=new demo();
    57.            dm.createZKInstance( );
    58.            dm.ZKOperations();
    59.             dm.ZKClose();
    60.     }
    61. }
    复制代码


    此类包含两个主要的 ZooKeeper 函数,分别为 createZKInstance ()和 ZKOperations ()。其中createZKInstance ()函数负责对 ZooKeeper 实例 zk 进行初始化。 ZooKeeper 类有两个构造函数,我们这里使用“ ZooKeeper ( String connectString, , int sessionTimeout, , Watcher watcher )”对其进行初始化。因此,我们需要提供初始化所需的,连接字符串信息,会话超时时间,以及一个 watcher 实例。 17 行到 23 行代码,是程序所构造的一个 watcher 实例,它能够输出所发生的事件。
    ZKOperations ()函数是我们所定义的对节点的一系列操作。它包括:创建 ZooKeeper 节点( 33 行到 34 行代码)、查看节点( 36 行到 37 行代码)、修改节点数据( 39 行到 40 行代码)、查看修改后节点数据( 42 行到43 行代码)、删除节点( 45 行到 46 行代码)、查看节点是否存在( 48 行到 49 行代码)。另外,需要注意的是:在创建节点的时候,需要提供节点的名称、数据、权限以及节点类型。此外,使用 exists 函数时,如果节点不存在将返回一个 null 值。关于 ZooKeeper API 的更多详细信息,读者可以查看 ZooKeeper 的 API 文档,如下所示:
    1. http://hadoop.apache.org/zookeeper/docs/r3.3.1/api/index.html
    文章转自:http://www.aboutyun.com/thread-9311-1-1.html
  • 相关阅读:
    在SQLite中使用索引优化查询速度
    SQLite支持的SQL数据操作
    left (outer) join , right (outer) join, full (outer) join, (inner) join, cross join 区别
    深入理解Android内存管理原理(六)
    Merge Sorted Array
    Sort Colors
    Construct Binary Tree from Preorder and Inorder Traversal
    Binary Tree Postorder Traversal
    Symmetric Tree
    Rotate Image
  • 原文地址:https://www.cnblogs.com/likehua/p/3999592.html
Copyright © 2020-2023  润新知