Ceph Librados Programmatic access

Source: Internet
Author: User

Introduction

I need direct programmatic access to Ceph's object storage to see the performance difference between using gateways and without gateways. Examples of access based on Gate-way have gone through. Now the test is not to go to the gateway, with Librados directly with the Ceph cluster.

Environment configuration
1. Ceph cluster: You have a ceph cluster that is already configured and you can see the status of the cluster through ceph-s.

2. The development library installs my system is CentOS6.5 installs the related development package with the following command (c/D + + development package)

sudo yum install Librados2-devel
After the installation is successful, you can see the corresponding header file under the/usr/include/rados path

Sample Program
The instance program is from the official website and can be used as an example
http://docs.ceph.com/docs/master/rados/api/librados-intro/

#include <rados/librados.hpp> #include <string> #include <list>int main (int argc, const char **argv) { int ret = 0;//Get cluster handle and connect to clusterstd::cout<< "Ceph cluster connect begin." <<std::endl;std::string cluster_name ("Ceph"); std::string user_name ("Client.admin"); Librados::rados cluster; ret = Cluster.init2 (User_name.c_str (), Cluster_name.c_str (), 0); if (Ret < 0) {Std::cerr << ' couldn ' t initialize t He cluster handle! Error "<< ret << std::endl;ret = Exit_failure;return 1;} 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 Configurat Ion file! Error "<< ret << std::endl;ret = Exit_failure;return 1;} else {std::cout << "Read the Ceph configuration file succeed." << Std::endl;} ret = Cluster.connect (); if (Ret < 0) {Std::cerr << ' couldn ' t connect to cluster! Error "<< ret << std::endl;ret = Exit_failure;return 1;} Else{std::cout << "Connected to the cluster." << Std::endl;} std::cout<< "Ceph Cluster connect end." <<std::endl;//IO context poolname pool-1std::cout<< "Ceph Cluster create IO context for pool begin." <<std::endl;librados::ioctx io_ctx; std::string pool_name ("Pool-1"); ret = Cluster.ioctx_create (pool_name.c_ STR (), 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;} std::cout<< "Ceph Cluster Create IO context for pool end." <<std::endl;//write an Object synchronouslystd::cout<< "write an object synchronously begin." <<std::endl;librados::bufferlist bl;std::string objectId ("HW"); std::string objectcontent ("Hello World!"); Bl.append (objectcontent); ret = Io_ctx.write_full ("HW", BL); if (Ret < 0) {Std::cerr << "CouldN ' t write object! Error "<< ret << std::endl;exit (exit_failure);} else {std::cout << "wrote new object ' HW '" << Std::endl;} std::cout<< "Write an object synchronously end." <<std::endl;//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 asynchronouslylibrados::bufferlist read_buf;int Read_len = 4194304;//c reate 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 P Rint contentread_completion->wait_for_complete (); Read_completion->get_return_value ();std::cout<< " Object Name: "<< objectId << \ n" << "Content:" << read_buf.c_str () << Std::endl;/Read T He 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 poolsstd::list<std::str Ing> pools cluster.pool_list (pools) std::cout << "List of pools from this cluster handle" << Std::endl; f or (Std::list<std::string>::iterator i = Pools.begin (); I! = Pools.end (); ++i) std::cout << *i << std::en dl;//Print the list of Objectslibrados::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::co UT << "\ t" << Oit->first << Std::endl;} Remove the Xattrio_ctx.rmxattr (objectId, "Lang");//Remove the Object.io_ctx.remove (objectId);//Cleanupio_ Ctx.close (); Cluster.shutdown (); return 0;}

Compiling instructions

g++-g-c cephclient.cxx-o cephclient.og++-G cephclient.o-lrados-o cephclient

Result output

[[email protected]_wenzhou-16-34 ceph-rados]#. /cephclient ceph Cluster Connect begin. Created a cluster handle. Read the Ceph configuration file succeed.connected to the Cluster.ceph cluster connect end.ceph cluster Create IO context For pool begin. Created an ioctx for the Pool.ceph Cluster create IO context for pool end. Write an object synchronously begin. Wrote new object ' HW ' Write an object synchronously end. Object Name:hwContent:Hello world! Object xattr:en_uslist of pools from this cluster Handlerbdpool-1pool-2.rgw.rgw.root.rgw.control.rgw.gc.rgw.buckets.rgw.buckets.index.log.intent-log.usage.users.users.emai L.users.swift.users.uidlist of objects from this poolrb.0.d402.238e1f29.00000000ee00rb.0.d402.238e1f29.000000015000rb.0.d402.238e1f29.00000000fa2frb.0.d402.238e1f29.00000 001ac00rb.0.d402.238e1f29.000000012000 

Interface description
The instance code contains the main interfaces, which are:
1. Cluster handle creation
2. Cluster Connection
3. IO Context Environment initialization
4. Object reading and writing
5. IO Context shutdown
6. Cluster handle shutdown

Description
I was referring to the official document, I went through the relevant process, there is not clear where the network can be directly crossing.

C/c++/java/python/php-related visits are described in the official website.

PS: Test data to be added.

Ceph Librados Programmatic access

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.