龍子 发表于 2015-11-21 14:22:37

zookeeper C API实例

  之前一直用zookeeper的java API,最近要用C开发,所以看了C的API及实现
  使用zookeeper C API 的步骤:
  (1)安装zookeeper,直接下载,解压,配置文件即可
  (2)安装C API
  进入./zookeeper/src/c目录
  ./configure
  make
  make install
  (3)编写调用函数,可以参考下面的例子
  (4)编译的时候注意引入所在的头文件和动态库,默认安装时头文件在/usr/local/include/c-client-src中,动态库在/usr/local/lib中,使用多线程时注意-DTHREAD
  具体的编译:gcc a.c -DTHREAD -I/usr/local/include/c-client-src/usr/local/lib/libzookeeper_mt.so-o a
  



#include<stdio.h>
#include<string.h>
#include&quot;zookeeper.h&quot;
#include&quot;zookeeper_log.h&quot;
void zktest_watcher_g(zhandle_t* zh, int type, int state, const char* path, void* watcherCtx)
{
printf(&quot;Something happened.\n&quot;);
printf(&quot;type: %d\n&quot;, type);
printf(&quot;state: %d\n&quot;, state);
printf(&quot;path: %s\n&quot;, path);
printf(&quot;watcherCtx: %s\n&quot;, (char *)watcherCtx);
}
//自己的监听函数
void watcher_myself(zhandle_t *zh,int type,int state,const char *path,void *watcherCtx)
{
printf(&quot;just for testing\n&quot;);
printf(&quot;path:%s\n&quot;,path);
}
//同步方式创建节点
void create(zhandle_t *zkhandle,char *str)
{
char path_buffer;
int bufferlen=sizeof(path_buffer);
printf(&quot;同步方式创建节点-----------------------\n&quot;);
int flag = zoo_create(zkhandle,str,&quot;hahah&quot;,5,
&ZOO_OPEN_ACL_UNSAFE,0,
path_buffer,bufferlen);
if (flag!=ZOK)
{
printf(&quot;节点创建失败 \n&quot;);
exit(EXIT_FAILURE);
}
else
{
printf(&quot;创建的节点名称为:%s\n&quot;,path_buffer);
}
}
//同步方式获取节点数据
void get(zhandle_t* zkhandle)
{
printf(&quot;同步方式获取节点数据-----------------------\n&quot;);
char buffer1;
int bufferlen1=sizeof(buffer1);
int flag1=zoo_get(zkhandle,&quot;/xyz3&quot;,0,
buffer1,&bufferlen1,NULL);
if (flag1 ==ZOK)
{
printf(&quot;节点/xyz3的数据为: %s\n&quot;,buffer1);
}
}
//exists
void exists(zhandle_t *zkhandle,char *str)
{
int flag = zoo_exists(zkhandle,str,1,NULL);
}
void wexists(zhandle_t *zkhandle,char *str)
{
int flag=zoo_wexists(zkhandle,str,watcher_myself,&quot;test&quot;,NULL);
}
//同步方式获得子节点信息
void getChildren(zhandle_t *zkhandle,char *str)
{
struct String_vector strings;
struct Stat stat;
int flag = zoo_wget_children2(zkhandle,str,
watcher_myself,&quot;testgetChildren&quot;,
&strings,&stat);
if (flag==ZOK)
{
int32_t i=0;
for (;i<strings.count;++i)
printf(&quot;%s\n&quot;,strings.data);
}
}
//获取ACL信息
void getACL(zhandle_t *zkhandle,char *str)
{
struct ACL_vector acl;
struct Stat stat;
int flag = zoo_get_acl(zkhandle,str,&acl,&stat);
if (flag==ZOK)
{
printf(&quot;-----------------the ACL of %s:\n------------&quot;,str);
printf(&quot;%d\n&quot;,acl.count);
printf(&quot;%d\n&quot;,acl.data->perms);
printf(&quot;%s\n&quot;,acl.data->id.scheme);
printf(&quot;%s\n&quot;,acl.data->id.id);
}
}
//删除节点
void delete(zhandle_t *zkhandle,char *str)
{
int flag = zoo_delete(zkhandle,str,-1);
if (flag==ZOK)
{
printf(&quot;delete node success\n&quot;);
}
}
int main(int argc, const char *argv[])
{
const char* host = &quot;192.168.0.187:2181&quot;;
int timeout = 30000;
char buffer;
int *bufferlen;
zoo_set_debug_level(ZOO_LOG_LEVEL_WARN); //设置日志级别,避免出现一些其他信息
zhandle_t* zkhandle = zookeeper_init(host,zktest_watcher_g, timeout, 0, &quot;hello zookeeper.&quot;, 0);
if (zkhandle ==NULL)
{
fprintf(stderr, &quot;Error when connecting to zookeeper servers...\n&quot;);
exit(EXIT_FAILURE);
}

char str[]=&quot;/xyz30000000014&quot;;
// wexists(zkhandle,str);
// printf(&quot;---------------\n&quot;);
//create(zkhandle,str);
//get(zkhandle);
// getChildren(zkhandle,str);
//getACL(zkhandle,str);
delete(zkhandle,str);

}
参考:
(1)http://www.cnblogs.com/haippy/archive/2013/02/21/2919365.html Zookeeper
C API 指南一(准备工作)
(2)http://blog.iyunv.com/uid-16989284-id-2303676.html关于引入头文件,库
页: [1]
查看完整版本: zookeeper C API实例