<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/minding-the-zookeeper-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2016/12/16/minding-the-zookeeper-with-r/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I’ve been drafting a new R package — <a target="_blank" rel="nofollow" href="https://github.com/hrbrmstr/sergeant"><code>sergeant</code></a> — to work with <a target="_blank" rel="nofollow" href="http://drill.apache.org/">Apache Drill</a> and have found it much easier to manage having Drill operating in a single node cluster vs <code>drill-embedded</code> mode (esp when I need to add a couple more nodes for additional capacity). That means running <a target="_blank" rel="nofollow" href="https://zookeeper.apache.org/">Apache Zookeeper</a> and I’ve had the occasional need to ping the Zookeeper process to see various stats (especially when I add some systems to the cluster). </p>
<p>Yes, it’s very easy+straightforward to <code>nc hostname 2181</code> from the command line to issue commands (or use the Zookeeper CLIs) but when I’m in R I like to stay in R. To that end, I made a small reference class that makes it super-easy to query Zookeeper from within R. Said class will eventually be in a sister package to <code>sergeant</code> (and, a more detailed post on <code>sergeant</code> is forthcoming), but there may be others who want/need to poke at Zookeeper processes with <a target="_blank" rel="nofollow" href="https://zookeeper.apache.org/doc/trunk/zookeeperAdmin.html#sc_zkCommands">4-letter words</a> and this will help you stay within R. The only command not available is <code>stmk</code> since I don’t have the pressing need to set the trace mask. </p>
<p>After you source the self-contained reference class you connect and then issue commands as so:</p>
<pre><code>zk &lt;- zookeeper$new(host=&quot;drill.local&quot;)

zk$ruok()

zk$conf()
zk$stat()</code></pre>
<p>Drop a note in the comments (since this isn’t on github, yet) with any questions/issues.</p>
<pre><code>zookeeper &lt;- setRefClass(

  Class=&quot;zookeeper&quot;,

  fields=list(
    host=&quot;character&quot;,
    port=&quot;integer&quot;,
    timeout=&quot;integer&quot;
  ),

  methods=list(

    initialize=function(..., host=&quot;localhost&quot;, port=2181L, timeout=30L) {
      host &lt;&lt;- host ; port &lt;&lt;- port ; timeout &lt;&lt;- timeout; callSuper(...)
    },

    available_commands=function() {
      return(sort(c(&quot;conf&quot;, &quot;envi&quot;, &quot;stat&quot;, &quot;srvr&quot;, &quot;whcs&quot;, &quot;wchc&quot;, &quot;wchp&quot;, &quot;mntr&quot;,
                    &quot;cons&quot;, &quot;crst&quot;, &quot;srst&quot;, &quot;dump&quot;, &quot;ruok&quot;, &quot;dirs&quot;, &quot;isro&quot;, &quot;gtmk&quot;)))
    },

    connect=function(host=&quot;localhost&quot;, port=2181L, timeout=30L) {
      .self$host &lt;- host ; .self$port &lt;- port ; .self$timeout &lt;- timeout
    },

    conf=function() {
      res &lt;- .self$send_cmd(&quot;conf&quot;)
      res &lt;- stringi::stri_split_fixed(res, &quot;=&quot;, 2, simplify=TRUE)
      as.list(setNames(res[,2], res[,1]))
    },

    envi=function() {
      res &lt;- .self$send_cmd(&quot;envi&quot;)
      res &lt;- stringi::stri_split_fixed(res[-1], &quot;=&quot;, 2, simplify=TRUE)
      as.list(setNames(res[,2], res[,1]))
    },

    stat=function() {
      res &lt;- .self$send_cmd(&quot;stat&quot;)
      version &lt;- stri_replace_first_regex(res[1], &quot;^Zoo.*: &quot;, &quot;&quot;)
      res &lt;- res[-(1:2)]
      sep &lt;- which(res==&quot;&quot;)
      clients &lt;- stri_trim(res[1:(sep-1)])
      zstats &lt;- stringi::stri_split_fixed(res[(sep+1):length(res)], &quot;: &quot;, 2, simplify=TRUE)
      zstats &lt;- as.list(setNames(zstats[,2], zstats[,1]))
      list(version=version, clients=clients, stats=zstats)
    },

    srvr=function() {
      res &lt;- .self$send_cmd(&quot;srvr&quot;)
      zstats &lt;- stringi::stri_split_fixed(res, &quot;: &quot;, 2, simplify=TRUE)
      as.list(setNames(zstats[,2], zstats[,1]))
    },

    wchs=function() {
      res &lt;- .self$send_cmd(&quot;wchs&quot;)
      conn_path &lt;- stri_match_first_regex(res[1], &quot;^([[:digit:]]+) connections watching ([[:digit:]]+) paths&quot;)
      tot_watch &lt;- stri_match_first_regex(res[2], &quot;Total watches:([[:digit:]]+)&quot;)
      list(connections_watching=conn_path[,2], paths=conn_path[,3], total_watches=tot_watch[,2])
    },

    wchc=function() {
      stri_trim(.self$send_cmd(&quot;wchc&quot;)) %>% discard(`==`, &quot;&quot;) -> res
      setNames(list(res[2:length(res)]), res[1])
    },

    wchp=function() {
      .self$send_cmd(&quot;wchp&quot;) %>% stri_trim() %>% discard(`==`, &quot;&quot;) -> res
      data.frame(
        path=qq[seq(1, length(qq), 2)],
        address=qq[seq(2, length(qq), 2)],
        stringsAsFactors=FALSE
      )
    },

    mntr=function() {
      res &lt;- .self$send_cmd(&quot;mntr&quot;)
      res &lt;- stringi::stri_split_fixed(res, &quot;\t&quot;, 2, simplify=TRUE)
      as.list(setNames(res[,2], res[,1]))
    },

    cons=function() { list(clients=stri_trim(.self$send_cmd(&quot;cons&quot;) %>% discard(`==`, &quot;&quot;))) },
    crst=function() { message(.self$send_cmd(&quot;crst&quot;)) ; invisible() },
    srst=function() { message(.self$send_cmd(&quot;srst&quot;)) ; invisible() },
    dump=function() { paste0(.self$send_cmd(&quot;dump&quot;), collapse=&quot;\n&quot;) },
    ruok=function() { .self$send_cmd(&quot;ruok&quot;) == &quot;imok&quot; },
    dirs=function() { .self$send_cmd(&quot;dirs&quot;) },
    isro=function() { .self$send_cmd(&quot;isro&quot;) },
    gtmk=function() { R.utils::intToBin(as.integer(.self$send_cmd(&quot;gtmk&quot;))) },

    send_cmd=function(cmd) {
      require(purrr)
      require(stringi)
      require(R.utils)
      sock &lt;- purrr::safely(socketConnection)
      con &lt;- sock(host=.self$host, port=.self$port, blocking=TRUE, open=&quot;r+&quot;, timeout=.self$timeout)
      if (!is.null(con$result)) {
        con &lt;- con$result
        cat(cmd, file=con)
        response &lt;- readLines(con, warn=FALSE)
        a &lt;- try(close(con))
        purrr::flatten_chr(stringi::stri_split_lines(response))
      } else {
        warning(sprintf(&quot;Error connecting to [%s:%s]&quot;, host, port))
      }
    }

  )

)</code></pre>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://rud.is/b/2016/12/16/minding-the-zookeeper-with-r/"> R – rud.is</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Vo072I1JgGY"></a></div>