Skip to content
CatKang edited this page Feb 24, 2018 · 1 revision

使用

下载并进入zeppelin目录:

git submodule init
git submodule update

进入libzp/libzp,编译:

make

在output/lib 下获得libzp.a

Example

#include <string>
#include <iostream>
#include "libzp/include/zp_client.h"
void usage() {
  std::cout << "usage:\n"
            << "      zp_timeout host port table timeout\n";
}

int main(int argc, char* argv[]) {
  if (argc != 5) {
    usage();
    return -1;
  }

  // Create client with ip, port and table name, the table must be already exist, you may create table by zp_manager first
  libzp::Options opt;
  opt.op_timeout = atoi(argv[4]);
  opt.meta_addr.push_back(libzp::Node(argv[1], atoi(argv[2])));

  libzp::Client* client = new libzp::Client(opt, argv[3]);
  assert(client);
  // Set key value
  libzp::Status s = client->Set("key", "value");
  if (!s.ok()) {
    std::cout << s.ToString() << std::endl;
  } else {
    std::cout << "set ok" << std::endl;
  }

  // Get value by key
  std::string var;
  s = client->Get("key", &var);
  if (!s.ok()) {
    std::cout << s.ToString() << std::endl;
  } else {
    std::cout << "get " << var << std::endl;
  }

  // Delete key
  s = client->Delete("key");
  if (!s.ok()) {
    std::cout << s.ToString() << std::endl;
  } else {
    std::cout << "delete key" << std::endl;
  }

  // Remember to delete the client
  delete client;
  return 0;
}

Async Example

#include <unistd.h>
#include <string>
#include <vector>
#include <iostream>
#include "libzp/include/zp_client.h"

void usage() {
  std::cout << "usage:\n"
            << "      zp_async host port count\n";
}

static std::string user_data = "ASYNC";

void set_callback(const struct libzp::Result& stat, void* data) {
  std::cout << "set " << stat.key << "callback called. Result: "<< stat.ret.ToString()
    << ", usr data: "<< *(static_cast<std::string*>(data)) << std::endl;
}

void get_callback(const struct libzp::Result& stat, void* data) {
  std::cout << "get " << stat.key << " callback called. Result: "<< stat.ret.ToString()
    << ", value:" << *(stat.value)
    << ", usr data: "<< *(static_cast<std::string*>(data)) << std::endl;
}

void delete_callback(const struct libzp::Result& stat, void* data) {
  std::cout << "delete " << stat.key  << " callback called. Result: "<< stat.ret.ToString()
    << ", usr data: "<< *(static_cast<std::string*>(data)) << std::endl;
}

void mget_callback(const struct libzp::Result& stat, void* data) {
  std::cout << "mget callback called. Result: "<< stat.ret.ToString() << std::endl;
  for (auto& get : *stat.kvs) {
    std::cout << get.first << " <-> " << get.second << std::endl;
  }
  std::cout << "usr data: "<< *(static_cast<std::string*>(data)) << std::endl;
}

int main(int argc, char* argv[]) {
  if (argc != 4) {
    usage();
    return -1;
  }

  // client handle io operation
  std::cout << "create client" << std::endl;
  libzp::Client* client = new libzp::Client(argv[1], atoi(argv[2]), "parade");
  std::cout << "connect cluster" << std::endl;
  int cnt = atoi(argv[3]);

  for(int i = 0; i < cnt; i++) {
    std::string key = "key" + std::to_string(i);
    std::string value(30, 'a');
    client->Aset(key, value, set_callback, &user_data);
  }
  std::cout << std::endl;

  for(int i = 0; i < cnt; i++) {
    std::string key = "key" + std::to_string(i);
    client->Aget(key, get_callback, &user_data);
  }

  std::vector<std::string> keys;
  for(int i = 0; i < cnt; i = i + 10) {
    keys.clear();
    for (int j = 0; j < 10; ++j) {
      std::string key = "key" + std::to_string(i + j);
      keys.push_back(key);
    }
    client->Amget(keys, mget_callback, &user_data);
  }

  for(int i = 0; i < cnt; i++) {
    std::string key = "key" + std::to_string(i);
    client->Adelete(key, delete_callback, &user_data);
  }
  std::cout << std::endl;

  // wait async process finished
  sleep(10);

  delete client;
  return  0;
}

Interface


Client(const Options& options, const std::string& table);

struct Options {
  // How many milliseconds to wait for a response
  // before returning an error from a zeppelin operation. 0 means no limit
  // Default 1000
  int op_timeout;

  // meta address collection
  std::vector<Node> meta_addr;

  Options()
    : op_timeout(1000) {
      }
};
Description

Initial Zeppelin Client

Parameters

Option: Init option

table: table name

Return Value

Status::OK if success

Required

table has been created


Status Set(const std::string& key, const std::string& value, int32_t ttl = -1);

Description

Set key value into Zeppelin,add if already exist and modify if not. However if the key contains the {...} pattern, only the part between { and } is hashed. This may be useful in the future to force certain keys to be in the same partition.

Parameters

key:

value:

ttl: Expire time(second)

Return Value

Status::OK if success


Status Get(const std::string& key, std::string* value);

Description

Get value from Zeppelin

Parameters

key:

value: out parameter

Return Value

Status::OK if success, Status::NotFound if not exist


Status Delete(const std::string& key);

Description

Delete key value from Zeppelin

Parameters

key:

Return Value

Status::OK if succes


Status Mget(const std::vector< std::string>& keys, std::map<std::string, std::string>* values);

Description

mget keys value from Zeppelin

Parameters

keys:

Return Value

Status::OK if succes

value will be empty string when the corresponding key does't exist

Async Interface

Status Aset(const std::string& key, const std::string& value, zp_completion_t complietion, void* data, int32_t ttl);

Status Adelete(const std::string& key, zp_completion_t complietion, void* data);

Status Aget(const std::string& key, zp_completion_t complietion, void* data);

Status Amget(const std::vectorstd::string& keys, zp_completion_t complietion, void* data);

Description

Async interface

Parameters

complietion: callback function data: point to user defined data

Return Value

Status::OK if succes

Table Store Interface

Status PutRow(const std::string primary_key, const std::map<std::string, std::string> columns);

Put a new row or update a existing row.

  • primary_key: Row's primary key
  • columns: column's key and its value

Status GetRow(const std::string& primary_key, const std::vector<std::string> col_to_get, std::map<std::string, std::string>* columns);

Get specific row by primary key

  • primary_key: Row's primary key
  • col_to_get: Get all columns if col_to_get is empty
  • columns: results

Status DeleteRow(const std::string primary_key, const std::vector<std::string> col_to_delete);

Delete specific columns in a row, or the row itself.

  • primary_key: Row's primary key
  • col_to_delete: Delete the row if col_to_delete is empty