ceph中librados接口怎么用

发布时间:2021-12-17 10:30:59 作者:小新
来源:亿速云 阅读:359

小编给大家分享一下ceph中librados接口怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

 rados_getxattr获取对象属性
 rados_getxattrs获取对象属性列表
 rados_getxattrs_next从获取的对象属性列表中读取值
 rados_getxattrs_end释放rados_getxattrs获取的指针
 rados_setxattr设置对象的自定义属性
 rados_rmxattr删除对象的自定义属性
 rados_stat返回对象的时间、大小
 rados_tmap_update
 rados_tmap_put
 rados_tmap_get

 rados_tmap_to_omap

使用示例

#include <rados/librados.hpp>
#include <string>
#include <list>
int main(int argc,  const  char  **argv)
{
   int ret  =  0 ;

   /*
   * Errors are not checked to avoid pollution.
   * After each Ceph operation:
   * if (ret < 0) error_condition
   * else success
   */

   // Get cluster handle and connect to cluster
  std::string cluster_name("ceph");
  std::string user_name("client.admin");
  librados::Rados cluster ;
  cluster.init2(user_name.c_str(), cluster_name.c_str(),  0);
  cluster.conf_read_file("/etc/ceph/ceph.conf");
  cluster.connect();

   // IO context
  librados::IoCtx io_ctx ;
  std::string pool_name("data");
  cluster.ioctx_create(pool_name.c_str(), io_ctx);

   // Write an object synchronously
  librados::bufferlist bl;
  std::string objectId("hw");
  std::string objectContent("Hello World!");
  bl.append(objectContent);
  io_ctx.write(objectId, bl, objectContent.size(),  0);

   // Add an xattr to the object.
  librados::bufferlist lang_bl;
  lang_bl.append("en_US");
  io_ctx.setxattr(objectId,  "lang", lang_bl);

   // Read the object back asynchronously
  librados::bufferlist read_buf;
  int read_len  =  4194304;
   //Create I/O Completion.
  librados::AioCompletion  *read_completion  =  librados::Rados::aio_create_completion();
   //Send read request.
  io_ctx.aio_read(objectId, read_completion,  &read_buf, read_len,  0 );

   // Wait for the request to complete, and print content
  read_completion->wait_for_complete();
  read_completion->get_return_value();
  std::cout<<  "Object name: "  << objectId  <<  "\n"
      <<  "Content: "  << read_buf.c_str()  << std::endl ;

   // Read the xattr.
  librados::bufferlist lang_res;
  io_ctx.getxattr(objectId,  "lang", lang_res);
  std::cout<<  "Object xattr: "  << lang_res.c_str()  << std::endl ;


   // Print the list of pools
  std::list<std::string> pools ;
  cluster.pool_list(pools );
  std::cout  <<  "List of pools from this cluster handle" << std::endl ;
  for (std::list<std::string>::iterator i = pools.begin(); i != pools.end(); ++i)
    std::cout << *i << std::endl;
   // Print the list of objects
  librados::ObjectIterator oit=io_ctx.objects_begin();
  librados::ObjectIterator oet=io_ctx.objects_end();
  std::cout<<  "List of objects from this pool" << std::endl ;
   for(; oit!= oet; oit++ )  {
    std::cout  <<  "\t"  << oit->first  << std::endl ;
   }

   // Remove the xattr
  io_ctx.rmxattr(objectId,  "lang");

   // Remove the object.
  io_ctx.remove(objectId);

   // Cleanup
  io_ctx.close();
  cluster.shutdown();

  return  0 ;
}

保存为cephrados.cpp,编译命令行如下

g++ cephrados.cpp -lrados -o cephrados

rados getxattr rbd.py lang -p data显示data池中对象rbd.py的扩展属性lang的值

rados listxattr -p data rbd.py显示data池中对象rbd.py的扩展属性,如果没有设置显示为空

rados stat rbd.py -p data 显示data池中对象rbd.py的大小、时间

以上是“ceph中librados接口怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. TypeScript中接口怎么用
  2. Ceph纠删码怎么用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ceph librados

上一篇:Spark Stream怎么使用

下一篇:python匿名函数怎么创建

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》