First, guide the storage
- The following inventory is placed in the directory Lib:
audience-annotations-0.5.0.jarjline-0.9.94.jarlog4j-1.2.17.jarnetty-3.10.6.final.jarslf4j-api-1.7.25.jarslf4j-log4j12-1.7 .25.jar
- The following inventory is placed in the root directory of the zookeeper installation:
Zookeeper-3.4.12.jar
second, the operation of the Zookeeper object
- Creation of Zookeeper objects
String connectstring = "hadoop1:2181,hadoop2:2181,hadoop3:2181"; int sessiontimeout =newnew watcher () {@Override Public void process (Watchedevent event) {...}});
Where: ConnectString is the connection zookeeper information Sessiontimeout is the session timeout time, that is, when a zookeeper more than the time there is no heartbeat, it is considered that the node failure. The Watcher is a monitor that, once triggered, will call back to the process method, and the event triggered will exist in the events object.
- Common methods for Zookeeper objects
Method name |
return value |
Role |
Each parameter |
Note |
String Create (Final String path, byte data[], list<acl> acl,createmode createmode) |
The return value is a string of path |
Create a Node |
- Path is the creation of a node path
- Data is the value in the node
- ACLs are permissions that can be obtained through an enumeration of IDs classes
- Createmode is a created type with four types, namely: Permanent, permanent serialization, short, ephemeral serialization.
|
|
Stat setData (Final String path, byte data[], int version) |
A node state object that contains various information about the node, including the version of the node change, the date the node was created, and so on. |
Set the data for a node |
- Path is the node path that modifies the data
- Data is the value after the modification
- Version is the revision of the node, 1 means all nodes
|
|
List<string> GetChildren (String path, Boolean watch) |
Node subnodes collection, only node name, not full path |
Get a list of child nodes |
- Path is the parent node path.
- Watch is whether to monitor the change of child nodes
|
|
Byte[] GetData (String path, Boolean watch, stat stat) |
The value of the node |
Gets the value of the node |
- Path is the node
- Watch is whether to monitor the data changes of the node
- Stat is the node that is explicitly in which state, and can be set to null.
|
|
void Delete (final String path, int version) |
|
Delete a node |
- Path is the node
- Version is a node, 1 means that all versions of the node are deleted
|
|
Use of zookeeper---java program