2021年ビッグデータZooKeeper(5):ZooKeeper Java API操作
2881 ワード
目次
ZooKeeper Java APIアクション
maven座標の導入
ノードの操作
ZooKeeper Java APIアクション
ここでZookeeperを操作するJavaAPIはzookeeperクライアントフレームワークCuratorを使用しており,多くのZookeeperクライアントの非常に下位層の詳細な開発作業を解決している.
Curatorにはいくつかのパッケージが含まれています.
curator-framework:zookeeperの下位apiのいくつかのパッケージ
Curator-recipes:Cacheイベントの傍受、選挙、分散ロック、分散カウンタなど、高度な特性をカプセル化
Maven依存(curatorを使用したバージョン:2.12.0、Zookeeper対応バージョン:3.4.x、バージョン間で互換性に問題がある場合、ノード操作に失敗する可能性があります):
maven座標の導入
org.apache.curator
curator-framework
2.12.0
org.apache.curator
curator-recipes
2.12.0
com.google.collections
google-collections
1.0
junit
junit
RELEASE
org.slf4j
slf4j-simple
1.7.25
org.apache.maven.plugins
maven-compiler-plugin
3.2
1.8
1.8
UTF-8
ノードの操作
/*
*/
@Test
public void createZnode() throws Exception {
//1:
/*
param1:
param2:
*/
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,1);
//2:
/*
param1: Zookeeper
param2:
param3:
param4:
*/
String connectionStr = "192.168.88.161:2181,192.168.88.162:2181,192.168.88.163:2181";
CuratorFramework client = CuratorFrameworkFactory.newClient(connectionStr, 8000, 8000, retryPolicy);
//3:
client.start();
//4:
/*
:
CreateMode.PERSISTENT:
CreateMode.PERSISTENT_SEQUENTIAL:
CreateMode.EPHEMERAL:
CreateMode.EPHEMERAL_SEQUENTIAL:
/hello2 :
world :
*/
client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/hello2","world".getBytes());
//5:
client.close();
}