Skip to content
Snippets Groups Projects
example2.cpp 1.66 KiB
Newer Older
root's avatar
root committed
#include <iostream>
#include <string>
#include <rados/librados.hpp>
 
int main(int argc, const char **argv)
{
  int ret = 0;
  /* Declare the cluster handle and required variables. */
  librados::Rados cluster;
  char cluster_name[] = "ceph";
  char user_name[] = "client.admin";
  uint64_t flags = 0;

  ret = cluster.init2(user_name, cluster_name, flags);
  if (ret < 0) {
    std::cerr << "Couldn't initialize the cluster handle! error " << ret << std::endl;
    return EXIT_FAILURE;
  } else {
           std::cout << "Created a cluster handle." << std::endl;
  }

  ret = cluster.conf_read_file("/etc/ceph/ceph.conf");
  if (ret < 0) {
    std::cerr << "Couldn't read the Ceph configuration file! error " << ret << std::endl;
    return EXIT_FAILURE;
  } else {
      std::cout << "Read the Ceph configuration file." << std::endl;
  }

  ret = cluster.connect();
  if (ret < 0) {
     std::cerr << "Couldn't connect to cluster! error " << ret << std::endl;
     return EXIT_FAILURE;
  } else {
      std::cout << "Connected to the cluster." << std::endl;
  }

  librados::IoCtx io_ctx;

  const char *pool_name = "ec62";

  ret = cluster.ioctx_create(pool_name, io_ctx);
  if (ret < 0) {
    std::cerr << "Couldn't set up ioctx! error " << ret << std::endl;
    exit(EXIT_FAILURE);
  } else {
      std::cout << "Created an ioctx for the pool." << std::endl;
  }



  librados::bufferlist bl;
  bl.append("Hello World!");
  ret = io_ctx.write_full("rados_write_example", bl);
  if (ret < 0) {
    std::cerr << "Couldn't write object! error " << ret << std::endl;
    exit(EXIT_FAILURE);
  } else {
      std::cout << "Wrote new object 'rados_write_example' " << std::endl;
  }

  return 0;
}