【黑馬】<史上最全面的Hadoop入門教程> day2 zookeeper 代碼


pom.iml

需要添加的代碼

<!--<repositories>
            <repostitory>
            <id>cloudera</id>
            <url>https://repository.cloudera.com/artifactory/cloudera-repos/
            </url>
            </repostitory>
        </repositories> -->
    <dependencies>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.12.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.collections</groupId>
            <artifactId>google-collections</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!--java編譯插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

操作節點代碼:

package cn.hbaf.zookeeper_api;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import org.apache.curator.framework.recipes.cache.TreeCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.junit.Test;

public class ZookeeperAPITest {

    /*
    節點的watch機制
     */

    @Test
    public void watchZnode() throws Exception {
        //1: 定制重試策略
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,1);

        String conectionStr = "192.168.52.100:2181,192.168.52.110:2181,192.168.52.120:2181";

        CuratorFramework client = CuratorFrameworkFactory.newClient(conectionStr,8000,8000,retryPolicy);

        client.start();
        //4:創建TreeCache對象,指定要監控的節點路徑
        TreeCache treeCache = new TreeCache(client,"/hello2");

    // 5:自定義一個監聽器
    treeCache.getListenable().addListener(new TreeCacheListener() {
      @Override
      public void childEvent(
          CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent)
          throws Exception {
        ChildData data = treeCacheEvent.getData();
        if (data != null) {
          switch (treeCacheEvent.getType()) {
            case NODE_ADDED:
              System.out.println("監控到有新增節點");
              break;
            case NODE_REMOVED:
              System.out.println("監控到有節點被移除");
              break;
            case NODE_UPDATED:
              System.out.println("有節點更新");
              break;
            default:
                  break;
          }
        }
      }
    });
    //開始監聽
    treeCache.start();
    Thread.sleep(200000);
    }

  /*
  獲取節點數據
  */

  @Test
  public void getZnodeData() throws Exception {
    //定制重試策略
      RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,1);

      String conectionStr = "192.168.52.100:2181,192.168.52.110:2181,192.168.52.120:2181";

      CuratorFramework client = CuratorFrameworkFactory.newClient(conectionStr,8000,8000,retryPolicy);

      client.start();


      byte [] bytes = client.getData().forPath("/hello");
      System.out.println(new String(bytes));
      client.close();

    }

    /*
    設置節點數據
    */

  @Test
  public void setZnodeData() throws Exception {
    //定制重試策略
      RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,1);

      String conectionStr = "192.168.52.100:2181,192.168.52.110:2181,192.168.52.120:2181";

      CuratorFramework client = CuratorFrameworkFactory.newClient(conectionStr,8000,8000,retryPolicy);

      client.start();


      client.setData().forPath("/hello","zookeeper".getBytes());

      client.close();

    }

    /*
    創建臨時節點
    */

    @Test
    public void createTmpZnode() throws Exception {
        //1:定制重試策略
        /*
        param1:重試時間間隔
        param2:重試最大次數
        * */
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,1);
        //2:獲取一個客戶端對象
        /*
        * param1:服務器列表
        * param2:會話超時時間
        * param3:鏈接超時時間
        * param4:重試策略
        * */
        String connectionStr = "192.168.52.100:2181,192.168.52.110:2181,192.168.52.120:2181";
        CuratorFramework client = CuratorFrameworkFactory.newClient(connectionStr,8000,8000,retryPolicy);

        //3:開啟客戶端
        client.start();

        //4:創建節點
        client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/hello3","world".getBytes());

        Thread.sleep(5000);
        //5:關閉客戶端
        client.close();
    }
    /*
        創建永久節點
     */

    @Test
    public void createZnode() throws Exception {
        //1:定制重試策略
        /*
        param1:重試時間間隔
        param2:重試最大次數
        * */
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,1);
        //2:獲取一個客戶端對象
        /*
        * param1:服務器列表
        * param2:會話超時時間
        * param3:鏈接超時時間
        * param4:重試策略
        * */
        String connectionStr = "192.168.52.100:2181,192.168.52.110:2181,192.168.52.120:2181";
        CuratorFramework client = CuratorFrameworkFactory.newClient(connectionStr,8000,8000,retryPolicy);

        //3:開啟客戶端
        client.start();

        //4:創建節點
        client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/hello2","world".getBytes());

        //5:關閉客戶端
        client.close();
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM